Replace event remarks placeholders in API data.

Events being created or edited via the API now call
replace_placeholders() making it possible to use
shortcuts to enter some event-related information.

See #229 for details.
This commit is contained in:
D. Berge
2022-05-12 22:10:33 +02:00
parent 3ed8339aa3
commit d6b985fcd2
3 changed files with 27 additions and 3 deletions

View File

@@ -5,6 +5,8 @@ async function patch (projectId, eventId, payload, opts = {}) {
const p = payload; // Shorter
const client = await setSurvey(projectId);
try {
await transaction.begin(client);
// The order of attributes in an object is not defined, so
// in theory we could get a different order if we made separate
// calls to Object.keys() and Object.values(), unlikely as that
@@ -22,10 +24,32 @@ async function patch (projectId, eventId, payload, opts = {}) {
const values = [ eventId, ...v ];
await client.query(text, values);
// NOTE Horrible hack warning.
// If we try to do the UPDATE on event_log, as you normally would,
// we get a constraint violation on `event_log_full_validity_check`.
// We would normally set that constraint as DEFERRABLE, except that
// as of version 14, PostgreSQL does not support deferrable CHECK
// constraints:
// https://www.postgresql.org/docs/current/sql-createtable.html
// So the options that I can think of are either not to use a transaction
// or to apply this second update directly on the `event_log_full` table.
const text1 = `
UPDATE event_log_full
SET remarks = replace_placeholders(remarks, tstamp, sequence, point)
WHERE id = $1 AND validity @> current_timestamp;
`;
const values1 = [ eventId ];
await client.query(text1, values1);
await transaction.commit(client);
} catch (err) {
err.origin = __filename;
throw err;
} finally {
client.release();
client.release(); // implies ROLLBACK;
}
return;

View File

@@ -10,7 +10,7 @@ async function post (projectId, payload, opts = {}) {
const text = `
INSERT
INTO event_log (tstamp, sequence, point, remarks, labels)
VALUES ($1, $2, $3, $4, $5);
VALUES ($1, $2, $3, replace_placeholders($4, $1, $2, $3), $5);
`;
const values = [ p.tstamp, p.sequence, p.point, p.remarks, p.labels ];

View File

@@ -12,7 +12,7 @@ async function put (projectId, eventId, payload, opts = {}) {
tstamp = $1,
sequence = $2,
point = $3,
remarks = $4,
remarks = replace_placeholders($4, $1, $2, $3),
labels = $5
WHERE id = $6;
`;