From 35e74b982fd4636b81289c2d97efdb9525ea2f02 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Mon, 11 Mar 2024 12:28:59 +0100 Subject: [PATCH] APIs more standardized --- .../apis/nodejs/src/models/person_model.js | 17 ++++++---- .../apis/nodejs/src/routes/person_routes.js | 33 +++++++++---------- .../src/utils/validators/person_validator.js | 4 +-- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/backend/apis/nodejs/src/models/person_model.js b/backend/apis/nodejs/src/models/person_model.js index 3eb05ff..5c79ebb 100644 --- a/backend/apis/nodejs/src/models/person_model.js +++ b/backend/apis/nodejs/src/models/person_model.js @@ -69,20 +69,25 @@ async function getPersonById(id) { * Registers a Person by inserting in the database, in a transaction, * both in the "Person" and in the "ActivationLink" tables. * @param {*} person A Person object - * @param {*} activationLink the activationLink identifier + * @param {*} activationLink the activationLink identifier, if null, it won't be inserted + * + * @returns The inserted person. */ async function registerPerson(person, activationLink) { // We need to insert either both in the "Person" table // and in the "ActivationLink" one, or in neither - await knex.transaction(async (tr) => { - const personIdResult = await tr('Person') + return await knex.transaction(async (tr) => { + const insertedPerson = await tr('Person') .insert(person) - .returning('id'); - await tr('ActivationLink') + .returning('*'); + if(activationLink != null){ + await tr('ActivationLink') .insert({ - person_id: personIdResult[0].id, + person_id: insertedPerson[0].id, identifier: activationLink }); + } + return insertedPerson[0]; }); } diff --git a/backend/apis/nodejs/src/routes/person_routes.js b/backend/apis/nodejs/src/routes/person_routes.js index 4f0b618..24d5612 100644 --- a/backend/apis/nodejs/src/routes/person_routes.js +++ b/backend/apis/nodejs/src/routes/person_routes.js @@ -53,11 +53,10 @@ async function registerPerson(req, res) { }); } - let activationLink = ''; + let activationCode = null; let isEnabled = true; if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') { - // Generate activation link token - activationLink = crypto.randomBytes(16).toString('hex'); + activationCode = crypto.randomBytes(16).toString('hex'); isEnabled = false; } @@ -74,14 +73,16 @@ async function registerPerson(req, res) { req.body.place_of_living, req.body.about_me, req.body.qualification); - await personModel.registerPerson(personToInsert, activationLink); + const insertedPerson = await personModel.registerPerson(personToInsert, activationCode); + delete insertedPerson.password; + if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') { - mailUtils.sendConfirmationLink(req.body.email, activationLink); + mailUtils.sendConfirmationLink(req.body.email, activationCode); } - return res.status(200).json({ - activationLink - }); + res.set('Location', `/api/persons/${insertedPerson.id}/details`); + return res.status(201).json(insertedPerson); + } catch (error) { console.error(`Error in function ${registerPerson.name}: ${error}`); res.status(500).json({ @@ -128,6 +129,8 @@ async function createTokenByEmailAndPassword(req, res) { } /** + * GET Request + * * Obtain a Person's details if the * Person to retrieve is either myself or an * enabled Person. @@ -256,9 +259,7 @@ async function updatePerson(req, res) { } await personModel.updatePerson(updatePerson, req.jwt.person_id); - return res.status(200).json({ - success: 'true' - }); + return res.status(204).send(); } catch (error) { console.error(`Error in function ${updatePerson.name}: ${error}`); return res.status(500).json({ @@ -268,7 +269,7 @@ async function updatePerson(req, res) { } /** - * GET Request + * DELETE Request * * Deletes a Person. An user can only delete * themselves. @@ -280,9 +281,7 @@ async function deletePerson(req, res) { // TODO: Delete Organization if this user was its only administrator try { await personModel.deletePerson(req.jwt.person_id); - return res.status(200).json({ - success: true - }); + return res.status(204).send(); } catch (error) { console.error(`Error in function ${deletePerson.name}: ${error}`); return res.status(500).json({ @@ -314,9 +313,7 @@ async function confirmActivation(req, res) { }); } await personModel.confirmActivation(personId); - return res.status(200).json({ - success: true - }); + return res.status(204).send(); } catch (error) { console.error(`Error in function ${confirmActivation.name}: ${error}`); return res.status(500).json({ diff --git a/backend/apis/nodejs/src/utils/validators/person_validator.js b/backend/apis/nodejs/src/utils/validators/person_validator.js index 4aae487..41aae1b 100644 --- a/backend/apis/nodejs/src/utils/validators/person_validator.js +++ b/backend/apis/nodejs/src/utils/validators/person_validator.js @@ -67,12 +67,12 @@ const updatePersonValidator = [ const confirmActivationValidator = [ check('code').trim().escape() -] +]; module.exports = { registerValidator, getTokenValidator, validationResult, updatePersonValidator, - confirmActivationValidator + confirmActivationValidator, }; \ No newline at end of file