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

feat(admin): 3259 - Donner les droits apres la fin de la date d'instruction aux référents #4524

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions admin/src/scenes/volontaires/view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Switch } from "react-router-dom";
import { useSelector } from "react-redux";
import { useQuery } from "@tanstack/react-query";

import { YoungDto, ROLES } from "snu-lib";
import { YoungDto, isReferentReg, isAdmin } from "snu-lib";
import { AuthState } from "@/redux/auth/reducer";
import useDocumentTitle from "@/hooks/useDocumentTitle";
import { SentryRoute } from "@/sentry";
Expand All @@ -23,6 +23,7 @@ import FormEquivalence from "./FormEquivalence";
import VolontairePhase0View from "../../phase0/view";
import ProposeMission from "./proposalMission";
import CustomMission from "./customMission";
import { isAfter } from "date-fns";

export default function Index({ ...props }) {
const cohorts = useSelector((state: CohortState) => state.Cohorts);
Expand All @@ -43,7 +44,7 @@ export default function Index({ ...props }) {
? "correction"
: "readonly";
const cohort = cohorts.find(({ _id, name }) => _id === young?.cohortId || name === young?.cohort);
if (user.role !== ROLES.ADMIN && new Date() > new Date(cohort?.instructionEndDate || "")) {
if (!isAdmin(user) && !isReferentReg(user) && cohort?.instructionEndDate && isAfter(new Date(), new Date(cohort.instructionEndDate))) {
mode = "readonly";
}

Expand Down
12 changes: 11 additions & 1 deletion api/src/young/edition/youngEditionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ import {
canEditYoung,
canAllowSNU,
YoungType,
isAdmin,
isReferentReg,
getPhaseStatusOptions,
} from "snu-lib";
import { getDensity, getQPV } from "../../geo";
import { sendTemplate } from "../../brevo";
import { format } from "date-fns";
import { format, isAfter } from "date-fns";
import config from "config";
const { logger } = require("../../logger");
import { validateId, idSchema } from "../../utils/validator";
Expand Down Expand Up @@ -116,6 +118,10 @@ router.put("/:id/identite", passport.authenticate("referent", { session: false,
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_UNAUTHORIZED });
}

const cohort = await CohortModel.findById(young.cohortId);
if (!isAdmin(req.user) && !isReferentReg(req.user) && cohort?.instructionEndDate && isAfter(new Date(), new Date(cohort.instructionEndDate)))
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_UNAUTHORIZED });

if (value.zip && value.city && value.address) {
const qpv = await getQPV(value.zip, value.city, value.address);
if (qpv === true) value.qpv = "true";
Expand Down Expand Up @@ -260,6 +266,10 @@ router.put("/:id/situationparents", passport.authenticate("referent", { session:
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_UNAUTHORIZED });
}

const cohort = await CohortModel.findById(young.cohortId);
if (!isAdmin(req.user) && !isReferentReg(req.user) && cohort?.instructionEndDate && isAfter(new Date(), new Date(cohort.instructionEndDate)))
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_UNAUTHORIZED });

young.set(value);
young.set({
employed: youngEmployedSituationOptions.includes(value.situation) ? "true" : "false",
Expand Down
42 changes: 26 additions & 16 deletions packages/lib/src/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ function isAdmin(user: UserRoles) {
return ROLES.ADMIN === user.role;
}

function isReferentReg(user: UserRoles) {
return ROLES.REFERENT_REGION === user.role;
}

function isReferentDep(user: UserRoles) {
return ROLES.REFERENT_DEPARTMENT === user.role;
}

function isReferentRegDep(user: UserRoles) {
return [ROLES.REFERENT_DEPARTMENT, ROLES.REFERENT_REGION].includes(user.role || "");
}
Expand Down Expand Up @@ -683,22 +691,22 @@ function canDownloadYoungDocuments(actor: UserDto, target?: UserDto, type?: stri
}

function canInviteYoung(actor: UserDto, cohort: CohortDto | null) {
if (!cohort) return false;

switch (actor.role) {
case ROLES.ADMIN:
return true;
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
return cohort.inscriptionOpenForReferentRegion === true;
case ROLES.REFERENT_CLASSE:
return cohort.inscriptionOpenForReferentClasse === true;
case ROLES.ADMINISTRATEUR_CLE:
return cohort.inscriptionOpenForAdministrateurCle === true;
default:
return false;
}
if (!cohort) return false;

switch (actor.role) {
case ROLES.ADMIN:
return true;
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
return cohort.inscriptionOpenForReferentRegion === true;
case ROLES.REFERENT_CLASSE:
return cohort.inscriptionOpenForReferentClasse === true;
case ROLES.ADMINISTRATEUR_CLE:
return cohort.inscriptionOpenForAdministrateurCle === true;
default:
return false;
}
}

function canSendTemplateToYoung(actor, young) {
Expand Down Expand Up @@ -1193,6 +1201,8 @@ export {
canCreateTags,
isSuperAdmin,
isAdmin,
isReferentReg,
isReferentDep,
isAdminCle,
isChefEtablissement,
isCoordinateurEtablissement,
Expand Down
Loading