eventlog: add params for from and to date
This commit is contained in:
+16
-1
@@ -1,4 +1,5 @@
|
||||
import apps from '../apps.js';
|
||||
import validator from '../validator.js';
|
||||
import appstore from '../appstore.js';
|
||||
import assert from 'node:assert';
|
||||
import AuditSource from '../auditsource.js';
|
||||
@@ -993,7 +994,21 @@ async function listEventlog(req, res, next) {
|
||||
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'));
|
||||
|
||||
const [error, eventlogs] = await safe(apps.listEventlog(req.resources.app, page, perPage));
|
||||
if (req.query.from) {
|
||||
if (typeof req.query.from !== 'string') return next(new HttpError(400, 'from must be an ISO 8601 datetime string'));
|
||||
if (!validator.isIsoDate(req.query.from)) return next(new HttpError(400, 'from must be a valid ISO 8601 datetime string'));
|
||||
}
|
||||
|
||||
if (req.query.to) {
|
||||
if (typeof req.query.to !== 'string') return next(new HttpError(400, 'to must be an ISO 8601 datetime string'));
|
||||
if (!validator.isIsoDate(req.query.to)) return next(new HttpError(400, 'to must be a valid ISO 8601 datetime string'));
|
||||
}
|
||||
|
||||
const filter = {
|
||||
from: req.query.from ? new Date(req.query.from) : null,
|
||||
to: req.query.to ? new Date(req.query.to) : null
|
||||
};
|
||||
const [error, eventlogs] = await safe(apps.listEventlog(req.resources.app, filter, page, perPage));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { eventlogs }));
|
||||
|
||||
+21
-1
@@ -1,5 +1,6 @@
|
||||
import BoxError from '../boxerror.js';
|
||||
import eventlog from '../eventlog.js';
|
||||
import validator from '../validator.js';
|
||||
import { HttpError } from '@cloudron/connect-lastmile';
|
||||
import { HttpSuccess } from '@cloudron/connect-lastmile';
|
||||
import safe from 'safetydance';
|
||||
@@ -24,10 +25,29 @@ async function list(req, res, next) {
|
||||
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'));
|
||||
|
||||
if (req.query.from) {
|
||||
if (typeof req.query.from !== 'string') return next(new HttpError(400, 'from must be an ISO 8601 datetime string'));
|
||||
if (!validator.isIsoDate(req.query.from)) return next(new HttpError(400, 'from must be a valid ISO 8601 datetime string'));
|
||||
}
|
||||
|
||||
if (req.query.to) {
|
||||
if (typeof req.query.to !== 'string') return next(new HttpError(400, 'to must be an ISO 8601 datetime string'));
|
||||
if (!validator.isIsoDate(req.query.to)) return next(new HttpError(400, 'to must be a valid ISO 8601 datetime string'));
|
||||
}
|
||||
|
||||
const actions = typeof req.query.actions === 'string' ? req.query.actions.split(',').map(function (s) { return s.trim(); }) : [];
|
||||
if (req.query.action) actions.push(req.query.action);
|
||||
|
||||
const [error, eventlogs] = await safe(eventlog.listPaged(actions, req.query.search || null, page, perPage));
|
||||
if (req.query.to && !validator.isIsoDate(req.query.to)) return next(new HttpError(400, 'to must be a valid ISO 8601 datetime string'));
|
||||
|
||||
const filter = {
|
||||
actions,
|
||||
search: req.query.search || null,
|
||||
from: req.query.from ? new Date(req.query.from) : null,
|
||||
to: req.query.to ? new Date(req.query.to) : null
|
||||
};
|
||||
|
||||
const [error, eventlogs] = await safe(eventlog.listPaged(filter, page, perPage));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { eventlogs }));
|
||||
|
||||
@@ -118,5 +118,31 @@ describe('Eventlog API', function () {
|
||||
expect(response.status).to.equal(200);
|
||||
expect(response.body.eventlogs.length).to.equal(0);
|
||||
});
|
||||
|
||||
it('succeeds with from and to', async function () {
|
||||
const from = new Date(Date.now() - 86400000).toISOString(); // 1 day ago
|
||||
const to = new Date(Date.now() + 86400000).toISOString(); // 1 day from now
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/eventlog`)
|
||||
.query({ access_token: owner.token, page: 1, per_page: 10, from, to });
|
||||
|
||||
expect(response.status).to.equal(200);
|
||||
expect(response.body.eventlogs).to.be.an('array');
|
||||
});
|
||||
|
||||
it('fails with invalid from', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/eventlog`)
|
||||
.query({ access_token: owner.token, page: 1, per_page: 10, from: 'not-a-date' })
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.status).to.equal(400);
|
||||
});
|
||||
|
||||
it('fails with invalid to', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/eventlog`)
|
||||
.query({ access_token: owner.token, page: 1, per_page: 10, to: 'invalid' })
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.status).to.equal(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user