eventlog: add params for from and to date
This commit is contained in:
@@ -76,28 +76,38 @@ async function getActivationEvent() {
|
||||
return postProcess(result[0]);
|
||||
}
|
||||
|
||||
async function listPaged(actions, search, page, perPage) {
|
||||
async function listPaged(filter, page, perPage) {
|
||||
const { actions, search, from = null, to = null } = filter;
|
||||
|
||||
assert(Array.isArray(actions));
|
||||
assert(typeof search === 'string' || search === null);
|
||||
assert(from === null || from instanceof Date, 'from must be a Date or null');
|
||||
assert(to === null || to instanceof Date, 'to must be a Date or null');
|
||||
assert.strictEqual(typeof page, 'number');
|
||||
assert.strictEqual(typeof perPage, 'number');
|
||||
|
||||
const data = [];
|
||||
const conditions = [], data = [];
|
||||
|
||||
if (search) conditions.push('(sourceJson LIKE ' + mysql.escape('%' + search + '%') + ' OR dataJson LIKE ' + mysql.escape('%' + search + '%') + ')');
|
||||
|
||||
if (actions.length) {
|
||||
const actionConds = actions.map((a) => ' (action LIKE ' + mysql.escape(`%${a}%`) + ') ').join(' OR ');
|
||||
conditions.push('( ' + actionConds + ' )');
|
||||
}
|
||||
if (from) {
|
||||
conditions.push('creationTime >= ?');
|
||||
data.push(from);
|
||||
}
|
||||
if (to) {
|
||||
conditions.push('creationTime <= ?');
|
||||
data.push(to);
|
||||
}
|
||||
|
||||
let query = `SELECT ${EVENTLOG_FIELDS} FROM eventlog`;
|
||||
|
||||
if (actions.length || search) query += ' WHERE';
|
||||
if (search) query += ' (sourceJson LIKE ' + mysql.escape('%' + search + '%') + ' OR dataJson LIKE ' + mysql.escape('%' + search + '%') + ')';
|
||||
|
||||
if (actions.length && search) query += ' AND ( ';
|
||||
actions.forEach(function (action, i) {
|
||||
query += ' (action LIKE ' + mysql.escape(`%${action}%`) + ') ';
|
||||
if (i < actions.length-1) query += ' OR ';
|
||||
});
|
||||
if (actions.length && search) query += ' ) ';
|
||||
|
||||
if (conditions.length) query += ' WHERE ' + conditions.join(' AND ');
|
||||
query += ' ORDER BY creationTime DESC LIMIT ?,?';
|
||||
|
||||
data.push((page-1)*perPage);
|
||||
data.push((page - 1) * perPage);
|
||||
data.push(perPage);
|
||||
|
||||
const results = await database.query(query, data);
|
||||
|
||||
Reference in New Issue
Block a user