mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:17:08 +00:00
Add library function returning event changes after given epoch
This commit is contained in:
61
lib/www/server/lib/db/event/changes.js
Normal file
61
lib/www/server/lib/db/event/changes.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
const { setSurvey } = require('../connection');
|
||||||
|
const { replaceMarkers } = require('../../utils');
|
||||||
|
|
||||||
|
function parseValidity (row) {
|
||||||
|
if (row.validity) {
|
||||||
|
const rx = /^(.)("([\d :.+-]+)")?,("([\d :.+-]+)")?([\]\)])$/;
|
||||||
|
const m = row.validity.match(rx);
|
||||||
|
row.validity = [ m[1], m[3], m[5], m[6] ];
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
function transform (row) {
|
||||||
|
if (row.validity[2]) {
|
||||||
|
return {
|
||||||
|
uid: row.uid,
|
||||||
|
id: row.id,
|
||||||
|
is_deleted: true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
row.is_deleted = false;
|
||||||
|
row.has_edits = row.id != row.uid;
|
||||||
|
row.modified_on = row.validity[1];
|
||||||
|
delete row.uid;
|
||||||
|
delete row.validity;
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unique (rows) {
|
||||||
|
const o = {};
|
||||||
|
rows.forEach(row => o[row.id] = row);
|
||||||
|
return Object.values(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the event change history from a given epoch (ts0),
|
||||||
|
* for all events.
|
||||||
|
*/
|
||||||
|
async function changes (projectId, ts0, opts = {}) {
|
||||||
|
|
||||||
|
if (!projectId || !ts0) {
|
||||||
|
throw {status: 400, message: "Invalid request" };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = await setSurvey(projectId);
|
||||||
|
|
||||||
|
const text = `
|
||||||
|
SELECT *
|
||||||
|
FROM event_log_changes($1);
|
||||||
|
`;
|
||||||
|
|
||||||
|
const res = await client.query(text, [ts0]);
|
||||||
|
client.release();
|
||||||
|
return opts.unique
|
||||||
|
? unique(res.rows.map(i => transform(replaceMarkers(parseValidity(i)))))
|
||||||
|
: res.rows.map(i => transform(replaceMarkers(parseValidity(i))));
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = changes;
|
||||||
@@ -5,5 +5,6 @@ module.exports = {
|
|||||||
post: require('./post'),
|
post: require('./post'),
|
||||||
put: require('./put'),
|
put: require('./put'),
|
||||||
patch: require('./patch'),
|
patch: require('./patch'),
|
||||||
del: require('./delete')
|
del: require('./delete'),
|
||||||
|
changes: require('./changes')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user