From 3f2d126dba95ace251cf49f952ffa0ada6729f65 Mon Sep 17 00:00:00 2001 From: Christian Hofmaier Date: Sun, 14 Apr 2019 16:20:31 +0000 Subject: [eventmanager] show config name instead of config id in event table - change models/migrations for config association --- server/api/events.js | 11 +++++++---- server/migrations/20190402032400-add-configid-event.js | 4 ++-- server/models/config.js | 1 + server/models/event.js | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-) (limited to 'server') diff --git a/server/api/events.js b/server/api/events.js index 23aeae7..5affbb0 100644 --- a/server/api/events.js +++ b/server/api/events.js @@ -13,7 +13,7 @@ var router = decorateApp(express.Router()) * @return: Returns event of given id. */ router.getAsync('/:id', async (req, res) => { - var event = await db.event.findOne({ where: { id: req.params.id }, include: ['clients', 'groups'] }) + var event = await db.event.findOne({ where: { id: req.params.id }, include: ['config', 'clients', 'groups'] }) if (event) res.send(event) else res.status(404).end() }) @@ -22,7 +22,7 @@ router.getAsync('/:id', async (req, res) => { * @return: Returns a list of all events in the database. */ router.getAsync('', async (req, res) => { - var events = await db.event.findAll({ include: ['clients', 'groups'] }) + var events = await db.event.findAll({ include: ['config', 'clients', 'groups'] }) if (events) res.status(200).send(events) else res.status(404).end() }) @@ -30,6 +30,9 @@ router.getAsync('', async (req, res) => { // ############################################################################ // ########################## POST requests ################################# +/* + * @return: Returns a list of all childs of the given groups + */ router.postAsync('/blacklist', async (req, res) => { if (req.body.groups) { var blacklist = await groupUtil.getAllChildren(req.body.groups) @@ -51,7 +54,7 @@ router.postAsync(['', '/:id'], async (req, res) => { // Update existing role eventDb = await db.event.findOne({ where: { id: req.params.id } }) if (eventDb !== null) { - promises.push(eventDb.update({ name: req.body.name, description: req.body.description, config: req.body.config || null, times: req.body.times, important: req.body.important })) + promises.push(eventDb.update({ name: req.body.name, description: req.body.description, times: req.body.times, important: req.body.important, configId: req.body.config || null })) promises.push(eventDb.setGroups(req.body.groups, { through: { blacklist: 0 } })) promises.push(eventDb.setClients(req.body.clients, { through: { blacklist: 0 } })) await Promise.all(promises) @@ -64,7 +67,7 @@ router.postAsync(['', '/:id'], async (req, res) => { } } else { // Create new role - eventDb = await db.event.create({ name: req.body.name, description: req.body.description, config: req.body.config || null, times: req.body.times, important: req.body.important }) + eventDb = await db.event.create({ name: req.body.name, description: req.body.description, times: req.body.times, important: req.body.important, configId: req.body.config || null }) promises.push(eventDb.setGroups(req.body.groups, { through: { blacklist: 0 } })) promises.push(eventDb.setClients(req.body.clients, { through: { blacklist: 0 } })) await Promise.all(promises) diff --git a/server/migrations/20190402032400-add-configid-event.js b/server/migrations/20190402032400-add-configid-event.js index 34e6ac9..fa44b52 100644 --- a/server/migrations/20190402032400-add-configid-event.js +++ b/server/migrations/20190402032400-add-configid-event.js @@ -1,7 +1,7 @@ 'use strict' module.exports = { up: (queryInterface, Sequelize) => { - return queryInterface.addColumn('events', 'config', { + return queryInterface.addColumn('events', 'configId', { type: Sequelize.INTEGER, onDelete: 'SET NULL', references: { @@ -11,6 +11,6 @@ module.exports = { }) }, down: (queryInterface, Sequelize) => { - return queryInterface.removeColumn('events', 'config') + return queryInterface.removeColumn('events', 'configId') } } diff --git a/server/models/config.js b/server/models/config.js index ae728f6..84f3d1b 100644 --- a/server/models/config.js +++ b/server/models/config.js @@ -24,6 +24,7 @@ module.exports = (sequelize, DataTypes) => { config.belongsToMany(models.entry, { as: 'entries', through: ConfigXEntry, foreignKey: 'configId', otherKey: 'entryId' }) config.hasMany(models.group, { as: 'groups' }) config.hasMany(models.client, { as: 'clients' }) + config.hasMany(models.event, { as: 'events'}) } return config } diff --git a/server/models/event.js b/server/models/event.js index 11a35e7..00f72c7 100644 --- a/server/models/event.js +++ b/server/models/event.js @@ -9,7 +9,6 @@ module.exports = (sequelize, DataTypes) => { }, name: DataTypes.STRING, description: DataTypes.STRING(2048), - config: DataTypes.INTEGER, times: DataTypes.STRING(4096), important: DataTypes.BOOLEAN }, { @@ -21,6 +20,8 @@ module.exports = (sequelize, DataTypes) => { var GroupXEvent = sequelize.define('group_x_event', { blacklist: DataTypes.BOOLEAN }, { timestamps: false, freezeTableName: true }) event.belongsToMany(models.group, { as: 'groups', through: GroupXEvent, foreignKey: 'eventId', otherKey: 'groupId' }) + + event.belongsTo(models.config, { as: 'config' }) } return event } -- cgit v1.2.3-55-g7522