This commit is contained in:
xfarrow 2024-03-18 16:10:24 +01:00
parent 8fdef6f510
commit 7ef8cd4a48
4 changed files with 90 additions and 7 deletions

View File

@ -15,7 +15,7 @@ const knex = require('../utils/knex_config');
const OrganizationAdmin = require('../models/organization_admin_model');
async function insert(requester, organizationId, title, description, requirements, salary, salaryFrequency, salaryCurrency, location, tags) {
const isAdmin = OrganizationAdmin.isAdmin(requester, organizationId);
const isAdmin = await OrganizationAdmin.isAdmin(requester, organizationId);
if (isAdmin) {
return await knex.transaction(async (tr) => {
const jobOffer = await tr('JobOffer').insert({
@ -32,10 +32,10 @@ async function insert(requester, organizationId, title, description, requirement
// Insert in the JobOfferTag table all the relevant tags.
if (tags.length !== 0) {
await Promise.all(tags.map(tagId =>
await Promise.all(tags.map(tag =>
tr('JobOfferTag').insert({
job_offer_id: jobOffer[0].id,
tag_id: tagId
tag_id: tag.id
})
));
}
@ -45,6 +45,26 @@ async function insert(requester, organizationId, title, description, requirement
return null;
}
async function remove(requester, jobOfferId) {
const jobOffer = await findById(jobOfferId);
const isAdmin = await OrganizationAdmin.isAdmin(requester, jobOffer.organization_id);
if (isAdmin) {
const deletedRows = await knex('JobOffer')
.where({
id: jobOfferId
}).del();
return deletedRows === 1;
} else {
return false;
}
}
async function findById(jobOfferId) {
return await knex('JobOffer').where({
id: jobOfferId
}).select().first();
}
// test
async function filter(title, description, requirements, salary, salaryOperator, salaryFrequency, location, tags) {
let query = knex('JobOffer');
@ -77,5 +97,6 @@ async function filter(title, description, requirements, salary, salaryOperator,
}
module.exports = {
insert
insert,
remove
}

View File

@ -119,5 +119,5 @@ module.exports = {
createOrganization,
insert,
update,
deleteOrganization: remove
remove
};

View File

@ -0,0 +1,26 @@
/*
This code is part of Blink
licensed under GPLv3
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
const knex = require('../utils/knex_config');
async function findByTags(tags) {
tags = tags.map(tag => tag.toLowerCase());
const result = await knex('Tag')
.whereIn('tag', tags)
.select();
return result;
}
module.exports = {
findByTags
}

View File

@ -14,9 +14,19 @@
const JobOffer = require('../models/job_offer_model');
const jwtUtils = require('../utils/jwt_utils');
const express = require('express');
const Tag = require('../models/tags_model');
/**
* POST Request
*
* Creates a new Job Offer
* @param {*} req
* @param {*} res
* @returns
*/
async function insert(req, res) {
try {
const tags = await Tag.findByTags(req.body.tags);
const insertedJobOffer = await JobOffer.insert(
req.jwt.person_id,
req.params.id, // organization id
@ -27,7 +37,7 @@ async function insert(req, res) {
req.body.salary_frequency,
req.body.salary_currency,
req.body.location,
req.body.tags);
tags);
if (insertedJobOffer) {
res.set('Location', `/api/joboffers/${insertedJobOffer.id}`);
@ -45,9 +55,35 @@ async function insert(req, res) {
}
}
const protectedRoutes = express.Router(); // Routes requiring token
/**
* DELETE Request
*
* Removes a Job Offer
* @param {*} req
* @param {*} res
*/
async function remove(req, res) {
try {
const result = await JobOffer.remove(req.jwt.person_id, req.params.jobOfferId);
if (result) {
return res.status(204).send();
} else {
return res.status(403).json({
error: 'Forbidden'
});
}
} catch (error) {
console.error(`Error in function ${insert.name}: ${error}`);
res.status(500).json({
error: 'Internal server error'
});
}
}
const protectedRoutes = express.Router();
protectedRoutes.use(jwtUtils.verifyToken);
protectedRoutes.post('/organizations/:id/joboffers', insert);
protectedRoutes.delete('/organizations/joboffers/:jobOfferId', remove);
module.exports = {
protectedRoutes