Skip to content

Commit

Permalink
feat(lba-2248): blocage modification d'entreprise par un opco (#1687)
Browse files Browse the repository at this point in the history
* feat: vérification opco du recruteur

* feat: casting

* feat: suppression redirection après modif

* Update server/src/http/controllers/user.controller.ts

Co-authored-by: Rémy Auricoste <[email protected]>

* feat: refacto

* feat: rollback gestion cas de changement d'opco

---------

Co-authored-by: Rémy Auricoste <[email protected]>
  • Loading branch information
alanlr and remy-auricoste authored Dec 9, 2024
1 parent da7d6a1 commit cdeccef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
19 changes: 11 additions & 8 deletions server/src/http/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ObjectId } from "mongodb"
import { ENTREPRISE } from "shared/constants"
import { BusinessErrorCodes } from "shared/constants/errorCodes"
import { CFA, OPCOS_LABEL } from "shared/constants/recruteur"
import { IJob, IRecruiter, getUserStatus, parseEnumOrError, zRoutes } from "shared/index"
import { IJob, IRecruiter, getUserStatus, parseEnum, parseEnumOrError, zRoutes } from "shared/index"
import { ICFA } from "shared/models/cfa.model"
import { IEntreprise } from "shared/models/entreprise.model"
import { AccessEntityType, AccessStatus } from "shared/models/roleManagement.model"
Expand All @@ -15,7 +15,7 @@ import { activateUserRole, deactivateUserRole, roleToUserType, entrepriseIsNotMy
import { createSuperUser } from "@/services/userWithAccount.service"

import { getDbCollection } from "../../common/utils/mongodbUtils"
import { deleteFormulaire, getFormulaireFromUserId } from "../../services/formulaire.service"
import { deleteFormulaire, getFormulaireFromUserId, getFormulaireFromUserIdWithOpco } from "../../services/formulaire.service"
import { getUserAndRecruitersDataForOpcoUser, getUserNamesFromIds as getUsersFromIds } from "../../services/user.service"
import {
getAdminUsers,
Expand Down Expand Up @@ -199,6 +199,7 @@ export default (server: Server) => {
async (req, res) => {
const requestUser = getUserFromRequest(req, zRoutes.get["/user/:userId/organization/:organizationId"]).value
if (!requestUser) throw badRequest()

const { userId } = req.params
const role = await getDbCollection("rolemanagements").findOne({
user_id: new ObjectId(userId),
Expand Down Expand Up @@ -229,18 +230,20 @@ export default (server: Server) => {
let jobs: IJob[] = []
let formulaire: IRecruiter | null = null

const opcoOrAdminRole = await getDbCollection("rolemanagements").findOne({
user_id: requestUser._id,
authorized_type: { $in: [AccessEntityType.ADMIN, AccessEntityType.OPCO] },
})

const opco: OPCOS_LABEL | null = opcoOrAdminRole?.authorized_type === AccessEntityType.OPCO ? parseEnum(OPCOS_LABEL, opcoOrAdminRole.authorized_id) : null

if (type === ENTREPRISE) {
formulaire = await getFormulaireFromUserId(userId)
formulaire = opco ? await getFormulaireFromUserIdWithOpco(userId, opco) : await getFormulaireFromUserId(userId)
jobs = formulaire?.jobs ?? []
}

const userRecruteur = userAndRoleAndOrganizationToUserRecruteur(user, role, organization, formulaire)

const opcoOrAdminRole = await getDbCollection("rolemanagements").findOne({
user_id: requestUser._id,
authorized_type: { $in: [AccessEntityType.ADMIN, AccessEntityType.OPCO] },
})

if (opcoOrAdminRole && getLastStatusEvent(opcoOrAdminRole.status)?.status === AccessStatus.GRANTED) {
const userIds = userRecruteur.status.flatMap(({ user }) => (user ? [user] : []))
const users = await getUsersFromIds(userIds)
Expand Down
6 changes: 5 additions & 1 deletion server/src/services/formulaire.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import equal from "fast-deep-equal"
import { Filter, ObjectId, UpdateFilter } from "mongodb"
import { IDelegation, IJob, IJobCreate, IJobWithRomeDetail, IRecruiter, IRecruiterWithApplicationCount, IUserRecruteur, JOB_STATUS, removeAccents } from "shared"
import { getDirectJobPath } from "shared/constants/lbaitem"
import { RECRUITER_STATUS } from "shared/constants/recruteur"
import { OPCOS_LABEL, RECRUITER_STATUS } from "shared/constants/recruteur"
import { EntrepriseStatus, IEntreprise } from "shared/models/entreprise.model"
import { AccessEntityType, AccessStatus } from "shared/models/roleManagement.model"
import { IUserWithAccount } from "shared/models/userWithAccount.model"
Expand Down Expand Up @@ -839,6 +839,10 @@ export const getFormulaireFromUserId = async (userId: string) => {
return getDbCollection("recruiters").findOne({ managed_by: userId })
}

export const getFormulaireFromUserIdWithOpco = async (userId: string, opco: OPCOS_LABEL) => {
return getDbCollection("recruiters").findOne({ managed_by: userId, opco })
}

export const getFormulaireFromUserIdOrError = async (userId: string) => {
const formulaire = await getFormulaireFromUserId(userId)
if (!formulaire) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ export const getUserAndRecruitersDataForOpcoUser = async (
disable: IUserForOpco[]
}> => {
const userRecruteurs = await getUserRecruteursForManagement({ opco })

const filteredUserRecruteurs = [...userRecruteurs.active, ...userRecruteurs.awaiting, ...userRecruteurs.disabled]
const userIds = [...new Set(filteredUserRecruteurs.map(({ _id }) => _id.toString()))]
const recruiters = await getDbCollection("recruiters")
.find({ "jobs.managed_by": { $in: userIds } }, { projection: { establishment_id: 1, origin: 1, jobs: 1, _id: 0 } })
.find({ "jobs.managed_by": { $in: userIds }, opco }, { projection: { establishment_id: 1, origin: 1, jobs: 1, _id: 0 } })
.toArray()

const recruiterMap = new Map<string, (typeof recruiters)[0]>()
Expand Down

0 comments on commit cdeccef

Please sign in to comment.