summaryrefslogtreecommitdiffstats
path: root/server/api/backends.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/backends.js')
-rw-r--r--server/api/backends.js109
1 files changed, 104 insertions, 5 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 32c7cc7..b75634f 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -6,6 +6,8 @@ var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
var noAuthRouter = decorateApp(express.Router())
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const log = require(path.join(__appdir, 'lib', 'log'))
// GET requests.
@@ -384,8 +386,31 @@ router.postAsync('/:id/connection', async (req, res) => {
const b = new ExternalBackends()
const instance = b.getInstance(backend.type)
const response = await instance.checkConnection(backend.credentials)
- if (response.error) return res.status(500).send(response)
- else if (response) res.status(200).send()
+
+ if (response.error) {
+ console.log(response)
+ log({
+ category: 'ERROR_BACKEND_CHECKCONNECTION',
+ description: '[' + backend.id + '] ' + backend.name + ': Connection error. [' + response.error + ']\n' +
+ 'ID: ' + backend.id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type + '\n' +
+ 'Error: ' + response.error + '\n' +
+ 'Message: ' + response.message,
+ userId: req.user.id
+ })
+ return res.status(500).send(response)
+ } else if (response) {
+ log({
+ category: 'BACKEND_CHECKCONNECTION',
+ description: '[' + backend.id + '] ' + backend.name + ': Connection successfull.\n' +
+ 'ID: ' + backend.id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type,
+ userId: req.user.id
+ })
+ res.status(200).send()
+ }
})
// PUT requests
@@ -408,6 +433,14 @@ router.putAsync('/:id', async (req, res) => {
// Insert new backend in the db.
const credentialString = JSON.stringify(backend.credentials)
db.backend.create({ name: backend.name, type: backend.type, credentials: credentialString })
+ log({
+ category: 'BACKEND_CREATE',
+ description: '[' + id + '] ' + backend.name + ': Backend successfully created.\n' +
+ 'ID: ' + id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type,
+ userId: req.user.id
+ })
} else {
const backendDb = await db.backend.findOne({ where: { id: id } })
if (!backendDb) return res.status(404).send({ error: 'BACKEND_NOT_FOUND', message: 'No backend was found with the provided backend id.' })
@@ -426,6 +459,14 @@ router.putAsync('/:id', async (req, res) => {
// Update an existing backend in the db.
db.backend.update({ name: backend.name, type: backend.type, credentials: JSON.stringify(credentials) }, { where: { id: id } })
+ log({
+ category: 'BACKEND_EDIT',
+ description: '[' + id + '] ' + backend.name + ': Backend successfully edited.\n' +
+ 'ID: ' + id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type,
+ userId: req.user.id
+ })
}
res.status(200).send('success')
@@ -446,6 +487,16 @@ router.put('/:id/syncsettings', (req, res) => {
const sync = req.body.sync
db.backend.findOne({ where: { id: id } }).then(backend => {
db.backend.update({ groupTypes: groups, clientTypes: clients, sync: sync }, { where: { id: id } }).then(() => {
+ log({
+ category: 'BACKEND_SYNCSETTINGS_EDIT',
+ description: '[' + backend.id + '] ' + backend.name + ': Sync settings successfully edited.\n' +
+ 'ID: ' + backend.id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type + '\n' +
+ 'Groups: ' + req.body.groups + '\n' +
+ 'Clients: ' + req.body.clients,
+ userId: req.user.id
+ })
res.status(200).send()
})
})
@@ -458,10 +509,58 @@ router.put('/:id/syncsettings', (req, res) => {
* Deletes the backend to the given id.
*/
router.postAsync('/delete', async (req, res) => {
- const backendIds = req.body.id
+ req.body.ids = req.body.id
+ // await db.backend.destroy({ where: { id: backendIds } })
+ // res.status(200).send('success')
+
+ const user = await db.user.findOne({ where: { id: req.user.id } })
+ // Only need to log batch request if there is more than one backend to delete.
+ if (req.body.ids.length > 1) {
+ await log({
+ category: 'BACKEND_BATCH_DELETE',
+ description: 'Batch deletion of ' + req.body.ids.length + ' backends initiated by user.',
+ user,
+ userId: req.user.id
+ })
+ }
- await db.backend.destroy({ where: { id: backendIds } })
- res.status(200).send('success')
+ let deletionCounter = 0
+ // Delete every backend on its own, to get a better log
+ for (let index in req.body.ids) {
+ const backend = await db.backend.findOne({ where: { id: req.body.ids[index] } })
+ const count = await db.backend.destroy({ where: { id: req.body.ids[index] } })
+ if (count !== 1) {
+ await log({
+ category: 'ERROR_BACKEND_DELETE',
+ description: '[' + backend.id + '] ' + backend.name + ': Backend could not be deleted.\n' +
+ 'ID: ' + backend.id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type,
+ user,
+ userId: req.user.id
+ })
+ } else {
+ await log({
+ category: 'BACKEND_DELETE',
+ description: '[' + backend.id + '] ' + backend.name + ': Backend successfully deleted.\n' +
+ 'ID: ' + backend.id + '\n' +
+ 'Name: ' + backend.name + '\n' +
+ 'Type: ' + backend.type,
+ user,
+ userId: req.user.id
+ })
+ deletionCounter++
+ }
+ }
+ if (req.body.ids.length > 1) {
+ log({
+ category: 'BACKEND_BATCH_DELETE',
+ description: deletionCounter + '/' + req.body.ids.length + ' backends successfully deleted.',
+ user,
+ userId: req.user.id
+ })
+ }
+ HttpResponse.successBatch('deleted', 'backend', deletionCounter).send(res)
})
module.exports.router = router