new func names

This commit is contained in:
xfarrow 2024-03-17 21:39:46 +01:00
parent 81dbae1d0a
commit a9b9a3b092
2 changed files with 26 additions and 26 deletions

View File

@ -45,7 +45,7 @@ function createPerson(email, password, displayName, dateOfBirth, available, enab
* @param {*} email email to look the Person for * @param {*} email email to look the Person for
* @returns the Person object * @returns the Person object
*/ */
async function getPersonByEmail(email) { async function findByEmail(email) {
return await knex('Person') return await knex('Person')
.where('email', email.toLowerCase()) .where('email', email.toLowerCase())
.first(); .first();
@ -56,7 +56,7 @@ async function getPersonByEmail(email) {
* @param {*} id - The id to look the person for * @param {*} id - The id to look the person for
* @returns * @returns
*/ */
async function getPersonById(id) { async function findById(id) {
return await knex('Person') return await knex('Person')
.select('*') .select('*')
.where({ .where({
@ -73,7 +73,7 @@ async function getPersonById(id) {
* *
* @returns The inserted person. * @returns The inserted person.
*/ */
async function registerPerson(person, activationLink) { async function insert(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
return await knex.transaction(async (tr) => { return await knex.transaction(async (tr) => {
@ -98,7 +98,7 @@ async function registerPerson(person, activationLink) {
* @param {*} password * @param {*} password
* @returns * @returns
*/ */
async function getPersonByEmailAndPassword(email, password) { async function authenticate(email, password) {
const person = await knex('Person') const person = await knex('Person')
.where('email', email.toLowerCase()) .where('email', email.toLowerCase())
.where('enabled', true) .where('enabled', true)
@ -119,7 +119,7 @@ async function getPersonByEmailAndPassword(email, password) {
* @param {*} person The Person to update * @param {*} person The Person to update
* @param {*} person_id The database id of the Person to update * @param {*} person_id The database id of the Person to update
*/ */
async function updatePerson(person, person_id) { async function update(person, person_id) {
await knex('Person') await knex('Person')
.where('id', person_id) .where('id', person_id)
.update(person); .update(person);
@ -129,7 +129,7 @@ async function updatePerson(person, person_id) {
* Deletes a Person specified by its database id. * Deletes a Person specified by its database id.
* @param {*} person_id * @param {*} person_id
*/ */
async function deletePerson(personId) { async function remove(personId) {
await knex('Person') await knex('Person')
.where({ .where({
id: personId id: personId
@ -157,11 +157,11 @@ async function confirmActivation(personId) {
// module available for use in another module. // module available for use in another module.
module.exports = { module.exports = {
createPerson, createPerson,
getPersonByEmail, findByEmail,
getPersonById, findById,
getPersonByEmailAndPassword, authenticate,
registerPerson, insert,
updatePerson, update,
deletePerson, remove,
confirmActivation confirmActivation
}; };

View File

@ -15,7 +15,7 @@ const personValidator = require('../utils/validators/person_validator');
const jwtUtils = require('../utils/jwt_utils'); const jwtUtils = require('../utils/jwt_utils');
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const crypto = require('crypto'); const crypto = require('crypto');
const personModel = require('../models/person_model'); const Person = require('../models/person_model');
const activationModel = require('../models/activation_model'); const activationModel = require('../models/activation_model');
const express = require('express'); const express = require('express');
const mailUtils = require('../utils/mail_utils'); const mailUtils = require('../utils/mail_utils');
@ -46,7 +46,7 @@ async function registerPerson(req, res) {
} }
// Check whether e-mail exists already (enforced by database constraints) // Check whether e-mail exists already (enforced by database constraints)
const existingUser = await personModel.getPersonByEmail(req.body.email); const existingUser = await Person.findByEmail(req.body.email);
if (existingUser) { if (existingUser) {
return res.status(409).json({ return res.status(409).json({
error: 'E-mail already in use' error: 'E-mail already in use'
@ -63,7 +63,7 @@ async function registerPerson(req, res) {
// Hash provided password // Hash provided password
const hashPasswordPromise = bcrypt.hash(req.body.password, 10); const hashPasswordPromise = bcrypt.hash(req.body.password, 10);
const personToInsert = personModel.createPerson( const personToInsert = Person.createPerson(
req.body.email, req.body.email,
await hashPasswordPromise, await hashPasswordPromise,
req.body.display_name, req.body.display_name,
@ -73,7 +73,7 @@ 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);
const insertedPerson = await personModel.registerPerson(personToInsert, activationCode); const insertedPerson = await Person.insert(personToInsert, activationCode);
delete insertedPerson.password; delete insertedPerson.password;
if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') { if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') {
@ -101,7 +101,7 @@ async function registerPerson(req, res) {
* *
* @returns The token * @returns The token
*/ */
async function createTokenByEmailAndPassword(req, res) { async function createToken(req, res) {
try { try {
const errors = personValidator.validationResult(req); const errors = personValidator.validationResult(req);
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
@ -109,7 +109,7 @@ async function createTokenByEmailAndPassword(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const person = await personModel.getPersonByEmailAndPassword(req.body.email, req.body.password); const person = await Person.authenticate(req.body.email, req.body.password);
if (person) { if (person) {
const token = jwtUtils.generateToken(person.id); const token = jwtUtils.generateToken(person.id);
return res.status(200).json({ return res.status(200).json({
@ -121,7 +121,7 @@ async function createTokenByEmailAndPassword(req, res) {
}); });
} }
} catch (error) { } catch (error) {
console.error(`Error in function ${createTokenByEmailAndPassword.name}: ${error}`); console.error(`Error in function ${createToken.name}: ${error}`);
return res.status(500).json({ return res.status(500).json({
error: 'Internal server error' error: 'Internal server error'
}); });
@ -141,7 +141,7 @@ async function createTokenByEmailAndPassword(req, res) {
*/ */
async function getPerson(req, res) { async function getPerson(req, res) {
try { try {
const person = await personModel.getPersonById(req.params.id); const person = await Person.findById(req.params.id);
if (person && person.enabled) { if (person && person.enabled) {
delete person.password; // remove password field for security reasons delete person.password; // remove password field for security reasons
return res.status(200).send(person); return res.status(200).send(person);
@ -167,7 +167,7 @@ async function getPerson(req, res) {
*/ */
async function getMyself(req, res) { async function getMyself(req, res) {
try { try {
const person = await personModel.getPersonById(req.jwt.person_id); const person = await Person.findById(req.jwt.person_id);
if (person) { if (person) {
delete person.password; delete person.password;
return res.status(200).send(person); return res.status(200).send(person);
@ -241,7 +241,7 @@ async function updatePerson(req, res) {
error: 'The new password must be specified' error: 'The new password must be specified'
}); });
} }
const user = await personModel.getPersonById(req.jwt.person_id); const user = await Person.getPersonById(req.jwt.person_id);
const passwordMatches = await bcrypt.compare(req.body.old_password, user.password); const passwordMatches = await bcrypt.compare(req.body.old_password, user.password);
if (passwordMatches) { if (passwordMatches) {
updatePerson.password = await bcrypt.hash(req.body.new_password, 10); updatePerson.password = await bcrypt.hash(req.body.new_password, 10);
@ -258,7 +258,7 @@ async function updatePerson(req, res) {
}); });
} }
await personModel.updatePerson(updatePerson, req.jwt.person_id); await Person.update(updatePerson, req.jwt.person_id);
return res.status(204).send(); return res.status(204).send();
} catch (error) { } catch (error) {
console.error(`Error in function ${updatePerson.name}: ${error}`); console.error(`Error in function ${updatePerson.name}: ${error}`);
@ -280,7 +280,7 @@ async function updatePerson(req, res) {
async function deletePerson(req, res) { 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 Person.remove(req.jwt.person_id);
return res.status(204).send(); return res.status(204).send();
} catch (error) { } catch (error) {
console.error(`Error in function ${deletePerson.name}: ${error}`); console.error(`Error in function ${deletePerson.name}: ${error}`);
@ -312,7 +312,7 @@ async function confirmActivation(req, res) {
error: 'Activation Link either not valid or expired' error: 'Activation Link either not valid or expired'
}); });
} }
await personModel.confirmActivation(personId); await Person.confirmActivation(personId);
return res.status(204).send(); return res.status(204).send();
} catch (error) { } catch (error) {
console.error(`Error in function ${confirmActivation.name}: ${error}`); console.error(`Error in function ${confirmActivation.name}: ${error}`);
@ -324,7 +324,7 @@ async function confirmActivation(req, res) {
const publicRoutes = express.Router(); // Routes not requiring token const publicRoutes = express.Router(); // Routes not requiring token
publicRoutes.post('/persons', personValidator.registerValidator, registerPerson); publicRoutes.post('/persons', personValidator.registerValidator, registerPerson);
publicRoutes.post('/persons/me/token', personValidator.getTokenValidator, createTokenByEmailAndPassword); publicRoutes.post('/persons/me/token', personValidator.getTokenValidator, createToken);
publicRoutes.get('/persons/:id/details', getPerson); publicRoutes.get('/persons/:id/details', getPerson);
publicRoutes.post('/persons/me/activation', personValidator.confirmActivationValidator, confirmActivation); publicRoutes.post('/persons/me/activation', personValidator.confirmActivationValidator, confirmActivation);