new func names

This commit is contained in:
xfarrow 2024-03-17 21:56:28 +01:00
parent a9b9a3b092
commit 8543b0db52
5 changed files with 38 additions and 25 deletions

View File

@ -20,7 +20,7 @@ const knex = require('../utils/knex_config');
* @param {*} organizationId * @param {*} organizationId
* @returns true if administrator, false otherwise * @returns true if administrator, false otherwise
*/ */
async function isPersonOrganizationAdministrator(personId, organizationId) { async function isAdmin(personId, organizationId) {
const isPersonAdmin = await knex('OrganizationAdministrator') const isPersonAdmin = await knex('OrganizationAdministrator')
.where('id_person', personId) .where('id_person', personId)
.where('id_organization', organizationId) .where('id_organization', organizationId)
@ -36,10 +36,10 @@ async function isPersonOrganizationAdministrator(personId, organizationId) {
* @param {*} organizationId * @param {*} organizationId
* @param {*} requester Id of the person requesting the addition * @param {*} requester Id of the person requesting the addition
*/ */
async function addOrganizationAdministrator(personId, organizationId, requester) { async function insert(personId, organizationId, requester) {
const isRequesterAdmin = await isPersonOrganizationAdministrator(requester, organizationId); const isRequesterAdmin = await isAdmin(requester, organizationId);
const isPersonAdmin = await isPersonOrganizationAdministrator(personId, organizationId); const isPersonAdmin = await isAdmin(personId, organizationId); // avoid database exception
// Unexploitable TOCTOU // Unexploitable TOCTOU
if (isRequesterAdmin && !isPersonAdmin) { if (isRequesterAdmin && !isPersonAdmin) {
await knex('OrganizationAdministrator') await knex('OrganizationAdministrator')
@ -58,7 +58,7 @@ async function addOrganizationAdministrator(personId, organizationId, requester)
* @param {*} personId * @param {*} personId
* @param {*} organizationId * @param {*} organizationId
*/ */
async function removeOrganizationAdmin(personId, organizationId) { async function remove(personId, organizationId) {
const transaction = await knex.transaction(); const transaction = await knex.transaction();
// We lock the table to ensure that we won't have concurrency issues // We lock the table to ensure that we won't have concurrency issues
@ -88,7 +88,7 @@ async function removeOrganizationAdmin(personId, organizationId) {
} }
module.exports = { module.exports = {
isPersonOrganizationAdministrator, isOrganizationAdmin: isAdmin,
addOrganizationAdministrator, insert,
removeOrganizationAdmin remove
}; };

View File

@ -36,7 +36,7 @@ function createOrganization(name, location, description, isHiring) {
* @param {*} id * @param {*} id
* @returns the Organization * @returns the Organization
*/ */
async function getOrganizationById(id) { async function findById(id) {
const organization = await knex('Organization') const organization = await knex('Organization')
.where('id', id) .where('id', id)
.select('*') .select('*')
@ -50,7 +50,7 @@ async function getOrganizationById(id) {
* *
* @returns The inserted Organization * @returns The inserted Organization
*/ */
async function insertOrganization(organization, organizationAdministratorId) { async function insert(organization, organizationAdministratorId) {
return await knex.transaction(async (trx) => { return await knex.transaction(async (trx) => {
// We have to insert either both in Organization and in OrganizationAdministrator // We have to insert either both in Organization and in OrganizationAdministrator
// or in neither // or in neither
@ -76,7 +76,7 @@ async function insertOrganization(organization, organizationAdministratorId) {
* @param {*} requester * @param {*} requester
* @returns true if the row was updated, false otherwise * @returns true if the row was updated, false otherwise
*/ */
async function updateOrganization(organization, organizationId, requester) { async function update(organization, organizationId, requester) {
const numberOfUpdatedRows = await knex('Organization') const numberOfUpdatedRows = await knex('Organization')
.where('id', organizationId) .where('id', organizationId)
.whereExists(function () { .whereExists(function () {
@ -96,7 +96,7 @@ async function updateOrganization(organization, organizationId, requester) {
* @param {*} requester PersonId of the supposedly administrator * @param {*} requester PersonId of the supposedly administrator
* @returns true if the Organization was successfully deleted, false otherwise * @returns true if the Organization was successfully deleted, false otherwise
*/ */
async function deleteOrganization(organizationId, requester) { async function remove(organizationId, requester) {
const numberOfDeletedRows = await knex('Organization') const numberOfDeletedRows = await knex('Organization')
.where({ .where({
id: organizationId id: organizationId
@ -115,9 +115,9 @@ async function deleteOrganization(organizationId, requester) {
// means making a JavaScript function defined in one // means making a JavaScript function defined in one
// module available for use in another module. // module available for use in another module.
module.exports = { module.exports = {
getOrganizationById, findById,
createOrganization, createOrganization,
insertOrganization, insert,
updateOrganization, update,
deleteOrganization deleteOrganization: remove
}; };

View File

@ -11,6 +11,19 @@
IN THE SOFTWARE. IN THE SOFTWARE.
*/ */
/******************************************************************************
* WARNING
*
*
* Posts are now scheduled to be developed at a later stage in the development
* process, with the possibility that it may not be developed at all.
* I am unsure whether it is a good thing or it'll only be used to
* flood timelines with low-effort content, like other competing platforms.
*
*
*
******************************************************************************/
const knex = require('../utils/knex_config'); const knex = require('../utils/knex_config');
/** /**

View File

@ -11,7 +11,7 @@
IN THE SOFTWARE. IN THE SOFTWARE.
*/ */
const organizationAdminModel = require('../models/organization_admin_model'); const organizationAdmin = require('../models/organization_admin_model');
const express = require('express'); const express = require('express');
const jwtUtils = require('../utils/jwt_utils'); const jwtUtils = require('../utils/jwt_utils');
const organizationAdminValidator = require('../utils/validators/organization_admin_validator'); const organizationAdminValidator = require('../utils/validators/organization_admin_validator');
@ -32,7 +32,7 @@ async function addOrganizationAdmin(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const success = await organizationAdminModel.addOrganizationAdministrator(req.body.person_id, req.params.organizationId, req.jwt.person_id); const success = await organizationAdmin.insert(req.body.person_id, req.params.organizationId, req.jwt.person_id);
if (success) { if (success) {
return res.status(204).send(); return res.status(204).send();
} }
@ -64,7 +64,7 @@ async function removeOrganizationAdmin(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const success = await organizationAdminModel.removeOrganizationAdmin(req.jwt.person_id, req.params.organizationId); const success = await organizationAdmin.remove(req.jwt.person_id, req.params.organizationId);
if(success){ if(success){
return res.status(204).send(); return res.status(204).send();
} }

View File

@ -11,7 +11,7 @@
IN THE SOFTWARE. IN THE SOFTWARE.
*/ */
const organizationModel = require('../models/organization_model'); const Organization = require('../models/organization_model');
const express = require('express'); const express = require('express');
const jwtUtils = require('../utils/jwt_utils'); const jwtUtils = require('../utils/jwt_utils');
const organizationValidator = require('../utils/validators/organization_validator'); const organizationValidator = require('../utils/validators/organization_validator');
@ -34,8 +34,8 @@ async function createOrganization(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const organization = organizationModel.createOrganization(req.body.name, req.body.location, req.body.description, req.body.is_hiring); const organization = Organization.createOrganization(req.body.name, req.body.location, req.body.description, req.body.is_hiring);
const insertedOrganization = await organizationModel.insertOrganization(organization, req.jwt.person_id); const insertedOrganization = await Organization.insert(organization, req.jwt.person_id);
res.set('Location', `/api/organizations/${insertedOrganization.id}`); res.set('Location', `/api/organizations/${insertedOrganization.id}`);
return res.status(201).json(insertedOrganization); return res.status(201).json(insertedOrganization);
} catch (error) { } catch (error) {
@ -85,7 +85,7 @@ async function updateOrganization(req, res) {
} }
try { try {
const isUpdateSuccessful = organizationModel.updateOrganization(updateOrganization, req.params.id, req.jwt.person_id); const isUpdateSuccessful = Organization.update(updateOrganization, req.params.id, req.jwt.person_id);
if (isUpdateSuccessful) { if (isUpdateSuccessful) {
return res.status(204).send(); return res.status(204).send();
} else { } else {
@ -115,7 +115,7 @@ async function deleteOrganization(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const isDeleteSuccessful = await organizationModel.deleteOrganization(req.params.id, req.jwt.person_id); const isDeleteSuccessful = await Organization.remove(req.params.id, req.jwt.person_id);
if (isDeleteSuccessful) { if (isDeleteSuccessful) {
return res.status(204).send(); return res.status(204).send();
} }
@ -147,7 +147,7 @@ async function getOrganization(req, res) {
errors: errors.array() errors: errors.array()
}); });
} }
const organization = await organizationModel.getOrganizationById(req.params.id); const organization = await Organization.findById(req.params.id);
if (organization) { if (organization) {
return res.status(200).json(organization); return res.status(200).json(organization);
} else { } else {