Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lbac 1490: reprise des entreprises à mettre à jour #1214

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 +40,21 @@ 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) => {
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 asyncForEach(users, async (user) => {
const userAndOrganization: UserAndOrganization = { user, type: ENTREPRISE, organization: updatedEntreprise }
const result = await autoValidateUserRoleOnCompany(userAndOrganization, "reprise des entreprises en erreur")
if (result.validated) {
Expand All @@ -63,7 +71,7 @@ const updateEntreprisesInfosInError = async () => {
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
Loading