APIs more standardized

This commit is contained in:
xfarrow 2024-03-11 12:28:59 +01:00
parent 4564ec84f0
commit 35e74b982f
3 changed files with 28 additions and 26 deletions

View File

@ -69,20 +69,25 @@ async function getPersonById(id) {
* Registers a Person by inserting in the database, in a transaction, * Registers a Person by inserting in the database, in a transaction,
* both in the "Person" and in the "ActivationLink" tables. * both in the "Person" and in the "ActivationLink" tables.
* @param {*} person A Person object * @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) { async function registerPerson(person, activationLink) {
// We need to insert either both in the "Person" table // We need to insert either both in the "Person" table
// and in the "ActivationLink" one, or in neither // and in the "ActivationLink" one, or in neither
await knex.transaction(async (tr) => { return await knex.transaction(async (tr) => {
const personIdResult = await tr('Person') const insertedPerson = await tr('Person')
.insert(person) .insert(person)
.returning('id'); .returning('*');
await tr('ActivationLink') if(activationLink != null){
await tr('ActivationLink')
.insert({ .insert({
person_id: personIdResult[0].id, person_id: insertedPerson[0].id,
identifier: activationLink identifier: activationLink
}); });
}
return insertedPerson[0];
}); });
} }

View File

@ -53,11 +53,10 @@ async function registerPerson(req, res) {
}); });
} }
let activationLink = ''; let activationCode = null;
let isEnabled = true; let isEnabled = true;
if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') { if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') {
// Generate activation link token activationCode = crypto.randomBytes(16).toString('hex');
activationLink = crypto.randomBytes(16).toString('hex');
isEnabled = false; isEnabled = false;
} }
@ -74,14 +73,16 @@ async function registerPerson(req, res) {
req.body.place_of_living, req.body.place_of_living,
req.body.about_me, req.body.about_me,
req.body.qualification); 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') { if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') {
mailUtils.sendConfirmationLink(req.body.email, activationLink); mailUtils.sendConfirmationLink(req.body.email, activationCode);
} }
return res.status(200).json({ res.set('Location', `/api/persons/${insertedPerson.id}/details`);
activationLink return res.status(201).json(insertedPerson);
});
} catch (error) { } catch (error) {
console.error(`Error in function ${registerPerson.name}: ${error}`); console.error(`Error in function ${registerPerson.name}: ${error}`);
res.status(500).json({ res.status(500).json({
@ -128,6 +129,8 @@ async function createTokenByEmailAndPassword(req, res) {
} }
/** /**
* GET Request
*
* Obtain a Person's details if the * Obtain a Person's details if the
* Person to retrieve is either myself or an * Person to retrieve is either myself or an
* enabled Person. * enabled Person.
@ -256,9 +259,7 @@ async function updatePerson(req, res) {
} }
await personModel.updatePerson(updatePerson, req.jwt.person_id); await personModel.updatePerson(updatePerson, req.jwt.person_id);
return res.status(200).json({ return res.status(204).send();
success: 'true'
});
} catch (error) { } catch (error) {
console.error(`Error in function ${updatePerson.name}: ${error}`); console.error(`Error in function ${updatePerson.name}: ${error}`);
return res.status(500).json({ 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 * Deletes a Person. An user can only delete
* themselves. * themselves.
@ -280,9 +281,7 @@ async function deletePerson(req, res) {
// TODO: Delete Organization if this user was its only administrator // TODO: Delete Organization if this user was its only administrator
try { try {
await personModel.deletePerson(req.jwt.person_id); await personModel.deletePerson(req.jwt.person_id);
return res.status(200).json({ return res.status(204).send();
success: true
});
} catch (error) { } catch (error) {
console.error(`Error in function ${deletePerson.name}: ${error}`); console.error(`Error in function ${deletePerson.name}: ${error}`);
return res.status(500).json({ return res.status(500).json({
@ -314,9 +313,7 @@ async function confirmActivation(req, res) {
}); });
} }
await personModel.confirmActivation(personId); await personModel.confirmActivation(personId);
return res.status(200).json({ return res.status(204).send();
success: true
});
} catch (error) { } catch (error) {
console.error(`Error in function ${confirmActivation.name}: ${error}`); console.error(`Error in function ${confirmActivation.name}: ${error}`);
return res.status(500).json({ return res.status(500).json({

View File

@ -67,12 +67,12 @@ const updatePersonValidator = [
const confirmActivationValidator = [ const confirmActivationValidator = [
check('code').trim().escape() check('code').trim().escape()
] ];
module.exports = { module.exports = {
registerValidator, registerValidator,
getTokenValidator, getTokenValidator,
validationResult, validationResult,
updatePersonValidator, updatePersonValidator,
confirmActivationValidator confirmActivationValidator,
}; };