Files
cloudron-box/src/routes/notifications.js
T

62 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-12-17 16:37:19 +01:00
'use strict';
exports = module.exports = {
2021-05-28 14:34:18 -07:00
load,
get,
list,
2021-04-21 12:00:07 -07:00
update
2018-12-17 16:37:19 +01:00
};
2025-08-14 11:17:38 +05:30
const assert = require('node:assert'),
2019-10-22 12:59:26 -07:00
BoxError = require('../boxerror.js'),
2025-07-10 11:00:31 +02:00
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
2021-05-28 14:34:18 -07:00
notifications = require('../notifications.js'),
safe = require('safetydance');
2019-10-22 12:59:26 -07:00
2021-05-28 14:34:18 -07:00
async function load(req, res, next) {
assert.strictEqual(typeof req.params.notificationId, 'string');
2018-12-17 16:37:19 +01:00
2021-05-28 14:34:18 -07:00
const [error, result] = await safe(notifications.get(req.params.notificationId));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Notification not found'));
2018-12-17 16:37:19 +01:00
req.resources.notification = result;
2021-05-28 14:34:18 -07:00
next();
2018-12-17 16:37:19 +01:00
}
function get(req, res, next) {
assert.strictEqual(typeof req.resources.notification, 'object');
2018-12-17 16:37:19 +01:00
next(new HttpSuccess(200, req.resources.notification));
2018-12-17 16:37:19 +01:00
}
2021-05-28 14:34:18 -07:00
async function list(req, res, next) {
2025-07-13 13:14:32 +02:00
const page = typeof req.query.page === 'string' ? parseInt(req.query.page) : 1;
2018-12-17 16:37:19 +01:00
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
2025-07-13 13:14:32 +02:00
const perPage = typeof req.query.per_page === 'string'? parseInt(req.query.per_page) : 25;
2018-12-17 16:37:19 +01:00
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
2019-01-07 16:18:18 +01:00
if (req.query.acknowledged && !(req.query.acknowledged === 'true' || req.query.acknowledged === 'false')) return next(new HttpError(400, 'acknowledged must be a true or false'));
2018-12-17 16:37:19 +01:00
const filters = {};
2021-05-29 12:11:28 -07:00
if (req.query.acknowledged) filters.acknowledged = req.query.acknowledged === 'true';
2021-05-29 12:11:28 -07:00
const [error, result] = await safe(notifications.list(filters, page, perPage));
2021-05-28 14:34:18 -07:00
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { notifications: result }));
2018-12-17 16:37:19 +01:00
}
2021-05-28 14:34:18 -07:00
async function update(req, res, next) {
assert.strictEqual(typeof req.resources.notification, 'object');
2021-04-21 12:00:07 -07:00
assert.strictEqual(typeof req.body, 'object');
2018-12-17 16:37:19 +01:00
2022-06-13 13:55:04 -07:00
if (typeof req.body.acknowledged !== 'boolean') return next(new HttpError(400, 'acknowledged must be a boolean'));
2021-04-21 12:00:07 -07:00
const [error] = await safe(notifications.update(req.resources.notification, { acknowledged: req.body.acknowledged }));
2021-05-28 14:34:18 -07:00
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
2018-12-17 16:37:19 +01:00
}