Updated A LOT
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
const express = require('express');
|
||||
const { all, audit, json, now, one, parseJson, run } = require('../db');
|
||||
const { requireRole } = require('../middleware/auth');
|
||||
const { requireCapability } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/templates', (req, res) => {
|
||||
const templates = all('SELECT * FROM asset_templates ORDER BY name').map((row) => ({
|
||||
const templates = all(
|
||||
`SELECT t.*, (SELECT COUNT(*) FROM assets a WHERE a.template_id = t.id) AS asset_count
|
||||
FROM asset_templates t ORDER BY t.name`
|
||||
).map((row) => ({
|
||||
...row,
|
||||
defaults: parseJson(row.defaults, {}),
|
||||
custom_fields: parseJson(row.custom_fields, [])
|
||||
@@ -13,7 +16,7 @@ router.get('/templates', (req, res) => {
|
||||
res.json({ templates });
|
||||
});
|
||||
|
||||
router.post('/templates', requireRole('admin', 'finance'), (req, res) => {
|
||||
router.post('/templates', requireCapability('config.manage'), (req, res) => {
|
||||
const created = now();
|
||||
const result = run(
|
||||
'INSERT INTO asset_templates (name, description, defaults, custom_fields, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
@@ -23,4 +26,34 @@ router.post('/templates', requireRole('admin', 'finance'), (req, res) => {
|
||||
res.status(201).json({ template: one('SELECT * FROM asset_templates WHERE id = ?', [result.lastInsertRowid]) });
|
||||
});
|
||||
|
||||
router.put('/templates/:id', requireCapability('config.manage'), (req, res) => {
|
||||
const before = one('SELECT * FROM asset_templates WHERE id = ?', [req.params.id]);
|
||||
if (!before) return res.status(404).json({ error: 'Template not found' });
|
||||
run(
|
||||
'UPDATE asset_templates SET name = ?, description = ?, defaults = ?, custom_fields = ?, updated_at = ? WHERE id = ?',
|
||||
[
|
||||
req.body.name ?? before.name,
|
||||
req.body.description ?? before.description,
|
||||
json(req.body.defaults || {}),
|
||||
json(req.body.custom_fields || []),
|
||||
now(),
|
||||
req.params.id
|
||||
]
|
||||
);
|
||||
audit(req.user.id, 'update', 'asset_template', req.params.id, before, req.body);
|
||||
return res.json({ template: one('SELECT * FROM asset_templates WHERE id = ?', [req.params.id]) });
|
||||
});
|
||||
|
||||
// Deleting a template unlinks it from any assets (their stored field values are kept)
|
||||
// and removes it from the picker for future assets.
|
||||
router.delete('/templates/:id', requireCapability('config.manage'), (req, res) => {
|
||||
const before = one('SELECT * FROM asset_templates WHERE id = ?', [req.params.id]);
|
||||
if (!before) return res.status(404).json({ error: 'Template not found' });
|
||||
const affected = one('SELECT COUNT(*) AS c FROM assets WHERE template_id = ?', [req.params.id]).c;
|
||||
run('UPDATE assets SET template_id = NULL, updated_at = ? WHERE template_id = ?', [now(), req.params.id]);
|
||||
run('DELETE FROM asset_templates WHERE id = ?', [req.params.id]);
|
||||
audit(req.user.id, 'delete', 'asset_template', req.params.id, before, { unlinked_assets: affected });
|
||||
return res.json({ deleted: true, unlinked_assets: affected });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user