diff --git a/server/src/http/controllers/user.controller.ts b/server/src/http/controllers/user.controller.ts index 785564b9e..c20920cbc 100644 --- a/server/src/http/controllers/user.controller.ts +++ b/server/src/http/controllers/user.controller.ts @@ -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" @@ -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, @@ -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), @@ -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) diff --git a/server/src/services/formulaire.service.ts b/server/src/services/formulaire.service.ts index 08f7a75e4..d7bf049f2 100644 --- a/server/src/services/formulaire.service.ts +++ b/server/src/services/formulaire.service.ts @@ -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" @@ -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) { diff --git a/server/src/services/user.service.ts b/server/src/services/user.service.ts index d63aef78e..f3b7b1e52 100644 --- a/server/src/services/user.service.ts +++ b/server/src/services/user.service.ts @@ -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()