-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkeywords.js
executable file
·46 lines (38 loc) · 1.3 KB
/
keywords.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const express = require('express');
const router = express.Router({ mergeParams: true });
const { requireUser, isAuthorizedForAgency } = require('../lib/access-helpers');
const db = require('../db');
router.post('/', requireUser, async (req, res) => {
const result = await db.createKeyword({
search_term: req.body.searchTerm,
mode: '',
notes: req.body.notes,
agency_id: req.session.selectedAgency,
type: req.body.type,
});
res.json(result);
});
router.delete('/:keywordId', requireUser, async (req, res) => {
// Get agency of keyword to be deleted.
const { agency_id } = await db.getKeyword(req.params.keywordId);
// Is this admin user authorized for that agency?
const authorized = isAuthorizedForAgency(req.session.user, agency_id);
if (!authorized) {
res.sendStatus(403);
return;
}
const deleteCount = await db.deleteKeyword(req.params.keywordId);
if (deleteCount === 1) {
res.json({});
} else {
res.status(400).send('No such keyword');
}
});
router.get('/', requireUser, async (req, res) => {
const keywords = await db.getAgencyKeywords(req.session.selectedAgency);
keywords.forEach((kw) => {
delete kw.updated_at;
});
res.json(keywords);
});
module.exports = router;