Skip to content

Commit

Permalink
fix: ajustement du model job-partner (#1506)
Browse files Browse the repository at this point in the history
* fix: ajustement du model job-partner

* fix: merge conflicts

---------

Co-authored-by: Alan Le Ruyet <[email protected]>
  • Loading branch information
moroine and alanlr authored Sep 12, 2024
1 parent dc48f49 commit b4a05cb
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 107 deletions.
42 changes: 1 addition & 41 deletions server/src/http/controllers/v2/jobs.controller.v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,15 @@ describe("POST /jobs", async () => {
const inSept = new Date("2024-09-01T00:00:00.000Z")

const data: IJobsPartnersWritableApiInput = {
partner_job_id: null,

contract_start: inSept.toJSON(),
contract_duration: null,
contract_type: null,
contract_remote: null,

offer_title: "Apprentis en développement web",
offer_rome_codes: ["M1602"],
offer_desired_skills: [],
offer_to_be_acquired_skills: [],
offer_access_conditions: [],
offer_creation: null,
offer_expiration: null,
offer_opening_count: 1,
offer_origin: null,
offer_multicast: true,
offer_description: "Envie de devenir développeur web ? Rejoignez-nous !",
offer_diploma_level_european: null,

apply_url: null,
apply_email: "[email protected]",
apply_phone: null,

workplace_siret: apiEntrepriseEtablissementFixture.dinum.data.siret,
workplace_address_label: null,
workplace_description: null,
workplace_website: null,
workplace_name: null,
}

beforeEach(async () => {
Expand Down Expand Up @@ -434,35 +414,15 @@ describe("PUT /jobs/:id", async () => {
const originalJob = generateJobsPartnersOfferPrivate({ _id: id, offer_title: "Old title", partner_label: "Un super Partenaire" })

const data: IJobsPartnersWritableApiInput = {
partner_job_id: null,

contract_start: inSept.toJSON(),
contract_duration: null,
contract_type: null,
contract_remote: null,

offer_title: "Apprentis en développement web",
offer_rome_codes: ["M1602"],
offer_desired_skills: [],
offer_to_be_acquired_skills: [],
offer_access_conditions: [],
offer_creation: null,
offer_expiration: null,
offer_opening_count: 1,
offer_origin: null,
offer_multicast: true,
offer_description: "Envie de devenir développeur web ? Rejoignez-nous !",
offer_diploma_level_european: null,

apply_url: null,
apply_email: "[email protected]",
apply_phone: null,

workplace_siret: apiEntrepriseEtablissementFixture.dinum.data.siret,
workplace_address_label: null,
workplace_description: null,
workplace_website: null,
workplace_name: null,
}

beforeEach(async () => {
Expand Down Expand Up @@ -551,7 +511,7 @@ describe("PUT /jobs/:id", async () => {
expect(response.json()).toEqual({ error: "Forbidden", message: "Unauthorized", statusCode: 403 })
})

it("should return 403 if user is trying to edit other partner job", async () => {
it("should return 403 if user is trying to edit other partner_label job", async () => {
const restrictedToken = getApiApprentissageTestingToken({ email: "[email protected]", organisation: "Un autre", habilitations: { "jobs:write": true } })

const response = await httpClient().inject({
Expand Down
2 changes: 1 addition & 1 deletion server/src/jobs/offrePartenaire/helloWorkMapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("helloWorkJobToJobsPartners", () => {
}
})

it("should convert a hellowork job to a partner job", () => {
it("should convert a hellowork job to a partner_label job", () => {
expect(
helloWorkJobToJobsPartners({
job_id: "73228597",
Expand Down
8 changes: 4 additions & 4 deletions server/src/jobs/offrePartenaire/rawToComputedJobsPartners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const rawToComputedJobsPartners = async <ZodInput extends AnyZodObject>({
partnerLabel: JOBPARTNERS_LABEL
documentJobRoot?: string
}) => {
logger.info(`début d'import dans computed_jobs_partners pour partner=${partnerLabel}`)
logger.info(`début d'import dans computed_jobs_partners pour partner_label=${partnerLabel}`)
const deletedCount = await getDbCollection("computed_jobs_partners").countDocuments({ partner_label: partnerLabel })
logger.info(`suppression de ${deletedCount} documents dans computed_jobs_partners pour partner=${partnerLabel}`)
logger.info(`suppression de ${deletedCount} documents dans computed_jobs_partners pour partner_label=${partnerLabel}`)
await getDbCollection("computed_jobs_partners").deleteMany({ partner_label: partnerLabel })
const counters = { total: 0, success: 0, error: 0 }
await oleoduc(
Expand All @@ -45,7 +45,7 @@ export const rawToComputedJobsPartners = async <ZodInput extends AnyZodObject>({
counters.success++
} catch (err) {
counters.error++
const newError = internal(`error converting raw job to partner job for partner=${partnerLabel}, id=${document._id}`)
const newError = internal(`error converting raw job to partner_label job for partner_label=${partnerLabel}, id=${document._id}`)
logger.error(newError.message, err)
newError.cause = err
sentryCaptureException(newError)
Expand All @@ -54,5 +54,5 @@ export const rawToComputedJobsPartners = async <ZodInput extends AnyZodObject>({
{ parallel: 10 }
)
)
logger.info(`import dans computed_jobs_partners pour partner=${partnerLabel} terminé`, counters)
logger.info(`import dans computed_jobs_partners pour partner_label=${partnerLabel} terminé`, counters)
}
10 changes: 5 additions & 5 deletions server/src/security/authorisationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ADMIN, CFA, ENTREPRISE, OPCOS_LABEL } from "shared/constants/recruteur"
import { ComputedUserAccess, IApplication, IJob, IRecruiter } from "shared/models"
import { ICFA } from "shared/models/cfa.model"
import { IEntreprise } from "shared/models/entreprise.model"
import { IJobsPartnersOfferApi } from "shared/models/jobsPartners.model"
import { IJobsPartnersOfferPrivate } from "shared/models/jobsPartners.model"
import { AccessEntityType, IRoleManagement } from "shared/models/roleManagement.model"
import { IRouteSchema, WithSecurityScheme } from "shared/routes/common.routes"
import { AccessPermission, AccessResourcePath } from "shared/security/permissions"
Expand All @@ -22,7 +22,7 @@ type RecruiterResource = { recruiter: IRecruiter } & ({ type: "ENTREPRISE"; entr
type JobResource = { job: IJob; recruiterResource: RecruiterResource }
type ApplicationResource = { application: IApplication; jobResource?: JobResource; applicantId?: string }
type EntrepriseResource = { entreprise: IEntreprise }
type JobPartnerResource = { job: IJobsPartnersOfferApi }
type JobPartnerResource = { job: IJobsPartnersOfferPrivate }

type Resources = {
users: Array<{ _id: string }>
Expand Down Expand Up @@ -305,7 +305,7 @@ function canAccessEntreprise(userAccess: ComputedUserAccess, resource: Entrepris

function canAccessJobPartner(userAccess: ComputedUserAccess, resource: JobPartnerResource): boolean {
const { job } = resource
return userAccess.partner.includes(job.partner_label)
return userAccess.partner_label.includes(job.partner_label)
}

function isAuthorized(access: AccessPermission, userAccess: ComputedUserAccess, resources: Resources): boolean {
Expand Down Expand Up @@ -384,7 +384,7 @@ export async function authorizationMiddleware<S extends Pick<IRouteSchema, "meth
cfas: [],
entreprises: [],
opcos: opco ? [opco] : [],
partner: [],
partner_label: [],
}
if (!isAuthorized(requestedAccess, userAccess, resources)) {
throw forbidden("non autorisé")
Expand All @@ -409,7 +409,7 @@ export async function authorizationMiddleware<S extends Pick<IRouteSchema, "meth
cfas: [],
entreprises: [],
opcos: [],
partner: [organisation],
partner_label: [organisation],
}
if (!isAuthorized(requestedAccess, userAccess, resources)) {
throw forbidden("Unauthorized")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ exports[`createJobOffer > should create a job offer with the minimal data 1`] =
"contract_duration": null,
"contract_remote": null,
"contract_start": 2024-09-01T00:00:00.000Z,
"contract_type": null,
"contract_type": [
"Apprentissage",
"Professionnalisation",
],
"created_at": 2024-06-18T00:00:00.000Z,
"offer_access_conditions": [],
"offer_creation": 2024-06-18T00:00:00.000Z,
Expand Down Expand Up @@ -157,7 +160,10 @@ exports[`findJobsOpportunities > should execute query 1`] = `
"contract_duration": null,
"contract_remote": null,
"contract_start": null,
"contract_type": null,
"contract_type": [
"Apprentissage",
"Professionnalisation",
],
"created_at": 2021-01-28T15:00:00.000Z,
"distance": 0,
"offer_access_conditions": [],
Expand Down Expand Up @@ -242,7 +248,10 @@ exports[`updateJobOffer > should update a job offer with the minimal data 1`] =
"contract_duration": null,
"contract_remote": null,
"contract_start": 2024-09-01T00:00:00.000Z,
"contract_type": null,
"contract_type": [
"Apprentissage",
"Professionnalisation",
],
"created_at": 2023-09-05T22:00:00.000Z,
"offer_access_conditions": [],
"offer_creation": 2023-09-05T22:00:00.000Z,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ describe("createJobOffer", () => {

contract_start: inSept,
contract_duration: null,
contract_type: null,
contract_type: ["Apprentissage", "Professionnalisation"],
contract_remote: null,

offer_title: "Apprentis en développement web",
Expand All @@ -1767,7 +1767,7 @@ describe("createJobOffer", () => {
offer_origin: null,
offer_multicast: true,
offer_description: "Envie de devenir développeur web ? Rejoignez-nous !",
offer_diploma_level_european: null,
offer_target_diploma_european: null,

apply_url: null,
apply_email: null,
Expand Down Expand Up @@ -1886,7 +1886,7 @@ describe("updateJobOffer", () => {

contract_start: inSept,
contract_duration: null,
contract_type: null,
contract_type: ["Apprentissage", "Professionnalisation"],
contract_remote: null,

offer_title: "Apprentis en développement web",
Expand All @@ -1900,7 +1900,7 @@ describe("updateJobOffer", () => {
offer_origin: null,
offer_multicast: true,
offer_description: "Envie de devenir développeur web ? Rejoignez-nous !",
offer_diploma_level_european: null,
offer_target_diploma_european: null,

apply_url: null,
apply_email: null,
Expand Down
18 changes: 12 additions & 6 deletions server/src/services/jobs/jobOpportunity/jobOpportunity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const getJobsQuery = async (
return result
}

export const getJobsPartnersFromDB = async ({ romes, geo, target_diploma_level }: IJobOpportunityGetQueryResolved): Promise<IJobsPartnersOfferPrivate[]> => {
export const getJobsPartnersFromDB = async ({ romes, geo, target_diploma_level }: IJobOpportunityGetQueryResolved): Promise<IJobsPartnersOfferApi[]> => {
const query: Filter<IJobsPartnersOfferPrivate> = {
offer_multicast: true,
}
Expand Down Expand Up @@ -225,14 +225,20 @@ export const getJobsPartnersFromDB = async ({ romes, geo, target_diploma_level }
{ $sort: { distance: 1, offer_creation: -1 } },
]

return await getDbCollection("jobs_partners")
const jobsPartners = await getDbCollection("jobs_partners")
.aggregate<IJobsPartnersOfferPrivate>([
...filterStages,
{
$limit: 150,
},
])
.toArray()

return jobsPartners.map((j) => ({
...j,
// TODO: set LBA url
apply_url: j.apply_url ?? `${config.publicUrl}/recherche-apprentissage`,
}))
}

const convertToGeopoint = ({ longitude, latitude }: { longitude: number; latitude: number }): IGeoPoint => ({ type: "Point", coordinates: [longitude, latitude] })
Expand Down Expand Up @@ -621,7 +627,7 @@ async function upsertJobOffer(data: IJobsPartnersWritableApi, identity: IApiAppr
throw internal("unexpected: cannot resolve all required data for the job offer")
}

const { offer_creation, offer_expiration, offer_rome_codes, offer_diploma_level_european, workplace_address_label, ...rest } = data
const { offer_creation, offer_expiration, offer_rome_codes, offer_target_diploma_european, workplace_address_label, ...rest } = data
const now = new Date()

const invariantData: Pick<IJobsPartnersOfferPrivate, InvariantFields> = {
Expand All @@ -640,11 +646,11 @@ async function upsertJobOffer(data: IJobsPartnersWritableApi, identity: IApiAppr
offer_creation: offer_creation ?? invariantData.created_at,
offer_expiration: offer_expiration || defaultOfferExpiration,
offer_target_diploma:
offer_diploma_level_european == null
offer_target_diploma_european == null
? null
: {
european: offer_diploma_level_european,
label: NIVEAU_DIPLOME_LABEL[offer_diploma_level_european],
european: offer_target_diploma_european,
label: NIVEAU_DIPLOME_LABEL[offer_target_diploma_european],
},
updated_at: now,
...rest,
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/roleManagement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const getComputedUserAccess = (userId: string, grantedRoles: IRoleManagem
}
return []
}),
partner: [],
partner_label: [],
}
return userAccess
}
Expand Down
4 changes: 2 additions & 2 deletions shared/fixtures/jobPartners.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export function generateJobsPartnersOfferPrivate(data: Partial<IJobsPartnersOffe
workplace_naf_code: null,
workplace_naf_label: null,

apply_url: null,
apply_url: "https://apply.com",
apply_phone: null,
partner_label: JOBPARTNERS_LABEL.HELLOWORK,
partner_job_id: null,

contract_start: null,
contract_duration: null,
contract_type: null,
contract_type: ["Apprentissage", "Professionnalisation"],
contract_remote: null,

offer_title: "Une super offre d'alternance",
Expand Down
2 changes: 1 addition & 1 deletion shared/models/computedUserAccess.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const ZComputedUserAccess = z
entreprises: z.array(z.string()),
cfas: z.array(z.string()),
opcos: z.array(extensions.buildEnum(OPCOS_LABEL)),
partner: z.array(z.string()),
partner_label: z.array(z.string()),
})
.strict()

Expand Down
Loading

0 comments on commit b4a05cb

Please sign in to comment.