db/SessionManager.js: completely converted to Promises/async

pull/3559/head
Ray Bellis 2019-01-28 14:44:36 +00:00
parent e58da69cfb
commit 005c0afa97
1 changed files with 11 additions and 17 deletions

View File

@ -18,13 +18,11 @@
* limitations under the License. * limitations under the License.
*/ */
var ERR = require("async-stacktrace");
var customError = require("../utils/customError"); var customError = require("../utils/customError");
var randomString = require("../utils/randomstring"); var randomString = require("../utils/randomstring");
var db = require("./DB"); var db = require("./DB");
var groupManager = require("./GroupManager"); var groupManager = require("./GroupManager");
var authorManager = require("./AuthorManager"); var authorManager = require("./AuthorManager");
const thenify = require("thenify").withCallback;
exports.doesSessionExist = async function(sessionID) exports.doesSessionExist = async function(sessionID)
{ {
@ -120,23 +118,19 @@ exports.createSession = async function(groupID, authorID, validUntil)
return { sessionID }; return { sessionID };
} }
// @TODO once external dependencies are using Promises exports.getSessionInfo = async function(sessionID)
exports.getSessionInfo = thenify(function(sessionID, callback)
{ {
// check if the database entry of this session exists // check if the database entry of this session exists
db.get("session:" + sessionID, function (err, session) let session = await db.get("session:" + sessionID);
{
if(ERR(err, callback)) return;
if (session == null) { if (session == null) {
// session does not exist // session does not exist
callback(new customError("sessionID does not exist", "apierror")) throw new customError("sessionID does not exist", "apierror");
} else { }
// everything is fine, return the sessioninfos
callback(null, session); // everything is fine, return the sessioninfos
return session;
} }
});
});
/** /**
* Deletes a session * Deletes a session
@ -199,7 +193,7 @@ exports.listSessionsOfAuthor = async function(authorID)
// this function is basically the code listSessionsOfAuthor and listSessionsOfGroup has in common // this function is basically the code listSessionsOfAuthor and listSessionsOfGroup has in common
// required to return null rather than an empty object if there are none // required to return null rather than an empty object if there are none
async function listSessionsWithDBKey(dbkey, callback) async function listSessionsWithDBKey(dbkey)
{ {
// get the group2sessions entry // get the group2sessions entry
let sessionObject = await db.get(dbkey); let sessionObject = await db.get(dbkey);