-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcategory-groups.js
More file actions
56 lines (50 loc) · 1.61 KB
/
category-groups.js
File metadata and controls
56 lines (50 loc) · 1.61 KB
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
47
48
49
50
51
52
53
54
55
56
// src/routes/category-groups.js - CRUD for category groups
import express from 'express';
import { authenticateJWT } from '../auth/jwt.js';
import {
categoryGroupsList,
categoryGroupCreate,
categoryGroupUpdate,
categoryGroupDelete
} from '../services/actualApi.js';
import { asyncHandler } from '../middleware/asyncHandler.js';
import { validateBody, validateParams } from '../middleware/validation-schemas.js';
import { IDSchema, CreateCategoryGroupSchema, UpdateCategoryGroupSchema } from '../middleware/validation-schemas.js';
import { categoryGroupLimiter } from '../middleware/rateLimiters.js';
const router = express.Router();
router.use(authenticateJWT);
router.get('/', asyncHandler(async (req, res) => {
const groups = await categoryGroupsList();
res.json({ success: true, categoryGroups: groups });
}));
router.post(
'/',
categoryGroupLimiter,
validateBody(CreateCategoryGroupSchema),
asyncHandler(async (req, res) => {
const { group } = req.validatedBody;
const id = await categoryGroupCreate(group);
res.status(201).json({ success: true, id });
})
);
router.put(
'/:id',
categoryGroupLimiter,
validateParams(IDSchema),
validateBody(UpdateCategoryGroupSchema),
asyncHandler(async (req, res) => {
const { fields } = req.validatedBody;
const id = await categoryGroupUpdate(req.validatedParams.id, fields);
res.json({ success: true, id });
})
);
router.delete(
'/:id',
categoryGroupLimiter,
validateParams(IDSchema),
asyncHandler(async (req, res) => {
await categoryGroupDelete(req.validatedParams.id);
res.json({ success: true });
})
);
export default router;