From b9545a1f80dbbde00bacc6c7e6e493cfed01ef62 Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:53:41 +0200 Subject: [PATCH] update --- backend/apis/nodejs/src/app.js | 2 + .../apis/nodejs/src/models/applicant_model.js | 39 +++++++++++++++ .../nodejs/src/routes/applicant_routes.js | 47 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 backend/apis/nodejs/src/models/applicant_model.js create mode 100644 backend/apis/nodejs/src/routes/applicant_routes.js diff --git a/backend/apis/nodejs/src/app.js b/backend/apis/nodejs/src/app.js index 3fc8107..22e6bd6 100644 --- a/backend/apis/nodejs/src/app.js +++ b/backend/apis/nodejs/src/app.js @@ -26,6 +26,7 @@ const organizationAdminRoutes = require('./routes/organization_admin_routes.js') const jobOffersRoutes = require('./routes/job_offer_routes.js'); const serverRoutes = require('./routes/server_routes.js'); const resetPasswordRoutes = require('./routes/reset_password_routes.js'); +const applicationRoutes = require('./routes/applicant_routes.js'); /* ===== END IMPORTING MODULES ===== @@ -55,6 +56,7 @@ app.use('/api/organizations', organizationRoutes.routes); app.use('/api/organizations', jobOffersRoutes.routes); app.use('/api/organizations', organizationAdminRoutes.routes); app.use('/api/resetpassword', resetPasswordRoutes.routes); +app.use('/api/applications', applicationRoutes.routes); /* ===== END ROUTE HANDLING ===== diff --git a/backend/apis/nodejs/src/models/applicant_model.js b/backend/apis/nodejs/src/models/applicant_model.js new file mode 100644 index 0000000..58546e7 --- /dev/null +++ b/backend/apis/nodejs/src/models/applicant_model.js @@ -0,0 +1,39 @@ +/* + 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 insert(personId, jobOfferId) { + return await knex('Applicant') + .insert({ + person_id: personId, + job_offer_id: jobOfferId + }) + .returning("*"); +} + +async function userAlreadyApplicated(personId, jobOfferId){ + const person = await knex('Applicant') + .where('person_id', personId) + .where('job_offer_id', jobOfferId) + .select('*') + .first(); + if(person){ + return true; + } + return false; +} + +module.exports = { + insert, + userAlreadyApplicated +} \ No newline at end of file diff --git a/backend/apis/nodejs/src/routes/applicant_routes.js b/backend/apis/nodejs/src/routes/applicant_routes.js new file mode 100644 index 0000000..b8f2bfe --- /dev/null +++ b/backend/apis/nodejs/src/routes/applicant_routes.js @@ -0,0 +1,47 @@ +/* + 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 Applicant = require('../models/applicant_model'); +const express = require('express'); +const jwtUtils = require('../utils/jwt_utils'); + +/** + * POST + * @param {*} req + * @param {*} res + * @returns + */ +async function insert(req, res) { + try { + if(await Applicant.userAlreadyApplicated(req.jwt.person_id, req.body.jobOfferId)){ + return res.status(401).json({ + error: 'User has already applied to this job' + }); + } + const application = await Applicant.insert(req.jwt.person_id, req.body.jobOfferId); + res.set('Location', `/api/applications/${application.id}`); + return res.status(201).json(application); + } catch (error) { + console.error(`Error in function ${registerPerson.name}: ${error}`); + res.status(500).json({ + error: 'Internal server error' + }); + } + } + + const routes = express.Router(); + routes.post('/', jwtUtils.extractToken, insert); + + module.exports = { + routes + }; \ No newline at end of file