Files
dougal-software/lib/www/server/lib/db/project/configuration/patch.js
2023-09-24 12:14:20 +02:00

58 lines
1.2 KiB
JavaScript

const { setSurvey } = require('../../connection');
const { deepMerge, removeNulls } = require('../../../utils');
const { modify } = require('../create');
async function patch (projectId, payload, opts = {}) {
let client;
try {
client = await setSurvey(); // Use public schema
const text = `
SELECT meta
FROM projects
WHERE pid = $1;
`;
const res = await client.query(text, [projectId]);
const source = res.rows[0].meta;
if (!source) {
throw { status: 404, message: "Not found" };
}
if (("id" in payload) && (projectId.toLowerCase() != payload.id.toLowerCase())) {
throw {
status: 422,
message: "Project ID cannot be changed in this Dougal version"
}
}
if (("name" in payload) && source.name && (source.name != payload.name)) {
throw {
status: 422,
message: "Project name cannot be changed in this Dougal version"
}
}
// We do not allow users to change the schema
delete payload.schema;
const dest = removeNulls(deepMerge(source, payload));
await modify(projectId, dest);
return dest;
} catch (err) {
if (err.code == "42P01") {
throw { status: 404, message: "Not found" };
} else {
throw err;
}
} finally {
client.release();
}
}
module.exports = patch;