forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerts.js
More file actions
52 lines (46 loc) · 1.69 KB
/
Copy pathalerts.js
File metadata and controls
52 lines (46 loc) · 1.69 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
const express = require('express');
const { validate } = require('../middleware/validate');
const alertsService = require('../services/alerts');
const logger = require('../logger');
const AppError = require('../errors/AppError');
const { alertCreateBodySchema, paginationQuerySchema, routeIdParamsSchema } = require('../validation/schemas');
const router = express.Router();
const validateRouteIdParams = validate(routeIdParamsSchema, 'params');
const { parsePagination, paginateResponse } = require('../utils/paginate');
router.post('/alerts', validate(alertCreateBodySchema), async (req, res, next) => {
try {
const alert = await alertsService.create(req.validated.body);
return res.status(201).json(alert);
} catch (err) {
logger.error('Create alert error', { error: err.message });
return next(err);
}
});
router.get('/alerts', validate(paginationQuerySchema, 'query'), async (req, res, next) => {
try {
const pagination = parsePagination(req.query);
const result = await alertsService.listPaginated(pagination);
return res.json(
paginateResponse(
result.alerts,
result.total,
pagination
));
} catch (err) {
logger.error('List alerts error', { error: err.message });
return next(err);
}
});
router.delete('/alerts/:id', validateRouteIdParams, async (req, res, next) => {
try {
const deleted = await alertsService.remove(req.params.id);
if (!deleted) {
return next(new AppError('ALERT_NOT_FOUND', 'Alert not found', 404));
}
return res.json({ deleted: true, id: req.params.id });
} catch (err) {
logger.error('Delete alert error', { error: err.message });
return next(err);
}
});
module.exports = router;