summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/api/registrations.js52
-rw-r--r--server/models/registrationhook.js2
2 files changed, 43 insertions, 11 deletions
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' })
+ })
+ })
+ })
})
})
@@ -135,6 +145,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
* groups: List of group [{ id: <GROUP_ID>, name: <GROUP_NAME> }, ...]
diff --git a/server/models/registrationhook.js b/server/models/registrationhook.js
index 0b7566d..1e74288 100644
--- a/server/models/registrationhook.js
+++ b/server/models/registrationhook.js
@@ -18,7 +18,7 @@ module.exports = (sequelize, DataTypes) => {
})
registrationhook.associate = function (models) {
var RegistrationhookXGroup = sequelize.define('registrationhook_x_group', {}, { timestamps: false, freezeTableName: true })
- registrationhook.belongsToMany(models.group, { as: 'groups', through: RegistrationhookXGroup, foreignKey: 'registraionhookId', otherKey: 'groupId' })
+ registrationhook.belongsToMany(models.group, { as: 'groups', through: RegistrationhookXGroup, foreignKey: 'registrationhookId', otherKey: 'groupId' })
}
return registrationhook
} \ No newline at end of file