From c9acaa6269de0f3fd181d685af6beebbf5020a86 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Thu, 16 Nov 2023 12:22:24 +0100 Subject: [PATCH] feat: update api job routes --- .../http/controllers/jobs/jobs.controller.ts | 49 +++++----- .../generateOpenapi.test.ts.snap | 90 +++++++++++-------- shared/routes/v1Jobs.routes.ts | 28 +++--- 3 files changed, 89 insertions(+), 78 deletions(-) diff --git a/server/src/http/controllers/jobs/jobs.controller.ts b/server/src/http/controllers/jobs/jobs.controller.ts index be37628767..c2b57adfda 100644 --- a/server/src/http/controllers/jobs/jobs.controller.ts +++ b/server/src/http/controllers/jobs/jobs.controller.ts @@ -203,18 +203,18 @@ export default (server: Server) => { }, async (req, res) => { const { jobId } = req.params - const jobExists = await getOffre(jobId.toString()) + const recruiter = await getOffre(jobId.toString()) - if (!jobExists) { - return res.status(400).send({ error: true, message: "Job does not exists" }) + if (!recruiter) { + throw Boom.badRequest("Job does not exists") } - if (!jobExists.geo_coordinates) { - throw Boom.internal("geo_coordinates is empty", { jobId: jobExists._id }) + if (!recruiter.geo_coordinates) { + throw Boom.internal("geo_coordinates is empty", { jobId: recruiter._id }) } - const [latitude = "", longitude = ""] = jobExists.geo_coordinates.split(",") - const { rome_code } = jobExists.jobs.filter(({ _id }) => _id == jobId)[0] + const [latitude = "", longitude = ""] = recruiter.geo_coordinates.split(",") + const { rome_code } = recruiter.jobs.filter(({ _id }) => _id.toString() === jobId.toString())[0] // Get related establishment from a job offer const etablissements = await getNearEtablissementsFromRomes({ @@ -225,6 +225,10 @@ export default (server: Server) => { }, }) + if (!etablissements.length) { + throw Boom.notFound("No delegations found") + } + const top10 = etablissements.slice(0, 10) return res.status(200).send(top10) @@ -242,13 +246,12 @@ export default (server: Server) => { const jobExists = await getOffre(jobId.toString()) if (!jobExists) { - return res.status(400).send({ error: true, message: "Job does not exists" }) + throw Boom.badRequest("Job does not exists") } const updatedRecruiter = await createJobDelegations({ jobId: jobId.toString(), etablissementCatalogueIds: req.body.establishmentIds }) - res.status(200) - return res.send(updatedRecruiter) + return res.status(200).send(updatedRecruiter) } ) @@ -264,16 +267,16 @@ export default (server: Server) => { const job = await getJob(jobId.toString()) if (!job) { - return res.status(400).send({ error: true, message: "Job does not exists" }) + throw Boom.badRequest("Job does not exists") } if (job.job_status === POURVUE) { - return res.status(400).send({ error: true, message: "Job is already provided" }) + throw Boom.badRequest("Job is already provided") } await provideOffre(jobId) - return res.status(204).send({}) + return res.status(204).send() } ) @@ -289,18 +292,16 @@ export default (server: Server) => { const job = await getJob(jobId.toString()) if (!job) { - res.status(400) - return res.send({ error: true, message: "Job does not exists" }) + throw Boom.badRequest("Job does not exists") } if (job.job_status === ANNULEE) { - res.status(400) - return res.send({ error: true, message: "Job is already canceled" }) + throw Boom.badRequest("Job is already canceled") } await cancelOffre(jobId) - return res.status(204).send({}) + return res.status(204).send() } ) @@ -316,24 +317,20 @@ export default (server: Server) => { const job = await getJob(jobId.toString()) if (!job) { - res.status(400) - return res.send({ error: true, message: "Job does not exists" }) + throw Boom.badRequest("Job does not exists") } if (addExpirationPeriod(dayjs()).isSame(dayjs(job.job_expiration_date), "day")) { - res.status(400) - return res.send({ error: true, message: "Job is already extended up to a month" }) + throw Boom.badRequest("Job is already extended up to a month") } if (job.job_status !== ACTIVE) { - res.status(400) - return res.send({ error: true, message: "Job cannot be extended as it is not enabled" }) + throw Boom.badRequest("Job cannot be extended as it is not active") } await extendOffre(jobId) - res.status(204) - return res.send({}) + return res.status(204).send() } ) server.get( diff --git a/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap b/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap index ce4b3719f3..4833c5cc1f 100644 --- a/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap +++ b/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap @@ -74,6 +74,7 @@ exports[`generateOpenApiSchema > should generate proper schema 1`] = ` "phone": { "description": "Le numéro de téléphone du contact de référence", "example": "0X XX XX XX XX", + "pattern": "^0[1-9]\\\\d{8}$", "type": [ "string", "null", @@ -3127,6 +3128,7 @@ L'email est envoyé depuis l'adresse générique \\"Ne pas répondre\\" de La bo "applicant_phone": { "description": "Le numéro de téléphone du candidat.", "example": "0101010101", + "pattern": "^0[1-9]\\\\d{8}$", "type": "string", }, "caller": { @@ -4338,47 +4340,56 @@ L'email est envoyé depuis l'adresse générique \\"Ne pas répondre\\" de La bo "content": { "application/json": { "schema": { - "additionalProperties": false, - "properties": { - "_id": { - "description": "Identifiant unique", - }, - "code_postal": { - "type": "string", - }, - "distance_en_km": { - "type": "number", - }, - "entreprise_raison_sociale": { - "type": "string", - }, - "geo_coordonnees": { - "type": "string", - }, - "nom_departement": { - "type": "string", - }, - "nom_voie": { - "type": "string", - }, - "numero_voie": { - "type": "string", - }, - "type_voie": { - "type": "string", + "items": { + "additionalProperties": false, + "properties": { + "_id": { + "description": "Identifiant unique", + }, + "code_postal": { + "type": "string", + }, + "distance_en_km": { + "type": "number", + }, + "entreprise_raison_sociale": { + "type": "string", + }, + "geo_coordonnees": { + "type": "string", + }, + "nom_departement": { + "type": "string", + }, + "nom_voie": { + "type": [ + "string", + "null", + ], + }, + "numero_voie": { + "type": [ + "string", + "null", + ], + }, + "type_voie": { + "type": [ + "string", + "null", + ], + }, }, + "required": [ + "code_postal", + "nom_departement", + "entreprise_raison_sociale", + "geo_coordonnees", + "distance_en_km", + ], + "type": "object", }, - "required": [ - "numero_voie", - "type_voie", - "nom_voie", - "code_postal", - "nom_departement", - "entreprise_raison_sociale", - "geo_coordonnees", - "distance_en_km", - ], - "type": "object", + "type": "array", }, }, }, @@ -4586,6 +4597,7 @@ L'email est envoyé depuis l'adresse générique \\"Ne pas répondre\\" de La bo "type": "string", }, "phone": { + "pattern": "^0[1-9]\\\\d{8}$", "type": "string", }, }, diff --git a/shared/routes/v1Jobs.routes.ts b/shared/routes/v1Jobs.routes.ts index e769ec1cea..bd1e26778d 100644 --- a/shared/routes/v1Jobs.routes.ts +++ b/shared/routes/v1Jobs.routes.ts @@ -135,19 +135,21 @@ export const zV1JobsRoutes = { }) .strict(), response: { - "200": z - .object({ - _id: zObjectId, - numero_voie: z.string(), - type_voie: z.string(), - nom_voie: z.string(), - code_postal: z.string(), - nom_departement: z.string(), - entreprise_raison_sociale: z.string(), - geo_coordonnees: z.string(), - distance_en_km: z.number(), - }) - .strict(), + "200": z.array( + z + .object({ + _id: zObjectId, + numero_voie: z.string().nullish(), + type_voie: z.string().nullish(), + nom_voie: z.string().nullish(), + code_postal: z.string(), + nom_departement: z.string(), + entreprise_raison_sociale: z.string(), + geo_coordonnees: z.string(), + distance_en_km: z.number(), + }) + .strict() + ), "4xx": z.union([ZLbarError, ZResError]), }, securityScheme: {