/* global __appdir */ var path = require('path') var Sequelize = require('sequelize') var db = require(path.join(__appdir, 'lib', 'sequelize')) var express = require('express') const { decorateApp } = require('@awaitjs/express') var router = decorateApp(express.Router()) const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse')) // ############################################################################ // ########################### GET requests ################################# router.getAsync('', async (req, res) => { const configs = await db.config.findAll({ include: [{ association: 'groups', attributes: [] }, { association: 'clients', attributes: [] }], attributes: { include: [ [Sequelize.fn('COUNT', Sequelize.col('groups.id')), 'groupCount'], [Sequelize.fn('COUNT', Sequelize.col('clients.id')), 'clientCount'] ] }, group: ['id'] }) res.status(200).send(configs) }) router.getAsync('/:id', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id }, include: ['groups', 'clients'] }) if (config) res.status(200).send(config) else HttpResponse.notFound(req.params.id).send(res) }) router.getAsync('/:id/entries', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id }, include: ['entries'], order: [[db.config.associations.entries, db.config.associations.entries.through, 'sortValue', 'ASC']] }) if (config) res.status(200).send(config.entries) else HttpResponse.notFound(req.params.id).send(res) }) router.getAsync('/:id/groups', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id }, include: ['groups'] }) if (config) res.status(200).send(config.groups) else HttpResponse.notFound(req.params.id).send(res) }) router.getAsync('/:id/clients', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id }, include: ['clients'] }) if (config) res.status(200).send(config.clients) else HttpResponse.notFound(req.params.id).send(res) }) // ############################################################################ // ########################## POST requests ################################# 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) const count = await db.config.destroy({ where: { id: req.body.ids } }) HttpResponse.successBatch('deleted', 'ipxe config', count).send(res) } else { let config let action = 'updated' if (req.params.id === undefined) { config = await db.config.create(req.body.data) action = 'created' } else if (req.params.id > 0) { config = await db.config.findOne({ where: { id: req.params.id } }) if (!config) return HttpResponse.notFound(req.params.id).send(res) else await config.update(req.body.data) } else { return HttpResponse.invalidId().send(res) } await config.setEntries([]) if (req.body.entries.length > 0) { const promises = [] req.body.entries.forEach((entry, index) => { promises.push(config.addEntry(entry.id, { through: { sortValue: index, customName: entry.customName, keyBind: entry.keyBind } })) }) await Promise.all(promises) } HttpResponse.success(action, 'ipxe entry', config.id).send(res) } }) // ############################################################################ // ########################### PUT requests ################################# router.putAsync('/:id/groups', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res) const config = await db.config.findOne({ where: { id: req.params.id } }) if (config) { await config.setGroups(req.body.ids) HttpResponse.success('set', 'clients of ipxe config', config.id).send(res) } else { HttpResponse.notFound(req.params.id).send(res) } }) router.putAsync('/:id/clients', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res) const config = await db.config.findOne({ where: { id: req.params.id } }) if (config) { await config.setClients(req.body.ids) HttpResponse.success('set', 'clients of ipxe config', config.id).send(res) } else { HttpResponse.notFound(req.params.id).send(res) } }) router.putAsync('/:id/default', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id } }) if (config) { await db.config.update({ isDefault: false }, { where: { isDefault: true } }) await config.update({ isDefault: true }) HttpResponse.success('set as default:', 'config', config.id).send(res) } else { HttpResponse.notFound(req.params.id).send(res) } }) // ############################################################################ // ########################## DELETE requests ############################### router.deleteAsync('/:id', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const count = await db.config.destroy({ where: { id: req.params.id } }) if (count) res.status(200).end() else HttpResponse.notFound(req.params.id).send(res) }) // ############################################################################ // ############################################################################ module.exports.router = router