Let patch regenerate line name.

If the user sends a patch to a line with
a `name` which is exactly `null` or the
empty string `""`, the server will regenerate
the name based on the defaults script.
This commit is contained in:
D. Berge
2020-10-09 15:02:31 +02:00
parent b7f65c4f78
commit aeae758744

View File

@@ -1,4 +1,5 @@
const { setSurvey, transaction } = require('../connection'); const { setSurvey, transaction } = require('../connection');
const { getLineName } = require('./lib');
async function patch (projectId, sequence, payload, opts = {}) { async function patch (projectId, sequence, payload, opts = {}) {
const client = await setSurvey(projectId); const client = await setSurvey(projectId);
@@ -25,6 +26,15 @@ async function patch (projectId, sequence, payload, opts = {}) {
await client.query(text, values); await client.query(text, values);
// Magic if the name is (strictly) null or empty, we generate a new one
if (p.name === null || p.name === "") {
const text = "SELECT * FROM planned_lines WHERE sequence = $1;";
const res = await client.query(text, [p.sequence||sequence]);
const row = res.rows[0];
const name = await getLineName(client, projectId, row);
await client.query("UPDATE planned_lines SET name = $2 WHERE sequence = $1", [p.sequence||sequence, name]);
}
transaction.commit(client); transaction.commit(client);
} catch (err) { } catch (err) {
transaction.rollback(client); transaction.rollback(client);