Skip to content

Commit

Permalink
fix(lbac-2328): nettoyage de la collection application (#1751)
Browse files Browse the repository at this point in the history
* fix(lbac-2328): nettoyage de la collection application

* fix: nom de la collection

* fix: tests

* fix: tests
  • Loading branch information
remy-auricoste authored Jan 15, 2025
1 parent 1a06c40 commit 82b1817
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 119 deletions.
4 changes: 2 additions & 2 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fileignoreconfig:
- filename: server/src/http/controllers/v2/applications.controller.v2.test.ts
checksum: 9147c705b5bdfe6eff6b1b75c467249ee1af783d57ff16d78722e48e60831c68
- filename: server/src/http/controllers/v2/appointment.controller.v2.test.ts
checksum: 52a0f730bc8feed13f4f1371c16a332872bec77ca31f0732422b7ca90b322df9
checksum: bbbf100d65bae0d840c715f4b1d893986df7423f470f3ee300545959105f6ca4
- filename: server/src/http/controllers/v2/jobs.controller.v2.test.ts
checksum: a99bfdcb5a8f4c66e9d055b44160adbc7ec2bcaf87209ff76fa9a9f674b9ca86
- filename: server/src/http/controllers/v2/jobs.controller.v2.ts
Expand Down Expand Up @@ -160,7 +160,7 @@ fileignoreconfig:
- filename: shared/helpers/generateUri.ts
checksum: 6542db0d3eca959c6e81d574f8b71d4b18d2f1af21042ca5ed4dff319cd39555
- filename: shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap
checksum: aa01a8388e2accb53cc88b46750fea7487998584a107059e3149695c0d219ae6
checksum: 903006c87a12fbb97070f96ee40d5acca427e46a00a3f4221cb0c7d351ede26b
- filename: shared/helpers/openapi/generateOpenapi.test.ts
checksum: d7b85c3dff488cec523d78f0926e15dbea41071a1864bda62b4d6caeb2541df3
- filename: shared/models/applications.model.ts
Expand Down
13 changes: 9 additions & 4 deletions server/src/http/controllers/application.controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { omit } from "lodash-es"
import { ObjectId } from "mongodb"
import { generateLbaCompanyFixture } from "shared/fixtures/recruteurLba.fixture"
import { beforeEach, describe, expect, it, vi } from "vitest"
Expand Down Expand Up @@ -86,16 +87,20 @@ describe("POST /v1/application", () => {
const applications = await getDbCollection("applications").find({}).toArray()
const applicant = await getDbCollection("applicants").findOne({ _id: applications[0]?.applicant_id })

expect(omit(applicant, ["createdAt", "last_connection", "updatedAt"])).toEqual({
_id: applicant?._id,
email: body.applicant_email,
firstname: body.applicant_first_name,
lastname: body.applicant_last_name,
phone: body.applicant_phone,
})

expect(applications).toEqual([
{
_id: expect.any(ObjectId),
applicant_id: applicant?._id,
applicant_attachment_name: body.applicant_file_name,
applicant_email: body.applicant_email,
applicant_first_name: body.applicant_first_name,
applicant_last_name: body.applicant_last_name,
applicant_message_to_company: "",
applicant_phone: body.applicant_phone,
caller: body.caller,
company_address: "126 RUE DE L UNIVERSITE, 75007 Paris",
company_email: recruteur.email,
Expand Down
25 changes: 17 additions & 8 deletions server/src/http/controllers/v2/applications.controller.v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { omit } from "lodash-es"
import { ObjectId } from "mongodb"
import { IApplicationApiPublic, JOB_STATUS } from "shared"
import { NIVEAUX_POUR_LBA, RECRUITER_STATUS } from "shared/constants"
Expand Down Expand Up @@ -141,15 +142,19 @@ describe("POST /v2/application", () => {
expect.soft(response.statusCode).toEqual(202)
expect.soft(response.json()).toEqual({ id: application!._id.toString() })

expect.soft(omit(applicant, ["createdAt", "last_connection", "updatedAt"])).toEqual({
_id: applicant?._id,
email: body.applicant_email,
firstname: body.applicant_first_name,
lastname: body.applicant_last_name,
phone: body.applicant_phone,
})

expect(application).toEqual({
_id: expect.any(ObjectId),
applicant_id: applicant?._id,
applicant_attachment_name: body.applicant_attachment_name,
applicant_email: body.applicant_email,
applicant_first_name: body.applicant_first_name,
applicant_last_name: body.applicant_last_name,
applicant_message_to_company: "",
applicant_phone: body.applicant_phone,
company_email: recruteur.email,
company_feedback: null,
company_name: "ASSEMBLEE NATIONALE",
Expand Down Expand Up @@ -200,15 +205,19 @@ describe("POST /v2/application", () => {
expect.soft(response.statusCode).toEqual(202)
expect.soft(response.json()).toEqual({ id: application!._id.toString() })

expect.soft(omit(applicant, ["createdAt", "last_connection", "updatedAt"])).toEqual({
_id: applicant?._id,
email: body.applicant_email,
firstname: body.applicant_first_name,
lastname: body.applicant_last_name,
phone: body.applicant_phone,
})

expect(application).toEqual({
_id: expect.any(ObjectId),
applicant_id: applicant?._id,
applicant_attachment_name: body.applicant_attachment_name,
applicant_email: body.applicant_email,
applicant_first_name: body.applicant_first_name,
applicant_last_name: body.applicant_last_name,
applicant_message_to_company: "",
applicant_phone: body.applicant_phone,
company_address: "Paris",
company_email: "[email protected]",
company_feedback: null,
Expand Down
24 changes: 13 additions & 11 deletions server/src/jobs/anonymization/anonymizeApplications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { logger } from "../../common/logger"
import { getDbCollection } from "../../common/utils/mongodbUtils"
import { notifyToSlack } from "../../common/utils/slackUtils"

export const anonymizeApplicationProjection = {
company_recruitment_intention: 1,
company_feedback_date: 1,
company_siret: 1,
company_naf: 1,
job_origin: 1,
job_id: 1,
caller: 1,
created_at: 1,
applicant_id: 1,
}

const anonymize = async () => {
logger.info(`Début anonymisation`)

Expand All @@ -16,17 +28,7 @@ const anonymize = async () => {
$match: matchCondition,
},
{
$project: {
company_recruitment_intention: 1,
company_feedback_date: 1,
company_siret: 1,
company_naf: 1,
job_origin: 1,
job_id: 1,
caller: 1,
created_at: 1,
applicant_id: 1,
},
$project: anonymizeApplicationProjection,
},
{
$merge: "anonymizedapplications",
Expand Down
41 changes: 0 additions & 41 deletions server/src/jobs/applications/fixApplications.ts

This file was deleted.

5 changes: 0 additions & 5 deletions server/src/jobs/simpleJobDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { generateSitemap } from "@/services/sitemap.service"
import { anonymizeApplicantsAndApplications } from "./anonymization/anonymizeApplicantAndApplications"
import { anonimizeUsersWithAccounts } from "./anonymization/anonymizeUserRecruteurs"
import { anonymizeUsers } from "./anonymization/anonymizeUsers"
import fixApplications from "./applications/fixApplications"
import { processApplications } from "./applications/processApplications"
import { sendContactsToBrevo } from "./brevoContacts/sendContactsToBrevo"
import { obfuscateCollections } from "./database/obfuscateCollections"
Expand Down Expand Up @@ -152,10 +151,6 @@ export const simpleJobDefinitions: SimpleJobDefinition[] = [
fct: fixJobExpirationDate,
description: "Répare les date d'expiration d'offre qui seraient trop dans le futur",
},
{
fct: fixApplications,
description: "Répare les adresses emails comportant des caractères erronés dans la collection applications",
},
{
fct: fixRecruiterDataValidation,
description: "Répare les data de la collection recruiters",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const saveBlacklistEmails = async (contacts) => {

if (await getDbCollection("applications").findOne({ company_email: email })) {
origin = BlackListOrigins.CANDIDATURE_SPONTANEE_RECRUTEUR
} else if (await getDbCollection("applications").findOne({ applicant_email: email })) {
} else if (await getDbCollection("applicants").findOne({ email })) {
origin = BlackListOrigins.CANDIDATURE_SPONTANEE_CANDIDAT
} else if (await getDbCollection("users").findOne({ email, role: EApplicantRole.CANDIDAT })) {
origin = BlackListOrigins.PRDV_CANDIDAT
Expand Down
15 changes: 15 additions & 0 deletions server/src/migrations/20250107000000-clean-applications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getDbCollection } from "@/common/utils/mongodbUtils"

export const up = async () => {
await getDbCollection("applications").updateMany(
{},
{
$unset: {
applicant_first_name: "",
applicant_last_name: "",
applicant_email: "",
applicant_phone: "",
},
}
)
}
42 changes: 42 additions & 0 deletions server/src/migrations/20250107000002-archive-applications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getDbCollection } from "@/common/utils/mongodbUtils"
import { anonymizeApplicationProjection } from "@/jobs/anonymization/anonymizeApplications"

export const up = async () => {
const filterStages = [
{
$lookup: {
from: "applicants",
localField: "applicant_id",
foreignField: "_id",
as: "matches",
},
},
{
$match: {
"matches.0": {
$exists: false,
},
},
},
]
await getDbCollection("applications")
.aggregate([
...filterStages,
{
$project: anonymizeApplicationProjection,
},
{
$merge: "anonymized_applications",
},
])
.toArray()
const idsToDelete = await getDbCollection("applications")
.aggregate([
...filterStages,
{
$project: { _id: 1 },
},
])
.toArray()
await getDbCollection("applications").deleteMany({ _id: { $in: idsToDelete.map(({ _id }) => _id) } })
}
10 changes: 8 additions & 2 deletions server/src/security/authorisationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { assertUnreachable, parseEnum } from "shared/utils"
import { Primitive } from "type-fest"

import { getDbCollection } from "@/common/utils/mongodbUtils"
import { getApplicantFromDB } from "@/services/applicant.service"
import { getComputedUserAccess, getGrantedRoles } from "@/services/roleManagement.service"
import { getUserWithAccountByEmail, isUserDisabled, isUserEmailChecked } from "@/services/userWithAccount.service"

Expand Down Expand Up @@ -241,8 +242,13 @@ async function getApplicationResource<S extends WithSecurityScheme>(schema: S, r
return { application }
}
const jobResource = await jobToJobResource(job, recruiter)
const user = await getUserWithAccountByEmail(application.applicant_email)
return { application, jobResource, user }
const applicantOpt = await getApplicantFromDB({ _id: application.applicant_id })
if (applicantOpt) {
const user = await getUserWithAccountByEmail(applicantOpt.email)
return { application, jobResource, user }
} else {
return { application, jobResource }
}
}

assertUnreachable(applicationDef)
Expand Down
13 changes: 7 additions & 6 deletions server/src/services/applicant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ const createApplicant = async (applicant: IApplicantNew) => {
return payload
}

export const getOrCreateApplicant = async (applicant: IApplicantNew | IApplicant) => {
let user = await getApplicantFromDB({ email: applicant?.email.toLowerCase() })
export const getApplicantByEmail = (email: string) => getApplicantFromDB({ email: email.toLowerCase() })

if (user) {
export const getOrCreateApplicant = async (applicant: IApplicantNew | IApplicant) => {
let dbApplicantOpt = await getApplicantByEmail(applicant.email.toLowerCase())
if (dbApplicantOpt) {
// update last_connection date on applicant collection (last application = last connection)
await updateApplicant(user._id, { last_connection: new Date() })
await updateApplicant(dbApplicantOpt._id, { last_connection: new Date() })
} else {
user = await createApplicant(applicant)
dbApplicantOpt = await createApplicant(applicant)
}

return user
return dbApplicantOpt
}
Loading

0 comments on commit 82b1817

Please sign in to comment.