-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschedules.js
More file actions
56 lines (50 loc) · 1.56 KB
/
schedules.js
File metadata and controls
56 lines (50 loc) · 1.56 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/schedules.js - CRUD for schedules
import express from 'express';
import { authenticateJWT } from '../auth/jwt.js';
import {
schedulesList,
scheduleCreate,
scheduleUpdate,
scheduleDelete
} from '../services/actualApi.js';
import { asyncHandler } from '../middleware/asyncHandler.js';
import { validateBody, validateParams } from '../middleware/validation-schemas.js';
import { IDSchema, CreateScheduleSchema, UpdateScheduleSchema } from '../middleware/validation-schemas.js';
import { standardWriteLimiter } from '../middleware/rateLimiters.js';
const router = express.Router();
router.use(authenticateJWT);
router.get('/', asyncHandler(async (req, res) => {
const schedules = await schedulesList();
res.json({ success: true, schedules });
}));
router.post(
'/',
standardWriteLimiter,
validateBody(CreateScheduleSchema),
asyncHandler(async (req, res) => {
const { schedule } = req.validatedBody;
const id = await scheduleCreate(schedule);
res.status(201).json({ success: true, id });
})
);
router.put(
'/:id',
standardWriteLimiter,
validateParams(IDSchema),
validateBody(UpdateScheduleSchema),
asyncHandler(async (req, res) => {
const { fields } = req.validatedBody;
const updated = await scheduleUpdate(req.validatedParams.id, fields);
res.json({ success: true, schedule: updated });
})
);
router.delete(
'/:id',
standardWriteLimiter,
validateParams(IDSchema),
asyncHandler(async (req, res) => {
await scheduleDelete(req.validatedParams.id);
res.json({ success: true });
})
);
export default router;