Skip to content

Commit

Permalink
fix: lbac 1490: reprise des entreprises à mettre à jour
Browse files Browse the repository at this point in the history
  • Loading branch information
remy-auricoste committed Apr 30, 2024
1 parent e8e20c2 commit ccdedb8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
58 changes: 34 additions & 24 deletions server/src/jobs/lba_recruteur/user/misc/updateSiretInfosInError.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/jobs/multiCompte/migrationUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ccdedb8

Please sign in to comment.