Files
cloudron-box/src/routes/eventlog.js

41 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-04-30 14:08:44 -07:00
'use strict';
exports = module.exports = {
2021-05-01 11:21:09 -07:00
get,
list
2016-04-30 14:08:44 -07:00
};
2021-06-01 09:35:20 -07:00
const BoxError = require('../boxerror.js'),
2019-10-22 13:59:01 -07:00
eventlog = require('../eventlog.js'),
2016-04-30 14:08:44 -07:00
HttpError = require('connect-lastmile').HttpError,
2021-06-01 09:35:20 -07:00
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
2016-04-30 14:08:44 -07:00
2021-06-01 09:35:20 -07:00
async function get(req, res, next) {
const [error, event] = await safe(eventlog.get(req.params.eventId));
if (error) return next(BoxError.toHttpError(error));
if (!event) return next(new HttpError(404, 'Eventlog not found'));
2019-01-23 16:44:45 +01:00
2021-06-01 09:35:20 -07:00
next(new HttpSuccess(200, { event }));
2019-01-23 16:44:45 +01:00
}
2021-06-01 09:35:20 -07:00
async function list(req, res, next) {
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
2016-04-30 14:08:44 -07:00
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
2021-06-01 09:35:20 -07:00
const perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
2016-04-30 14:08:44 -07:00
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
if (req.query.actions && typeof req.query.actions !== 'string') return next(new HttpError(400, 'actions must be a comma separated string'));
if (req.query.action && typeof req.query.action !== 'string') return next(new HttpError(400, 'action must be a string'));
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
2016-04-30 14:08:44 -07:00
2021-06-01 09:35:20 -07:00
const actions = req.query.actions ? req.query.actions.split(',').map(function (s) { return s.trim(); }) : [];
if (req.query.action) actions.push(req.query.action);
2021-08-20 11:27:35 -07:00
const [error, eventlogs] = await safe(eventlog.listPaged(actions, req.query.search || null, page, perPage));
2021-06-01 09:35:20 -07:00
if (error) return next(BoxError.toHttpError(error));
2021-06-01 09:35:20 -07:00
next(new HttpSuccess(200, { eventlogs }));
2016-04-30 14:08:44 -07:00
}