36aa641cb9
also, set no-use-before-define in linter
61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
import assert from 'node:assert';
|
|
import BoxError from '../boxerror.js';
|
|
import { HttpError } from '@cloudron/connect-lastmile';
|
|
import { HttpSuccess } from '@cloudron/connect-lastmile';
|
|
import notifications from '../notifications.js';
|
|
import safe from 'safetydance';
|
|
|
|
|
|
async function load(req, res, next) {
|
|
assert.strictEqual(typeof req.params.notificationId, 'string');
|
|
|
|
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'));
|
|
|
|
req.resources.notification = result;
|
|
|
|
next();
|
|
}
|
|
|
|
function get(req, res, next) {
|
|
assert.strictEqual(typeof req.resources.notification, 'object');
|
|
|
|
next(new HttpSuccess(200, req.resources.notification));
|
|
}
|
|
|
|
async function list(req, res, next) {
|
|
const page = typeof req.query.page === 'string' ? parseInt(req.query.page) : 1;
|
|
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
|
|
|
|
const perPage = typeof req.query.per_page === 'string'? parseInt(req.query.per_page) : 25;
|
|
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
|
|
|
|
if (req.query.acknowledged && !(req.query.acknowledged === 'true' || req.query.acknowledged === 'false')) return next(new HttpError(400, 'acknowledged must be a true or false'));
|
|
|
|
const filters = {};
|
|
if (req.query.acknowledged) filters.acknowledged = req.query.acknowledged === 'true';
|
|
|
|
const [error, result] = await safe(notifications.list(filters, page, perPage));
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
next(new HttpSuccess(200, { notifications: result }));
|
|
}
|
|
|
|
async function update(req, res, next) {
|
|
assert.strictEqual(typeof req.resources.notification, 'object');
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
if (typeof req.body.acknowledged !== 'boolean') return next(new HttpError(400, 'acknowledged must be a boolean'));
|
|
|
|
const [error] = await safe(notifications.update(req.resources.notification, { acknowledged: req.body.acknowledged }));
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
next(new HttpSuccess(204, {}));
|
|
}
|
|
|
|
export default {
|
|
load,
|
|
get,
|
|
list,
|
|
update
|
|
};
|