summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/clients.js')
-rw-r--r--server/api/clients.js78
1 files changed, 72 insertions, 6 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index d72207b..15e3b8d 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -7,6 +7,7 @@ var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const log = require(path.join(__appdir, 'lib', 'log'))
// ############################################################################
// ########################### GET requests #################################
@@ -30,8 +31,51 @@ router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
await backendHelper.deleteClients(req.body.ids, req.body.backendIds)
- const count = await db.client.destroy({ where: { id: req.body.ids } })
- HttpResponse.successBatch('deleted', 'client', count).send(res)
+
+ const user = await db.user.findOne({ where: { id: req.user.id } })
+ // Only need to log batch request if there is more than one client to delete.
+ if (req.body.ids.length > 1) {
+ await log({
+ category: 'CLIENT_BATCH_DELETE',
+ description: 'Batch deletion of ' + req.body.ids.length + ' clients initiated by user.',
+ user,
+ userId: req.user.id
+ })
+ }
+
+ let deletionCounter = 0
+ // Delete every client on its own, to get a better log
+ for (let index in req.body.ids) {
+ const client = await db.client.findOne({ where: { id: req.body.ids[index] } })
+ const count = await db.client.destroy({ where: { id: req.body.ids[index] } })
+ if (count !== 1) {
+ await log({
+ category: 'ERROR_CLIENT_DELETE',
+ description: 'Client could not be deleted.',
+ client,
+ user,
+ userId: req.user.id
+ })
+ } else {
+ await log({
+ category: 'CLIENT_DELETE',
+ description: 'Client successfully deleted.',
+ client,
+ user,
+ userId: req.user.id
+ })
+ deletionCounter++
+ }
+ }
+ if (req.body.ids.length > 1) {
+ log({
+ category: 'CLIENT_BATCH_DELETE',
+ description: deletionCounter + '/' + req.body.ids.length + ' clients successfully deleted.',
+ user,
+ userId: req.user.id
+ })
+ }
+ HttpResponse.successBatch('deleted', 'client', deletionCounter).send(res)
} else {
let client
let action = 'updated'
@@ -51,6 +95,12 @@ router.postAsync(['', '/:id'], async (req, res) => {
client = await db.client.create(req.body.data)
action = 'created'
io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
+ log({
+ category: 'CLIENT_CREATE',
+ description: 'Client successfully created.',
+ clientId: client.id,
+ userId: req.user.id
+ })
// Add client to the backends
backendClient.id = client.id
@@ -58,7 +108,15 @@ router.postAsync(['', '/:id'], async (req, res) => {
} else if (req.params.id > 0) {
client = await db.client.findOne({ where: { id: req.params.id } })
if (!client) return HttpResponse.notFound(req.params.id).send(res)
- else await client.update(req.body.data)
+ else {
+ await client.update(req.body.data)
+ log({
+ category: 'CLIENT_EDIT',
+ description: 'Client successfully edited.',
+ clientId: client.id,
+ userId: req.user.id
+ })
+ }
backendHelper.updateClient(backendClient)
} else {
return HttpResponse.invalidId().send(res)
@@ -71,10 +129,18 @@ router.postAsync(['', '/:id'], async (req, res) => {
// ############################################################################
// ########################## DELETE requests ###############################
-router.delete('/:id', async (req, res) => {
+router.deleteAsync('/:id', async (req, res) => {
+ const client = db.client.findOne({ where: { id: req.params.id } })
const count = await db.client.destroy({ where: { id: req.params.id } })
- if (count) HttpResponse.success('deleted', 'client', req.params.id).send(res)
- else HttpResponse.notFound(req.params.id).send(res)
+ if (count) {
+ log({
+ category: 'CLIENT_DELETE',
+ description: 'Client successfully deleted.',
+ client,
+ userId: req.user.id
+ })
+ HttpResponse.success('deleted', 'client', req.params.id).send(res)
+ } else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################