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(api, admin, lib): 3423 - Add manual inscription rights according to dynamic HTS settings #4506

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { CohortDto, COHORT_TYPE, ROLES } from "snu-lib";

import { renderManualInscriptionCLEToggles } from "./renderManualInscriptionCLEToggles";
import { renderManualInscriptionHTSToggles } from "./renderManualInscriptionHTSToggles";

type ToggleItem = {
label: string;
field: keyof CohortDto;
role: string;
};

interface BaseProps {
cohort: CohortDto;
handleToggleChange: (field: keyof CohortDto) => void;
isLoading: boolean;
readOnly: boolean;
}

export interface RenderManualInscriptionTogglesProps extends BaseProps {
toggles: ToggleItem[];
}

export interface RenderManualInscriptionHTSTogglesProps extends RenderManualInscriptionTogglesProps {
handleOffsetChange: (field: keyof CohortDto, value: number) => void;
}

type CLEProps = BaseProps & { cohort: CohortDto & { type: typeof COHORT_TYPE.CLE } };
type HTSProps = BaseProps & {
cohort: CohortDto & { type: typeof COHORT_TYPE.VOLONTAIRE };
handleOffsetChange: (field: keyof CohortDto, value: number) => void;
};

export type ManualInscriptionTogglesByCohortTypeProps = CLEProps | HTSProps;

const togglesCommon: ToggleItem[] = [
{
label: "Référents régionaux",
field: "inscriptionOpenForReferentRegion",
role: ROLES.REFERENT_REGION,
},
{
label: "Référents départementaux",
field: "inscriptionOpenForReferentDepartment",
role: ROLES.REFERENT_DEPARTMENT,
},
];

const togglesCLE: ToggleItem[] = [
{
label: "Référents de classe",
field: "inscriptionOpenForReferentClasse",
role: ROLES.REFERENT_CLASSE,
},
{
label: "Administrateurs CLE",
field: "inscriptionOpenForAdministrateurCle",
role: ROLES.ADMINISTRATEUR_CLE,
},
];

export const getManualInscriptionTogglesByCohortType = (props: ManualInscriptionTogglesByCohortTypeProps) => {
switch (props.cohort.type) {
case COHORT_TYPE.CLE:
return renderManualInscriptionCLEToggles({
...props,
toggles: [...togglesCLE, ...togglesCommon],
});
case COHORT_TYPE.VOLONTAIRE:
return renderManualInscriptionHTSToggles({
...(props as HTSProps),
toggles: togglesCommon,
});
default:
return null;
}
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useMemo } from "react";
import { CohortDto } from "snu-lib";
import { getManualInscriptionTogglesByCohortType } from "./getManualInscriptionTogglesByCohortType";
import { CohortDto, COHORT_TYPE } from "snu-lib";
import { getManualInscriptionTogglesByCohortType, ManualInscriptionTogglesByCohortTypeProps } from "./getManualInscriptionTogglesByCohortType";
import ReactTooltip from "react-tooltip";
import React from "react";
import { MdInfoOutline } from "react-icons/md";

interface ManualInscriptionSettingsProps {
cohort: CohortDto;
cohort: CohortDto & { type: (typeof COHORT_TYPE)[keyof typeof COHORT_TYPE] };
setCohort: React.Dispatch<React.SetStateAction<CohortDto>>;
isLoading: boolean;
readOnly: boolean;
Expand All @@ -20,21 +20,28 @@ export const ManualInscriptionSettings: React.FC<ManualInscriptionSettingsProps>
[setCohort],
);

const renderedManualInscriptionTogglesByCohortType = useMemo(
() => getManualInscriptionTogglesByCohortType({ cohort, handleToggleChange, isLoading, readOnly }),
// Disabled exhaustive-deps because we don't want to re-render the component when the all props data changes only specific fields should trigger a re-render
// eslint-disable-next-line react-hooks/exhaustive-deps
[
cohort.type,
cohort.inscriptionOpenForReferentRegion,
cohort.inscriptionOpenForReferentDepartment,
cohort.inscriptionOpenForReferentClasse,
cohort.inscriptionOpenForAdministrateurCle,
const handleOffsetChange = useCallback(
(field: keyof CohortDto, value: number) => {
setCohort((prevCohort) => ({
...prevCohort,
[field]: value,
}));
},
[setCohort],
);

const renderedManualInscriptionTogglesByCohortType = useMemo(() => {
const baseProps = {
cohort,
handleToggleChange,
isLoading,
readOnly,
],
);
};

return getManualInscriptionTogglesByCohortType(
(cohort.type === COHORT_TYPE.VOLONTAIRE ? { ...baseProps, handleOffsetChange } : baseProps) as ManualInscriptionTogglesByCohortTypeProps,
);
}, [cohort, handleToggleChange, handleOffsetChange, isLoading, readOnly]);

// If no rendered toggles according to the cohort type, we don't display anything
if (!renderedManualInscriptionTogglesByCohortType) return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import SimpleToggle from "@/components/ui/forms/dateForm/SimpleToggle";
import { RenderManualInscriptionTogglesProps } from "./getManualInscriptionTogglesByCohortType";

export const renderManualInscriptionCLEToggles = ({ cohort, handleToggleChange, isLoading, readOnly, toggles }: RenderManualInscriptionTogglesProps) => {
return (
<>
{toggles.map((toggle) => {
const toggleValue = (cohort[toggle.field] as boolean) ?? false;
return <SimpleToggle key={toggle.field} label={toggle.label} value={toggleValue} disabled={isLoading || readOnly} onChange={() => handleToggleChange(toggle.field)} />;
})}
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React from "react";
import { CohortDto, ROLES } from "snu-lib";
import { Select } from "@snu/ds/admin";
import SimpleToggle from "@/components/ui/forms/dateForm/SimpleToggle";
import { RenderManualInscriptionHTSTogglesProps } from "./getManualInscriptionTogglesByCohortType";

import dayjs from "@/utils/dayjs.utils";

interface HTSInscriptionDates {
startDate: string;
endDate: string;
}

export function calculateHTSInscriptionDates(cohort: CohortDto, role: string): HTSInscriptionDates | null {
const inscriptionStartDate = dayjs.utc(cohort.inscriptionStartDate);
const instructionEndDate = dayjs.utc(cohort.instructionEndDate);

const calculateDates = (startOffset: number = 0, endOffset: number = 0): HTSInscriptionDates => ({
startDate: inscriptionStartDate.add(startOffset, "day").format("DD/MM/YYYY HH:mm"),
endDate: instructionEndDate.add(endOffset, "day").format("DD/MM/YYYY HH:mm"),
});

if (role === ROLES.REFERENT_REGION) {
return calculateDates(cohort.inscriptionHTSStartOffsetForReferentRegion, cohort.inscriptionHTSEndOffsetForReferentRegion);
} else if (role === ROLES.REFERENT_DEPARTMENT) {
return calculateDates(cohort.inscriptionHTSStartOffsetForReferentDepartment, cohort.inscriptionHTSEndOffsetForReferentDepartment);
}

return null;
}

export const getInscriptionHTSOffsetsByUserRole = (role: string, cohort: CohortDto): { startOffset: number | null; endOffset: number | null } => {
switch (role) {
case ROLES.REFERENT_REGION:
return {
startOffset: cohort.inscriptionHTSStartOffsetForReferentRegion ?? 0,
endOffset: cohort.inscriptionHTSEndOffsetForReferentRegion ?? 0,
};
case ROLES.REFERENT_DEPARTMENT:
return {
startOffset: cohort.inscriptionHTSStartOffsetForReferentDepartment ?? 0,
endOffset: cohort.inscriptionHTSEndOffsetForReferentDepartment ?? 0,
};
default:
return {
startOffset: null,
endOffset: null,
};
}
};

const getOffsetFieldNames = (role: string): { startField: keyof CohortDto; endField: keyof CohortDto } => {
const roleType = role === ROLES.REFERENT_REGION ? "ReferentRegion" : "ReferentDepartment";
return {
startField: `inscriptionHTSStartOffsetFor${roleType}` as keyof CohortDto,
endField: `inscriptionHTSEndOffsetFor${roleType}` as keyof CohortDto,
};
};

export const renderManualInscriptionHTSToggles = ({ cohort, handleToggleChange, handleOffsetChange, isLoading, readOnly, toggles }: RenderManualInscriptionHTSTogglesProps) => {
const dateOffsetOptions = Array.from({ length: 21 }, (_, i) => ({ value: i - 5, label: `${i - 5}` }));

return (
<>
{toggles.map((toggle) => {
const field = toggle.field as keyof CohortDto;
const toggleValue = cohort[field] as boolean;
const { startOffset, endOffset } = getInscriptionHTSOffsetsByUserRole(toggle.role as string, cohort);
const htsInscriptionDatesCalculation = calculateHTSInscriptionDates(cohort, toggle.role as string);
const { startField: inscriptionHTSStartOffsetField, endField: inscriptionHTSEndOffsetField } = getOffsetFieldNames(toggle.role);
return (
<div key={field} className="flex flex-col gap-2 rounded-lg bg-gray-100">
<SimpleToggle label={toggle.label} value={toggleValue ?? false} disabled={isLoading || readOnly} onChange={() => handleToggleChange(field)} />
{toggleValue && (
<div className="flex w-full gap-4 px-3 pb-3">
<div className="flex flex-col gap-2 w-full">
<Select
label="Ouverture des inscriptions"
options={dateOffsetOptions}
value={(startOffset ?? 0).toString()}
onChange={({ value }) => handleOffsetChange(inscriptionHTSStartOffsetField, value)}
disabled={isLoading || readOnly}
isClearable={false}
isSearchable={false}
closeMenuOnSelect
hideSelectedOptions
placeholder={`J${(startOffset ?? 0) >= 0 ? "+" : ""}${startOffset ?? 0}`}
/>
{htsInscriptionDatesCalculation && <span className="text-gray-500">Début: {htsInscriptionDatesCalculation.startDate}</span>}
</div>
<div className="flex flex-col gap-2 w-full">
<Select
label="Fermeture des instructions"
options={dateOffsetOptions}
value={(endOffset ?? 0).toString()}
onChange={({ value }) => handleOffsetChange(inscriptionHTSEndOffsetField, value)}
disabled={isLoading || readOnly}
isClearable={false}
isSearchable={false}
isMulti={false}
closeMenuOnSelect
hideSelectedOptions
placeholder={`J${(endOffset ?? 0) >= 0 ? "+" : ""}${endOffset ?? 0}`}
/>
{htsInscriptionDatesCalculation && <span className="text-gray-500">Fin: {htsInscriptionDatesCalculation.endDate}</span>}
</div>
</div>
)}
</div>
);
})}
</>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
async up(db) {
await db.collection("cohorts").updateMany(
{},
{
$set: {
inscriptionHTSStartOffsetForReferentRegion: 0,
inscriptionHTSEndOffsetForReferentRegion: 0,
inscriptionHTSStartOffsetForReferentDepartment: 0,
inscriptionHTSEndOffsetForReferentDepartment: 0,
},
},
);
},

async down(db) {
await db.collection("cohorts").updateMany(
{},
{
$unset: {
inscriptionHTSStartOffsetForReferentRegion: "",
inscriptionHTSEndOffsetForReferentRegion: "",
inscriptionHTSStartOffsetForReferentDepartment: "",
inscriptionHTSEndOffsetForReferentDepartment: "",
},
},
);
},
};
4 changes: 4 additions & 0 deletions api/src/cohort/cohortValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export const validateCohortDto = (dto: UpdateCohortDto): Joi.ValidationResult<Up
inscriptionOpenForReferentRegion: Joi.boolean().default(false),
inscriptionOpenForReferentDepartment: Joi.boolean().default(false),
inscriptionOpenForAdministrateurCle: Joi.boolean().default(false),
inscriptionHTSStartOffsetForReferentRegion: Joi.number().default(0),
inscriptionHTSEndOffsetForReferentRegion: Joi.number().default(0),
inscriptionHTSStartOffsetForReferentDepartment: Joi.number().default(0),
inscriptionHTSEndOffsetForReferentDepartment: Joi.number().default(0),
// --
inscriptionModificationEndDate: Joi.date(),
instructionEndDate: Joi.date().required(),
Expand Down
2 changes: 1 addition & 1 deletion packages/ds/src/admin/form/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default function SelectButton(props: SelectProps) {
/>
) : (
<Select
placeholder={placeholder}
{...(placeholder ? { placeholder } : { placeholder: value })}
options={options}
noOptionsMessage={() => noOptionsMessage}
defaultValue={defaultValue}
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/dto/cohortDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
youngCheckinForRegionReferent?: boolean;
youngCheckinForDepartmentReferent?: boolean;
daysToValidate?: number | null;
uselessInformation?: Record<string, any> | null;

Check warning on line 65 in packages/lib/src/dto/cohortDto.ts

View workflow job for this annotation

GitHub Actions / run_tests_lib / test

Unexpected any. Specify a different type
validationDate?: Date | null;
validationDateForTerminaleGrade?: Date | null;
daysToValidateForTerminalGrade?: number | null;
Expand All @@ -76,6 +76,10 @@
inscriptionOpenForReferentRegion?: boolean;
inscriptionOpenForReferentDepartment?: boolean;
inscriptionOpenForAdministrateurCle?: boolean;
inscriptionHTSStartOffsetForReferentRegion?: number;
inscriptionHTSEndOffsetForReferentRegion?: number;
inscriptionHTSStartOffsetForReferentDepartment?: number;
inscriptionHTSEndOffsetForReferentDepartment?: number;
};

export type UpdateCohortDto = Omit<CohortDto, "name" | "type" | "snuId" | "eligibility">;
Expand Down
Loading
Loading