From d94b8f10d3e9118f9038a508847494354efda115 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Thu, 11 Oct 2018 00:37:43 +0000 Subject: [registration] Add functionality to the set registration state API call Recursion was needed, to get all the recursive parent groups. --- server/api/registrations.js | 52 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'server/api/registrations.js') diff --git a/server/api/registrations.js b/server/api/registrations.js index 466e9b6..3c5c8d8 100644 --- a/server/api/registrations.js +++ b/server/api/registrations.js @@ -84,7 +84,7 @@ noAuthRouter.post('/:uuid/state', (req, res) => { const uuid = req.params.uuid const state = parseInt(req.body.state) - db.client.findOne({ where: { uuid: uuid } }).then(client => { + db.client.findOne({ where: { uuid: uuid }, include: ['groups'] }).then(client => { // Client not found handling if (client === null) { res.status(404).send({ status: 'INVALID_UUID', error: 'There is no client with the provided UUID.' }) @@ -98,16 +98,26 @@ noAuthRouter.post('/:uuid/state', (req, res) => { } // If it matches, search for the next script and update the clients registrationState. - const newState = null - - // TODO: get the next script from the db. - - // Update the client's registration state - // client.updateAttributes({ - // registrationState: newState - // }) + // Get all group id's of the client. + var groupids = [] + client.groups.forEach(g => { + groupids = [...groupids, g.id] + }) - res.send({ status: 'SUCCESS', data: client }) + // Get the sort value of the current hook. + db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => { + // Gets the list of all groupids inclusive the recursive parents. + getRecursiveParents(groupids).then(gids => { + // Get the list of all hooks where the parent is null or those who fullfill the group dependency. + db.registrationhook.findAll({ where: { '$groups.id$': { $or: [null, gids] }, sortvalue: { $gt: hook.sortvalue } }, include: ['groups'], order: [['sortvalue', 'ASC']] }).then(result => { + // Update the client's registration state + client.updateAttributes({ + registrationState: result[0].id + }) + res.send({ status: 'SUCCESS' }) + }) + }) + }) }) }) @@ -134,6 +144,28 @@ noAuthRouter.get('/:uuid/nexthook', (req, res) => { module.exports.noAuthRouter = noAuthRouter +/* + * groupids: Array of group ids to get the parents from. + * + * Returns a list of the grop ids and all recursive ids of their parents. + */ +function getRecursiveParents (groupIds) { + var gids = [] + return db.group.findAll({ where: { id: groupIds }, include: ['parents'] }).then(groups => { + groups.forEach(group => { + group.parents.forEach(parent => { + if (!groupIds.includes(parent.id) && !gids.includes(parent.id)) gids = [...gids, parent.id] + }) + }) + if (gids.length === 0) return groupIds + else { + return getRecursiveParents(gids).then(r => { + return groupIds.concat(r) + }) + } + }) +} + /* * id: id of the current selected location. * name: Name of the current selected location -- cgit v1.2.3-55-g7522