Skip to content

Commit

Permalink
feat(lbac-2243): safe parse jobs before return (#1647)
Browse files Browse the repository at this point in the history
* feat: safe parse jobs before return

* fix: label
  • Loading branch information
kevbarns authored Nov 14, 2024
1 parent 0e25abe commit bde0c88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
// "**/node_modules": true,
"**/node_modules": true,
".next": true,
"**/*/lib": true,
// "**/*/dist": true,
"**/*/dist": true,
"**/*/build": true,
"**/.webpack": true
},
Expand All @@ -68,6 +68,7 @@
"**/.webpack": true
},
"search.exclude": {
"**/node_modules": true,
"**/.webpack": true,
"**/.yarn/**": true,
"**/*/.next": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { IRouteSchema } from "shared/routes/common.routes"
// Messages are not constants but the codes are
export const IJobOpportunityWarningMap = {
FRANCE_TRAVAIL_API_ERROR: "Unable to retrieve job offers from France Travail API",
JOB_OFFER_FORMATING_ERROR: "Some job offers are invalid and have been excluded due to unexpected errors.",
RECRUITERS_FORMATING_ERROR: "Some recruiters are invalid and have been excluded due to unexpected errors.",
} as const satisfies Record<string, string>

export type IJobOpportunityWarningCode = keyof typeof IJobOpportunityWarningMap
Expand All @@ -16,18 +18,18 @@ export class JobOpportunityRequestContext {

caller: string

#warnings: IJobOpportunityWarningCode[] = []
#warnings: Set<IJobOpportunityWarningCode> = new Set()

constructor(route: Pick<IRouteSchema, "path">, caller: string) {
this.route = route
this.caller = caller
}

addWarning(code: IJobOpportunityWarningCode) {
this.#warnings.push(code)
this.#warnings.add(code)
}

getWarnings(): Array<{ code: string; message: string }> {
return this.#warnings.map((code) => ({ code, message: IJobOpportunityWarningMap[code] }))
return Array.from(this.#warnings).map((code) => ({ code, message: IJobOpportunityWarningMap[code] }))
}
}
27 changes: 25 additions & 2 deletions server/src/services/jobs/jobOpportunity/jobOpportunity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IJobsPartnersWritableApi,
INiveauDiplomeEuropeen,
JOBPARTNERS_LABEL,
ZJobsPartnersOfferApi,
ZJobsPartnersRecruiterApi,
} from "shared/models/jobsPartners.model"
import { zOpcoLabel } from "shared/models/opco.model"
Expand All @@ -22,6 +23,7 @@ import { sentryCaptureException } from "@/common/utils/sentryUtils"
import { getRomeFromRomeo } from "@/services/cache.service"
import { getEntrepriseDataFromSiret, getGeoPoint, getOpcoData } from "@/services/etablissement.service"

import { logger } from "../../../common/logger"
import { IApiError } from "../../../common/utils/errorManager"
import { getDbCollection } from "../../../common/utils/mongodbUtils"
import { trackApiCall } from "../../../common/utils/sendTrackingEvent"
Expand Down Expand Up @@ -524,9 +526,30 @@ export async function findJobsOpportunities(payload: IJobOpportunityGetQuery, co
findFranceTravailOpportunities(resolvedQuery, context),
])

const jobs = [...offreEmploiLba, ...franceTravail, ...offreEmploiPartenaire].filter((job) => {
const parsedJob = ZJobsPartnersOfferApi.safeParse(job)
if (!parsedJob.success) {
const error = internal("jobOpportunity.service.ts-findJobsOpportunities: invalid job offer", { job, error: parsedJob.error.format() })
logger.error(error)
context.addWarning("JOB_OFFER_FORMATING_ERROR")
sentryCaptureException(error)
}
return parsedJob.success
})
const recruiters = convertLbaCompanyToJobPartnerRecruiterApi(recruterLba).filter((recruteur) => {
const parsedRecruiter = ZJobsPartnersRecruiterApi.safeParse(recruteur)
if (!parsedRecruiter.success) {
const error = internal("jobOpportunity.service.ts-findJobsOpportunities: invalid recruiter", { recruteur, error: parsedRecruiter.error.format() })
logger.error(error)
context.addWarning("RECRUITERS_FORMATING_ERROR")
sentryCaptureException(error)
}
return parsedRecruiter.success
})

return {
jobs: [...offreEmploiLba, ...franceTravail, ...offreEmploiPartenaire],
recruiters: convertLbaCompanyToJobPartnerRecruiterApi(recruterLba),
jobs,
recruiters,
warnings: context.getWarnings(),
}
}
Expand Down

0 comments on commit bde0c88

Please sign in to comment.