adding filter organization

This commit is contained in:
xfarrow 2024-03-20 10:48:46 +01:00
parent 6bd04adccb
commit 4f92f2dab5
2 changed files with 26 additions and 1 deletions

View File

@ -110,6 +110,17 @@ async function remove(organizationId, requester) {
return numberOfDeletedRows == 1;
}
/**
* Gets a list of Organizations given their prefix.
* E.g. "Can" --> "Canonical"
* @param {*} name
*/
async function filterByPrefix(name) {
return await knex('Organization')
.where('name', 'ilike', `${name}%`)
.select('name', 'location');
}
// Exporting a function
// means making a JavaScript function defined in one
// module available for use in another module.
@ -118,5 +129,6 @@ module.exports = {
createOrganization,
insert,
update,
remove
remove,
filterByPrefix
};

View File

@ -163,11 +163,24 @@ async function getOrganization(req, res) {
}
}
async function filterByPrefix(req, res) {
try {
const organizations = await Organization.filterByPrefix(req.body.name);
return res.status(200).json(organizations).send();
} catch (error) {
console.error(`Error in function ${getOrganization.name}: ${error}`);
return res.status(500).json({
error: 'Internal server error'
});
}
}
// Here we can not use the jwtUtils.verifyToken as the Router's middleware directly, as the latter
// will be mounted under /organizations, but there are other resources under /organizations
// that do not require the authorization, e.g. job offers
const routes = express.Router();
routes.get('/:id', organizationValidator.deleteOrGetOrganizationValidator, getOrganization);
routes.post('/filterByPrefix', filterByPrefix);
routes.post('/', jwtUtils.verifyToken, organizationValidator.createOrganizationValidator, createOrganization);
routes.patch('/:id', jwtUtils.verifyToken, organizationValidator.updateOrganizationValidator, updateOrganization);
routes.delete('/:id', jwtUtils.verifyToken, organizationValidator.deleteOrGetOrganizationValidator, deleteOrganization);