From ccdedb875bd9ec87dbf582ddabbb97cc295421c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Auricoste?= Date: Tue, 30 Apr 2024 16:31:13 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20lbac=201490:=20reprise=20des=20entrepris?= =?UTF-8?q?es=20=C3=A0=20mettre=20=C3=A0=20jour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/misc/updateSiretInfosInError.ts | 58 +++++++++++-------- server/src/jobs/multiCompte/migrationUsers.ts | 2 +- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/server/src/jobs/lba_recruteur/user/misc/updateSiretInfosInError.ts b/server/src/jobs/lba_recruteur/user/misc/updateSiretInfosInError.ts index e777c7b37a..9d7656c185 100644 --- a/server/src/jobs/lba_recruteur/user/misc/updateSiretInfosInError.ts +++ b/server/src/jobs/lba_recruteur/user/misc/updateSiretInfosInError.ts @@ -1,7 +1,7 @@ import Boom from "boom" import { JOB_STATUS } from "shared" -import { CFA, RECRUITER_STATUS } from "shared/constants/recruteur" -import { EntrepriseStatus } from "shared/models/entreprise.model" +import { CFA, RECRUITER_STATUS, VALIDATION_UTILISATEUR } from "shared/constants/recruteur" +import { EntrepriseStatus, IEntrepriseStatusEvent } from "shared/models/entreprise.model" import { AccessEntityType, AccessStatus } from "shared/models/roleManagement.model" import { getLastStatusEvent } from "shared/utils/getLastStatusEvent" @@ -40,30 +40,40 @@ const updateEntreprisesInfosInError = async () => { if (!updatedEntreprise) { throw Boom.internal(`could not find and update entreprise with id=${_id}`) } + const entrepriseEvent: IEntrepriseStatusEvent = { + date: new Date(), + reason: "reprise des données des entreprises en erreur / MAJ", + status: EntrepriseStatus.VALIDE, + validation_type: VALIDATION_UTILISATEUR.AUTO, + granted_by: "", + } + await Entreprise.updateOne({ _id }, { $push: { status: entrepriseEvent } }) await Recruiter.updateMany({ establishment_siret: siret }, entrepriseData) - const recruiters = await Recruiter.find({ establishment_siret: siret }).lean() - const roles = await RoleManagement.find({ authorized_type: AccessEntityType.ENTREPRISE, authorized_id: updatedEntreprise._id.toString() }).lean() - const rolesToUpdate = roles.filter((role) => getLastStatusEvent(role.status)?.status !== AccessStatus.DENIED) - const users = await User2.find({ _id: { $in: rolesToUpdate.map((role) => role.user_id) } }).lean() - await Promise.all( - users.map(async (user) => { - const userAndOrganization: UserAndOrganization = { user, type: ENTREPRISE, organization: updatedEntreprise } - const result = await autoValidateUserRoleOnCompany(userAndOrganization, "reprise des entreprises en erreur") - if (result.validated) { - const recruiter = recruiters.find((recruiter) => recruiter.email === user.email && recruiter.establishment_siret === siret) - if (!recruiter) { - throw Boom.internal(`inattendu : recruiter non trouvé`, { email: user.email, siret }) - } - await activateEntrepriseRecruiterForTheFirstTime(recruiter) - const role = rolesToUpdate.find((role) => role.user_id.toString() === user._id.toString()) - const status = getLastStatusEvent(role?.status)?.status - if (!status) { - throw Boom.internal("inattendu : status du role non trouvé") + if (getLastStatusEvent(entreprise.status)?.status === EntrepriseStatus.ERROR) { + const recruiters = await Recruiter.find({ establishment_siret: siret }).lean() + const roles = await RoleManagement.find({ authorized_type: AccessEntityType.ENTREPRISE, authorized_id: updatedEntreprise._id.toString() }).lean() + const rolesToUpdate = roles.filter((role) => getLastStatusEvent(role.status)?.status !== AccessStatus.DENIED) + const users = await User2.find({ _id: { $in: rolesToUpdate.map((role) => role.user_id) } }).lean() + await Promise.all( + users.map(async (user) => { + const userAndOrganization: UserAndOrganization = { user, type: ENTREPRISE, organization: updatedEntreprise } + const result = await autoValidateUserRoleOnCompany(userAndOrganization, "reprise des entreprises en erreur") + if (result.validated) { + const recruiter = recruiters.find((recruiter) => recruiter.email === user.email && recruiter.establishment_siret === siret) + if (!recruiter) { + throw Boom.internal(`inattendu : recruiter non trouvé`, { email: user.email, siret }) + } + await activateEntrepriseRecruiterForTheFirstTime(recruiter) + const role = rolesToUpdate.find((role) => role.user_id.toString() === user._id.toString()) + const status = getLastStatusEvent(role?.status)?.status + if (!status) { + throw Boom.internal("inattendu : status du role non trouvé") + } + await sendEmailConfirmationEntreprise(user, recruiter, status, EntrepriseStatus.VALIDE) } - await sendEmailConfirmationEntreprise(user, recruiter, status, EntrepriseStatus.VALIDE) - } - }) - ) + }) + ) + } stats.success++ } } catch (err) { diff --git a/server/src/jobs/multiCompte/migrationUsers.ts b/server/src/jobs/multiCompte/migrationUsers.ts index 107f098121..2f8a00422c 100644 --- a/server/src/jobs/multiCompte/migrationUsers.ts +++ b/server/src/jobs/multiCompte/migrationUsers.ts @@ -190,7 +190,7 @@ const migrationUserRecruteurs = async () => { } } else { if (getLastStatusEvent(newEntreprise.status)?.status !== EntrepriseStatus.ERROR) { - if (!newEntreprise.address || !newEntreprise.address_detail || !newEntreprise.enseigne || !newEntreprise.raison_sociale || !newEntreprise.geo_coordinates) { + if (!newEntreprise.address || !newEntreprise.address_detail || !newEntreprise.raison_sociale || !newEntreprise.geo_coordinates) { newEntreprise.status.push({ date: new Date(), reason: "champ manquant",