Implement PUT method for project configuration endpoint.

In short:

POST creates a new project
PUT overwrites a project configuration with a new one
PATCH merges the request body with the existing configuration
This commit is contained in:
D. Berge
2023-11-13 20:18:17 +01:00
parent 313e9687bd
commit efe64f0a8c
5 changed files with 77 additions and 2 deletions

View File

@@ -114,6 +114,7 @@ app.map({
'/project/:project/configuration': {
get: [ mw.project.configuration.get ], // Get project configuration
patch: [ mw.auth.access.admin, mw.project.configuration.patch ], // Modify project configuration
put: [ mw.auth.access.admin, mw.project.configuration.put ], // Overwrite configuration
},
/*

View File

@@ -2,7 +2,7 @@
module.exports = {
get: require('./get'),
// post: require('./post'),
// put: require('./put'),
put: require('./put'),
patch: require('./patch'),
// delete: require('./delete'),
};

View File

@@ -0,0 +1,16 @@
const { project } = require('../../../../lib/db');
module.exports = async function (req, res, next) {
try {
// TODO
// Implement If-Match header requirements
res.send(await project.configuration.put(req.params.project, req.body));
next();
} catch (err) {
next(err);
}
};

View File

@@ -2,7 +2,7 @@
module.exports = {
get: require('./get'),
// post: require('./post'),
// put: require('./put'),
put: require('./put'),
patch: require('./patch'),
// delete: require('./delete'),
};

View File

@@ -0,0 +1,58 @@
const { setSurvey } = require('../../connection');
const { deepMerge, removeNulls } = require('../../../utils');
const { modify } = require('../create');
async function put (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" };
}
console.log("PAYLOAD ID", payload.id, typeof payload);
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(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 = put;