summaryrefslogtreecommitdiffstats
path: root/server/api/events.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/events.js')
-rw-r--r--server/api/events.js99
1 files changed, 96 insertions, 3 deletions
diff --git a/server/api/events.js b/server/api/events.js
index 6ec37c4..7e330e5 100644
--- a/server/api/events.js
+++ b/server/api/events.js
@@ -8,6 +8,8 @@ var router = decorateApp(express.Router())
const zmq = require('zeromq')
const socket = zmq.socket('push')
socket.connect('ipc:///tmp/bas_zeromq_events')
+const log = require(path.join(__appdir, 'lib', 'log'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -47,7 +49,65 @@ router.postAsync('/blacklist', async (req, res) => {
// Create, Update or Delete POST
router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
- await db.event.destroy({ where: { id: req.params.id || req.body.ids } }).then(count => { res.send({ count }) })
+ const user = await db.user.findOne({ where: { id: req.user.id } })
+
+ // Only need to log batch request if there is more than one event to delete.
+ if (req.body.ids.length > 1) {
+ await log({
+ category: 'EVENT_BATCH_DELETE',
+ description: 'Event batch deletion of ' + req.body.ids.length + ' events initiated by user.',
+ user,
+ userId: req.user.id
+ })
+ }
+
+ let deletionCounter = 0
+ // Delete every event on its own, to get a better log
+ for (let index in req.body.ids) {
+ const event = await db.event.findOne({ where: { id: req.body.ids[index] } })
+ const count = await db.event.destroy({ where: { id: req.body.ids[index] } })
+ if (count !== 1) {
+ await log({
+ category: 'ERROR_EVENT_DELETE',
+ description: '[' + event.id + '] ' + event.name + ': Event could not be deleted.\n' +
+ 'ID: ' + event.id + '\n' +
+ 'Name: ' + event.name + '\n' +
+ 'Description: ' + event.description + '\n' +
+ 'Times: ' + event.times + '\n' +
+ 'Important: ' + event.important + '\n' +
+ 'Wake-on-Lan: ' + event.wakeonlan + '\n' +
+ 'Config ID: ' + event.configId,
+ user,
+ userId: req.user.id
+ })
+ } else {
+ await log({
+ category: 'EVENT_DELETE',
+ description: '[' + event.id + '] ' + event.name + ': Event successfully deleted.\n' +
+ 'ID: ' + event.id + '\n' +
+ 'Name: ' + event.name + '\n' +
+ 'Description: ' + event.description + '\n' +
+ 'Times: ' + event.times + '\n' +
+ 'Important: ' + event.important + '\n' +
+ 'Wake-on-Lan: ' + event.wakeonlan + '\n' +
+ 'Config ID: ' + event.configId,
+ user,
+ userId: req.user.id
+ })
+ deletionCounter++
+ }
+ }
+ if (req.body.ids.length > 1) {
+ log({
+ category: 'EVENT_BATCH_DELETE',
+ description: deletionCounter + '/' + req.body.ids.length + ' events successfully deleted.',
+ user,
+ userId: req.user.id
+ })
+ }
+ HttpResponse.successBatch('deleted', 'event', deletionCounter).send(res)
+
+ await db.event.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
req.body.ids.forEach(id => {
socket.send(id)
})
@@ -58,7 +118,7 @@ router.postAsync(['', '/:id'], async (req, res) => {
if (req.body.config.length !== 1) req.body.config = null
if (req.body.times.length === 0) req.body.times = null
if (req.params.id > 0) {
- // Update existing role
+ // Update existing event
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, times: req.body.times, important: req.body.important, wakeonlan: req.body.wakeonlan, configId: req.body.config || null }))
@@ -68,13 +128,30 @@ router.postAsync(['', '/:id'], async (req, res) => {
promisesBlacklist.push(eventDb.addGroups(req.body.blacklistGroups, { through: { blacklist: 1 } }))
promisesBlacklist.push(eventDb.addClients(req.body.blacklistClients, { through: { blacklist: 1 } }))
await Promise.all(promisesBlacklist)
+ log({
+ category: 'EVENT_EDIT',
+ description: '[' + eventDb.id + '] ' + eventDb.name + ': Event successfully edited.\n' +
+ 'ID: ' + eventDb.id + '\n' +
+ 'Name: ' + eventDb.name + '\n' +
+ 'Description: ' + eventDb.description + '\n' +
+ 'Times: ' + eventDb.times + '\n' +
+ 'Important: ' + eventDb.important + '\n' +
+ 'Wake-on-Lan: ' + eventDb.wakeonlan + '\n' +
+ 'Config ID: ' + eventDb.configId + '\n' +
+ 'Groups: ' + req.body.groups + '\n' +
+ 'Clients: ' + req.body.clients + '\n' +
+ 'Blacklist-Groups: ' + req.body.blacklistGroups + '\n' +
+ 'Blacklist-Clients: ' + req.body.blacklistClients,
+ userId: req.user.id
+ })
+
socket.send(eventDb.id)
res.send({ id: req.params.id })
} else {
res.status(404).end()
}
} else if (req.params.id === undefined) {
- // Create new role
+ // Create new event
eventDb = await db.event.create({ name: req.body.name, description: req.body.description, times: req.body.times, important: req.body.important, wakeonlan: req.body.wakeonlan, 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 } }))
@@ -82,6 +159,22 @@ router.postAsync(['', '/:id'], async (req, res) => {
promisesBlacklist.push(eventDb.addGroups(req.body.blacklistGroups, { through: { blacklist: 1 } }))
promisesBlacklist.push(eventDb.addClients(req.body.blacklistClients, { through: { blacklist: 1 } }))
await Promise.all(promisesBlacklist)
+ log({
+ category: 'EVENT_CREATE',
+ description: '[' + eventDb.id + '] ' + eventDb.name + ': Event successfully created.\n' +
+ 'ID: ' + eventDb.id + '\n' +
+ 'Name: ' + eventDb.name + '\n' +
+ 'Description: ' + eventDb.description + '\n' +
+ 'Times: ' + eventDb.times + '\n' +
+ 'Important: ' + eventDb.important + '\n' +
+ 'Wake-on-Lan: ' + eventDb.wakeonlan + '\n' +
+ 'Config ID: ' + eventDb.configId + '\n' +
+ 'Groups: ' + req.body.groups + '\n' +
+ 'Clients: ' + req.body.clients + '\n' +
+ 'Blacklist-Groups: ' + req.body.blacklistGroups + '\n' +
+ 'Blacklist-Clients: ' + req.body.blacklistClients,
+ userId: req.user.id
+ })
socket.send(eventDb.id)
res.send({ id: req.body.id })
}