Skip to content

Commit

Permalink
Updated session controller to use generators.
Browse files Browse the repository at this point in the history
(cherry picked from commit 56569e0)
  • Loading branch information
jackdh committed Mar 17, 2019
1 parent bbfbdf4 commit 3acec24
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions server/mongo/controllers/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@
// const db = require('../mongo/mongodb');
const SessionSchema = require('../schemas/sessionSchema');
const debug = require('debug')('session');
const co = require('co');

/**
* Gets the session or creates one.
* @param userID
* @returns {Promise}
*/
function getSessionInternal(userID) {
return new Promise(resolve => {
SessionSchema.findOne({ sessionID: userID })
.then(model => {
if (model) {
if (!model.entities.saved) {
model.entities.saved = {};
}
resolve(model);
} else {
SessionSchema.create({ sessionID: userID }).then(m => {
m.entities = {
context: {},
saved: {},
};
return resolve(m);
});
}
})
.catch(error => {
debug(error);
});
});
}
const getSessionInternal = userID =>
co(function* t() {
const model = yield SessionSchema.findOne({ sessionID: userID });

if (model) {
if (!model.entities.saved) {
model.entities.saved = {};
}
return model;
}
const newModel = yield SessionSchema.create({ sessionID: userID });
newModel.entities = {
context: {},
saved: {},
};
return newModel;
})
.then(data => data)
.catch(error => {
debug(error);
});

module.exports = {
getSessionInternal,
Expand Down

0 comments on commit 3acec24

Please sign in to comment.