Skip to content

Commit eda4509

Browse files
committed
Updates Multiple Models
This commit updates multiple models to fix an issue preventing sessions from being saved and tracked by the clients. This commit should close #71. Signed-off-by: Lui de la Parra <Lui@mutesymphony.com>
1 parent 4af7d93 commit eda4509

File tree

3 files changed

+90
-28
lines changed

3 files changed

+90
-28
lines changed

src/models/agent.model.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,29 @@ class Agent extends EmbeddedDocument {
210210
* @return {Object} request The configured axios instance to use for a request.
211211
*/
212212
request(data, parameters = {}) {
213+
const id = uuidv4();
213214
const interceptor = instance.interceptors.request.use(
214215
({ httpAgent, httpsAgent, ...request }) => {
215216
instance.interceptors.request.eject(interceptor);
216217
return new Promise((resolve, reject) =>
217218
this.push({
218-
request: this.handleRequest(request),
219+
request: this.handleRequest(request, id),
219220
resolve,
220221
reject
221222
})
222223
);
223224
}
224225
);
225226

226-
instance.interceptors.response.use(
227-
response => this.handleResponse(response),
228-
error => this.handleError(error)
227+
const response = instance.interceptors.response.use(
228+
response => {
229+
instance.interceptors.response.eject(response);
230+
return this.handleResponse(response, id);
231+
},
232+
error => {
233+
instance.interceptors.response.eject(response);
234+
return this.handleError(error, id);
235+
}
229236
);
230237

231238
return instance(
@@ -246,17 +253,22 @@ class Agent extends EmbeddedDocument {
246253
* @description handles request data before it is sent to the resource. This function
247254
* will eventually be used to cancel the request and return the configuration body.
248255
* This function will test the url for an http proticol and reject if none exist.
249-
* @param {Object} response The axios response
256+
* @param {Object} response The axios response.
257+
* @param {String} id the request id.
250258
* @return {Promise} the request configuration object
251259
*/
252-
handleResponse(response) {
260+
handleResponse(response, id) {
261+
const token = _.get(response, 'config.headers.Authorization');
262+
if (token) {
263+
this.connection.deactivate(token, id);
264+
}
253265
if (typeof response.data !== 'object') {
254266
return Promise.reject({
255267
message: 'The Data API is currently unavailable',
256268
code: '1630'
257269
});
258270
} else {
259-
this.connection.extend(response.config.headers.Authorization);
271+
this.connection.extend(token);
260272
return response;
261273
}
262274
}
@@ -268,10 +280,12 @@ class Agent extends EmbeddedDocument {
268280
* @description handles request data before it is sent to the resource. This function
269281
* will eventually be used to cancel the request and return the configuration body.
270282
* This function will test the url for an http proticol and reject if none exist.
271-
* @param {Object} config The axios request configuration
283+
* @param {Object} config The axios request configuration.
284+
* @param {String} id the request id.
272285
* @return {Promise} the request configuration object
273286
*/
274-
handleRequest(config) {
287+
handleRequest(config, id) {
288+
config.id = id;
275289
return config.url.startsWith('http')
276290
? omit(config, ['params.request', 'data.request'])
277291
: Promise.reject({
@@ -362,9 +376,17 @@ class Agent extends EmbeddedDocument {
362376
* function will add an expired property to the error response if it recieves a invalid
363377
* token response.
364378
* @param {Object} error The error recieved from the requested resource.
379+
* @param {String} id the request id.
365380
* @return {Promise} A promise rejection containing a code and a message
366381
*/
367-
handleError(error) {
382+
handleError(error, id) {
383+
const token = _.get(error, 'config.headers.Authorization');
384+
if (token) {
385+
this.connection.deactivate(token, id);
386+
}
387+
388+
this.connection.confirm();
389+
368390
if (error.code) {
369391
return Promise.reject({ code: error.code, message: error.message });
370392
} else if (
@@ -377,9 +399,7 @@ class Agent extends EmbeddedDocument {
377399
});
378400
} else {
379401
if (error.response.data.messages[0].code === '952')
380-
this.connection.clear(
381-
_.get(error, 'response.config.headers.Authorization')
382-
);
402+
this.connection.clear(token);
383403
return Promise.reject(error.response.data.messages[0]);
384404
}
385405
}
@@ -464,7 +484,8 @@ class Agent extends EmbeddedDocument {
464484
Object.assign(
465485
this.mutate(pending.request, (value, key) =>
466486
key.replace(/{{dot}}/g, '.')
467-
)
487+
),
488+
{ id: pending.id }
468489
),
469490
_.isEmpty(this.agent) ? {} : this.localize()
470491
)

src/models/connection.model.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Connection extends EmbeddedDocument {
104104
* @see {@link Connection#available}
105105
* @return {String} The session token.
106106
*/
107-
authentication({ headers, ...request }) {
107+
authentication({ headers, id, ...request }) {
108108
return new Promise((resolve, reject) => {
109109
const sessions = _.sortBy(
110110
this.sessions.filter(session => !session.expired()),
@@ -114,6 +114,7 @@ class Connection extends EmbeddedDocument {
114114
const session = sessions[0];
115115
session.active = true;
116116
session.url = request.url;
117+
session.request = id;
117118
session.used = moment().format();
118119
resolve({
119120
...request,
@@ -125,18 +126,6 @@ class Connection extends EmbeddedDocument {
125126
});
126127
}
127128

128-
/**
129-
* @method ready
130-
* @public
131-
* @memberof Connection
132-
* @description Saves a token retrieved from the Data API as a sessions
133-
* @see {@link session}
134-
* @return {Boolean} data a boolean indicating if the connection has a session.
135-
*/
136-
ready() {
137-
return this.sessions.filter(session => !session.expired()).length > 0;
138-
}
139-
140129
/**
141130
* @method available
142131
* @public
@@ -287,6 +276,38 @@ class Connection extends EmbeddedDocument {
287276
const session = _.find(this.sessions, session => session.token === token);
288277
if (session) session.extend();
289278
}
279+
280+
/**
281+
* @method confirm
282+
* @memberOf confirm
283+
* @description The confirm method will set the active property to false when a session does not have a
284+
* valid request id.
285+
*/
286+
confirm() {
287+
this.sessions.forEach(session => {
288+
if (_.isEmpty(session.request)) {
289+
session.active = false;
290+
}
291+
});
292+
}
293+
/**
294+
* @method deactivate
295+
* @memberOf Connection
296+
* @public
297+
* @description The deactivate method will reactive a session by setting the active property to false.
298+
* @param {String} header The header containing the token representing the session to deactivate
299+
* @param {String} id The request id.
300+
* @see {@link Agent#handleResponse}
301+
* @see {@link Agent#handleError}
302+
*/
303+
deactivate(header, id) {
304+
const token = header.replace('Bearer ', '');
305+
const session = _.find(
306+
this.sessions,
307+
session => session.token === token || session.request === id
308+
);
309+
if (session) session.deactivate();
310+
}
290311
}
291312

292313
module.exports = {

src/models/session.model.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class Session extends EmbeddedDocument {
4343
used: {
4444
type: String
4545
},
46+
/* A string containing the request created when the token is in use..
47+
* @member Session#request
48+
* @type String
49+
*/
50+
request: {
51+
type: String
52+
},
4653
/* A boolean set if the current session is in use.
4754
* @member Session#active
4855
* @type Boolean
@@ -95,13 +102,26 @@ class Session extends EmbeddedDocument {
95102
* @method extend
96103
* @memberof Session
97104
* @public
98-
* @description This method extends a Data API session and sets it to inactive.
105+
* @description This method extends a Data API session.
99106
* @see {@link Agent#handleResponse}
100107
*/
101108
extend() {
102109
this.active = false;
103110
this.expires = moment().add(15, 'minutes').format();
104111
}
112+
113+
/**
114+
* @method deactivate
115+
* @memberOf Sessions
116+
* @public
117+
* @description This method sets deactivates a session by setting active to false.
118+
* @see {@link Agent#handleResponse}
119+
* @see {@link Agent#handleError}
120+
*/
121+
deactivate() {
122+
this.request = "";
123+
this.active = false;
124+
}
105125
}
106126

107127
module.exports = {

0 commit comments

Comments
 (0)