summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorUdo Walter2018-11-12 05:59:16 +0100
committerUdo Walter2018-11-12 05:59:16 +0100
commit5a15373dcd76ed7371dbbee88104dd0c4384a889 (patch)
tree1d2c798f7602e17c007d67d279a7cb1ee754b053 /server
parent[iDoIT] Rework the import method to use batch requests (diff)
downloadbas-5a15373dcd76ed7371dbbee88104dd0c4384a889.tar.gz
bas-5a15373dcd76ed7371dbbee88104dd0c4384a889.tar.xz
bas-5a15373dcd76ed7371dbbee88104dd0c4384a889.zip
[registration] add configurator for registration hooks
Diffstat (limited to 'server')
-rw-r--r--server/.sequelizerc5
-rw-r--r--server/api/configurator.js2
-rw-r--r--server/api/registrations.js59
-rw-r--r--server/migrations/20181008151633-create-registrationhook.js7
-rw-r--r--server/models/registrationhook.js8
5 files changed, 79 insertions, 2 deletions
diff --git a/server/.sequelizerc b/server/.sequelizerc
new file mode 100644
index 0000000..3a02529
--- /dev/null
+++ b/server/.sequelizerc
@@ -0,0 +1,5 @@
+const path = require('path');
+
+module.exports = {
+ 'config': path.resolve('config', 'database.json')
+} \ No newline at end of file
diff --git a/server/api/configurator.js b/server/api/configurator.js
index 3c5bab8..250471b 100644
--- a/server/api/configurator.js
+++ b/server/api/configurator.js
@@ -60,7 +60,7 @@ router.post(['/entries', '/entries/:id'], async (req, res) => {
entry = await db.entry.findOne({ where: { id: req.params.id } })
if (entry) await entry.update(item)
} else {
- await db.entry.create(item)
+ entry = await db.entry.create(item)
}
if (entry) {
res.send({ id: entry.id })
diff --git a/server/api/registrations.js b/server/api/registrations.js
index eb9a7a0..928ee94 100644
--- a/server/api/registrations.js
+++ b/server/api/registrations.js
@@ -21,6 +21,65 @@ router.get('/', (req, res) => {
})
})
+/*
+ * Returns all registration hooks sorted by sortValue.
+ *
+ * @return: List of registration hooks
+ */
+router.get('/hooks', (req, res) => {
+ db.registrationhook.findAll({ order: [ ['sortValue', 'ASC'] ], include: [{ model: db.group, as: 'groups', attributes: ['id'] }] }).then(hooks => {
+ res.send(hooks)
+ })
+})
+
+// POST requests.
+
+/*
+ * Reorders the registration hooks based on an array of hook ids.
+ */
+router.post('/hookorder', async (req, res) => {
+ var idSortvalueMap = {}
+ req.body.ids.forEach((id, index) => {
+ idSortvalueMap[id] = index
+ })
+ var hooks = await db.registrationhook.findAll()
+ var promises = []
+ hooks.forEach(hook => {
+ promises.push(hook.update({ sortvalue: idSortvalueMap[hook.id] }))
+ })
+ await Promise.all(promises)
+ res.end()
+})
+
+router.post(['/hooks', '/hooks/:id'], async (req, res) => {
+ var item = {
+ name: req.body.name,
+ description: req.body.description,
+ type: req.body.type,
+ script: req.body.script
+ }
+ var hook = null
+ if (req.params.id > 0) {
+ hook = await db.registrationhook.findOne({ where: { id: req.params.id } })
+ if (hook) await hook.update(item)
+ } else {
+ var maxSortvalue = await db.registrationhook.max('sortvalue')
+ item.sortvalue = maxSortvalue ? maxSortvalue + 1 : 1
+ hook = await db.registrationhook.create(item)
+ }
+ if (hook) {
+ hook.setGroups(req.body.groups)
+ res.send({ id: hook.id })
+ }
+ res.end()
+})
+
+// DELETE requests.
+
+router.delete(['/hooks', '/hooks/:id'], (req, res) => {
+ db.registrationhook.destroy({ where: { id: req.params.id || req.body.ids } }).then(count => { res.send({ count }) })
+})
+
module.exports.router = router
// GET requests.
diff --git a/server/migrations/20181008151633-create-registrationhook.js b/server/migrations/20181008151633-create-registrationhook.js
index 59692c5..5914188 100644
--- a/server/migrations/20181008151633-create-registrationhook.js
+++ b/server/migrations/20181008151633-create-registrationhook.js
@@ -4,9 +4,16 @@ module.exports = {
return queryInterface.createTable('registrationhooks', {
id: {
primaryKey: true,
+ autoIncrement: true,
allowNull: false,
type: Sequelize.INTEGER,
},
+ name: {
+ type: Sequelize.STRING
+ },
+ description: {
+ type: Sequelize.STRING(2048)
+ },
sortvalue: Sequelize.INTEGER,
type: Sequelize.STRING,
script: {
diff --git a/server/models/registrationhook.js b/server/models/registrationhook.js
index 1e74288..0d1ea6e 100644
--- a/server/models/registrationhook.js
+++ b/server/models/registrationhook.js
@@ -7,11 +7,17 @@ module.exports = (sequelize, DataTypes) => {
primaryKey: true,
type: DataTypes.INTEGER
},
+ name: DataTypes.STRING,
+ description: DataTypes.STRING(2048),
sortvalue: DataTypes.INTEGER,
type: DataTypes.STRING,
script: {
allowNull: true,
- type: DataTypes.BLOB
+ type: DataTypes.BLOB,
+ get() {
+ var blob = this.getDataValue('script')
+ return blob ? blob.toString() : '';
+ }
}
}, {
timestamps: false