Skip to content

Commit 0bd41ec

Browse files
authored
Merge pull request #19 from PerfectFit-project/383-messages-optimization
383 messages optimization
2 parents 5ac7d8c + 0b033a9 commit 0bd41ec

File tree

4 files changed

+63
-56
lines changed

4 files changed

+63
-56
lines changed

niceday-api/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { Authentication, SenseServer } = require('@sense-os/goalie-js');
1+
const {
2+
Authentication, SenseServer, Chat, ConnectionStatus, SenseServerEnvironment,
3+
} = require('@sense-os/goalie-js');
24
const schedule = require('node-schedule');
35
const path = require('path');
46
const http = require('http');
@@ -12,14 +14,18 @@ const serverPort = 8080;
1214

1315
const { THERAPIST_PASSWORD, THERAPIST_EMAIL_ADDRESS, ENVIRONMENT } = process.env;
1416
let selectedServer;
17+
let selectedServerEnv;
1518

1619
if (ENVIRONMENT === 'dev') {
1720
selectedServer = SenseServer.Alpha;
21+
selectedServerEnv = SenseServerEnvironment.Alpha;
1822
} else {
1923
selectedServer = SenseServer.Production;
24+
selectedServerEnv = SenseServerEnvironment.Production;
2025
}
2126

2227
const authSdk = new Authentication(selectedServer);
28+
const chatSdk = new Chat();
2329

2430
// swaggerRouter configuration
2531
const options = {
@@ -31,11 +37,37 @@ const options = {
3137
const expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
3238
const app = expressAppConfig.getApp();
3339

40+
function setupChat(therapistId, token) {
41+
// Setup connection
42+
chatSdk.init(selectedServerEnv);
43+
chatSdk.connect(therapistId, token);
44+
45+
// Send initial presence when connected
46+
chatSdk.subscribeToConnectionStatusChanges((connectionStatus) => {
47+
if (connectionStatus === ConnectionStatus.Connected) {
48+
chatSdk.sendInitialPresence();
49+
app.set('chatsdk', chatSdk);
50+
} else if (connectionStatus === ConnectionStatus.Disconnected) {
51+
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
52+
.then((response) => {
53+
chatSdk.connect(response.user.id, response.token)
54+
.catch((connectionError) => {
55+
throw Error(`Error during connection: ${connectionError}`);
56+
});
57+
})
58+
.catch((error) => {
59+
throw Error(`Error during relogin: ${error}`);
60+
});
61+
}
62+
});
63+
}
64+
3465
function createNicedayApiServer() {
3566
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
3667
.then((response) => {
3768
app.set('therapistId', response.user.id);
3869
app.set('token', response.token);
70+
setupChat(response.user.id, response.token);
3971
})
4072
.catch((error) => {
4173
throw Error(`Error during authentication: ${error}`);

niceday-api/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ require('isomorphic-fetch');
33
const NICEDAY_TEST_SERVERPORT = 8080;
44
const NICEDAY_TEST_USER_ID = 38527;
55
const NICEDAY_TEST_TRACKER_RRULE = 'DTSTART:20210310T150000\nRRULE:FREQ=DAILY';
6+
const MOCK_ID_FROM = 1;
7+
const MOCK_ID_TO = 12345;
68
const MOCK_USER_DATA = {
79
id: NICEDAY_TEST_USER_ID,
810
userProfile: {
911
firstName: 'Mr Mock',
1012
},
1113
};
14+
const MOCK_TEST_MESSAGE = 'Test message';
1215
const MOCK_TRACKER_RESPONSE = { response: 'mock response' };
1316
const MOCK_SMOKING_TRACKER_RESPONSE = [
1417
{ value: { measures: { measureCigarettes: { sensorData: MOCK_TRACKER_RESPONSE } } } }];
@@ -45,6 +48,24 @@ describe('Tests on niceday-api server using mocked goalie-js', () => {
4548
SenseServerEnvironment: () => ({
4649
Alpha: undefined,
4750
}),
51+
Chat: jest.fn().mockImplementation(() => ({
52+
init: jest.fn(),
53+
connect: jest.fn(),
54+
markMessageAsRead: jest.fn(),
55+
subscribeToConnectionStatusChanges: jest.fn(),
56+
sendInitialPresence: jest.fn(),
57+
subscribeToIncomingMessage: (handler) => {
58+
const mockTestMessage = {
59+
from: MOCK_ID_FROM,
60+
to: MOCK_ID_TO,
61+
content: {
62+
TEXT: MOCK_TEST_MESSAGE,
63+
},
64+
};
65+
66+
handler(mockTestMessage);
67+
},
68+
})),
4869
Contacts: jest.fn().mockImplementation(() => ({
4970
getConnectedContacts: () => new Promise((resolve) => {
5071
resolve(MOCK_USER_DATA);

niceday-api/login.mjs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
import { Authentication, SenseServer } from '@sense-os/goalie-js';
44
import 'isomorphic-fetch'
55

6-
const { ENVIRONMENT } = process.env;
7-
if (ENVIRONMENT == 'dev'){
8-
const authSdk = new Authentication(SenseServer.Alpha);
9-
}
10-
else {
11-
const authSdk = new Authentication(SenseServer.Production);
12-
}
6+
const authSdk = new Authentication(SenseServer.Alpha);
7+
138

149
// Read in command line arguments
1510
var args = process.argv.slice(2)
Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
const {
2-
Authentication, Chat, SenseServer, SenseServerEnvironment, ConnectionStatus,
3-
} = require('@sense-os/goalie-js');
4-
5-
const { ENVIRONMENT, THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD } = process.env;
6-
7-
let selectedServerEnv;
8-
let selectedServer;
9-
10-
if (ENVIRONMENT === 'dev') {
11-
selectedServerEnv = SenseServerEnvironment.Alpha;
12-
selectedServer = SenseServer.Alpha;
13-
} else {
14-
selectedServerEnv = SenseServerEnvironment.Production;
15-
selectedServer = SenseServer.Production;
16-
}
17-
const authSdk = new Authentication(selectedServer);
181
/**
192
* Send a text message
203
*
@@ -23,38 +6,14 @@ const authSdk = new Authentication(selectedServer);
236
*
247
* no response value expected for this operation
258
* */
9+
2610
exports.sendTextMessage = function (req, body) {
2711
return new Promise((resolve, reject) => {
28-
const chatSdk = new Chat();
29-
chatSdk.init(selectedServerEnv);
30-
chatSdk.connect(req.app.get('therapistId'), req.app.get('token'))
31-
.catch(() => {
32-
// if the connection fails, we regenreate the token by logging in again
33-
// and we try to reconnect
34-
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
35-
.then((response) => {
36-
req.app.set('therapistId', response.user.id);
37-
req.app.set('token', response.token);
38-
chatSdk.connect(response.user.id, response.token)
39-
.catch((connectError) => {
40-
throw Error(`Error during chat connection: ${connectError}`);
41-
});
42-
})
43-
.catch((loginError) => {
44-
throw Error(`Error during authentication: ${loginError}`);
45-
});
46-
});
47-
48-
const subscriptionId = chatSdk.subscribeToConnectionStatusChanges((connectionStatus) => {
49-
if (connectionStatus === ConnectionStatus.Connected) {
50-
chatSdk.sendInitialPresence();
51-
chatSdk.sendTextMessage(body.recipient_id, body.text).then((response) => {
52-
console.log('Successfully sent the message', response);
53-
chatSdk.unsubscribeFromConnectionStatusChanges(subscriptionId);
54-
resolve();
55-
})
56-
.catch((error) => reject(error));
57-
}
58-
});
12+
const chatSdk = req.app.get('chatsdk');
13+
chatSdk.sendTextMessage(body.recipient_id, body.text).then((response) => {
14+
console.log('Successfully sent the message', response);
15+
resolve();
16+
})
17+
.catch((error) => reject(error));
5918
});
6019
};

0 commit comments

Comments
 (0)