Skip to content

Commit

Permalink
fix: simulation to declaration in indicator 2 or 3 allows empty strin…
Browse files Browse the repository at this point in the history
…gs (#1941)
  • Loading branch information
jonat75 authored Dec 21, 2023
1 parent 78db67d commit 55c3025
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ import { storePicker } from "@common/utils/zustand";
import { AideSimulationIndicateurDeux } from "@components/aide-simulation/IndicateurDeux";
import { AideSimulationIndicateurTrois } from "@components/aide-simulation/IndicateurTrois";
import { RadioOuiNon } from "@components/RHF/RadioOuiNon";
import { AlternativeTable, type AlternativeTableProps, BackNextButtonsGroup, Box, FormLayout } from "@design-system";
import { SkeletonForm } from "@components/utils/skeleton/SkeletonForm";
import {
AlternativeTable,
type AlternativeTableProps,
BackNextButtonsGroup,
Box,
CenteredContainer,
FormLayout,
} from "@design-system";
import { ClientAnimate } from "@design-system/utils/client/ClientAnimate";
import { zodResolver } from "@hookform/resolvers/zod";
import { isEmpty } from "lodash";
Expand All @@ -28,7 +36,7 @@ import { type FieldErrors, FormProvider, useForm } from "react-hook-form";
import { type z } from "zod";

import { NAVIGATION, simulateurPath } from "../navigation";
import { useSimuFunnelStore } from "../useSimuFunnelStore";
import { useSimuFunnelStore, useSimuFunnelStoreHasHydrated } from "../useSimuFunnelStore";
import { getPourcentagesAugmentationPromotionsWithCount, prepareIndicateurUnComputer } from "../utils";
import { Indicateur2ou3Note } from "./Indicateur2ou3Note";

Expand Down Expand Up @@ -60,6 +68,23 @@ const schemaWithGlobalPourcentageVerification = (indicateur: Indic2or3FormProps[
path: ["root.totalPourcentages"],
});
}
categories.forEach(category => {
const categoryValues = obj.pourcentages[category] ?? null;
if (categoryValues) {
if (categoryValues.women === "")
ctx.addIssue({
code: zodFr.ZodIssueCode.custom,
message: "Ce champs est requis",
path: [`pourcentages.${category}.women`],
});
if (categoryValues.men === "")
ctx.addIssue({
code: zodFr.ZodIssueCode.custom,
message: "Ce champs est requis",
path: [`pourcentages.${category}.men`],
});
}
});
}
});

Expand All @@ -69,7 +94,7 @@ export const Indic2or3Form = ({ indicateur }: Indic2or3FormProps) => {
const router = useRouter();
const [_funnel, saveFunnel] = useStore("funnel", "saveFunnel");
const funnel = _funnel as Partial<CreateSimulationWorkforceRangeMoreThan250DTO> | undefined;
prepareIndicateurUnComputer(indicateur1Computer, funnel as CreateSimulationDTO);
const hydrated = useSimuFunnelStoreHasHydrated();

const computer = indicateur === 2 ? indicateur2Computer : indicateur3Computer;
const indicateurNav = indicateur === 2 ? NAVIGATION.indicateur2 : NAVIGATION.indicateur3;
Expand All @@ -88,9 +113,20 @@ export const Indic2or3Form = ({ indicateur }: Indic2or3FormProps) => {
handleSubmit,
register,
watch,
getValues,
} = methods;

const computableCheck = watch("calculable");
if (!hydrated) {
return (
<CenteredContainer pb="6w">
<SkeletonForm fields={1} />
</CenteredContainer>
);
}

prepareIndicateurUnComputer(indicateur1Computer, funnel as CreateSimulationDTO);

const computableCheck = watch("calculable") || getValues("calculable");
const pourcentages = watch("pourcentages");

let pourcentagesWithCount = undefined as Percentages | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,20 @@ export const simuFunnelToDeclarationDTO = (simulation: CreateSimulationDTO): Dec
dto["augmentations"] = {
estCalculable: "oui",
catégories: {
[CSP.Enum.OUVRIERS]: resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.OUVRIERS)),
[CSP.Enum.EMPLOYES]: resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.EMPLOYES)),
[CSP.Enum.TECHNICIENS_AGENTS_MAITRISES]: resultWithSign(
computerIndicateurDeux.computeGroup(CSP.Enum.TECHNICIENS_AGENTS_MAITRISES),
),
[CSP.Enum.INGENIEURS_CADRES]: resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.INGENIEURS_CADRES)),
[CSP.Enum.OUVRIERS]: computerIndicateurDeux.canComputeGroup(CSP.Enum.OUVRIERS)
? resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.OUVRIERS))
: "",
[CSP.Enum.EMPLOYES]: computerIndicateurDeux.canComputeGroup(CSP.Enum.EMPLOYES)
? resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.EMPLOYES))
: "",
[CSP.Enum.TECHNICIENS_AGENTS_MAITRISES]: computerIndicateurDeux.canComputeGroup(
CSP.Enum.TECHNICIENS_AGENTS_MAITRISES,
)
? resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.TECHNICIENS_AGENTS_MAITRISES))
: "",
[CSP.Enum.INGENIEURS_CADRES]: computerIndicateurDeux.canComputeGroup(CSP.Enum.INGENIEURS_CADRES)
? resultWithSign(computerIndicateurDeux.computeGroup(CSP.Enum.INGENIEURS_CADRES))
: "",
},
note: resultIndicateurDeux.note,
populationFavorable: toFavorablePopulation(resultIndicateurDeux.favorablePopulation), // TODO: Use the FavorablePopulation.Enum instead.
Expand All @@ -112,14 +120,20 @@ export const simuFunnelToDeclarationDTO = (simulation: CreateSimulationDTO): Dec
dto["promotions"] = {
estCalculable: "oui",
catégories: {
[CSP.Enum.OUVRIERS]: resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.OUVRIERS)),
[CSP.Enum.EMPLOYES]: resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.EMPLOYES)),
[CSP.Enum.TECHNICIENS_AGENTS_MAITRISES]: resultWithSign(
computerIndicateurTrois.computeGroup(CSP.Enum.TECHNICIENS_AGENTS_MAITRISES),
),
[CSP.Enum.INGENIEURS_CADRES]: resultWithSign(
computerIndicateurTrois.computeGroup(CSP.Enum.INGENIEURS_CADRES),
),
[CSP.Enum.OUVRIERS]: computerIndicateurTrois.canComputeGroup(CSP.Enum.OUVRIERS)
? resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.OUVRIERS))
: "",
[CSP.Enum.EMPLOYES]: computerIndicateurTrois.canComputeGroup(CSP.Enum.EMPLOYES)
? resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.EMPLOYES))
: "",
[CSP.Enum.TECHNICIENS_AGENTS_MAITRISES]: computerIndicateurTrois.canComputeGroup(
CSP.Enum.TECHNICIENS_AGENTS_MAITRISES,
)
? resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.TECHNICIENS_AGENTS_MAITRISES))
: "",
[CSP.Enum.INGENIEURS_CADRES]: computerIndicateurTrois.canComputeGroup(CSP.Enum.INGENIEURS_CADRES)
? resultWithSign(computerIndicateurTrois.computeGroup(CSP.Enum.INGENIEURS_CADRES))
: "",
},
note: resultIndicateurTrois.note,
populationFavorable: toFavorablePopulation(resultIndicateurTrois.favorablePopulation),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ export class IndicateurDeuxComputer extends AbstractGroupComputer<Percentages, o
}
const category = this.input[categoryName];

if (!category || !(category.menCount >= 10 && category.womenCount >= 10 && category.men && category.women)) {
if (
!category ||
!(
category.menCount >= 10 &&
category.womenCount >= 10 &&
typeof category.men === "number" &&
typeof category.women === "number"
)
) {
return false;
}

Expand Down
20 changes: 14 additions & 6 deletions packages/app/src/common/core-domain/dtos/CreateSimulationDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ const positiveInt = zodFr
.int("La valeur doit être un entier")
.nonnegative({ message: "Le nombre ne peut pas être inférieur à 0" });

const positivePercentageFloat = zodFr
.number({ invalid_type_error: "Le champ est requis" })
.nonnegative("Le pourcentage ne peut pas être inférieur à 0")
.lte(100, "Le pourcentage ne peut pas être supérieur à 100%");
const positivePercentageOrEmptyString = zodFr
.literal("", {
errorMap: () => ({
message: "Le champ est requis",
}),
})
.or(
zodFr
.number({ invalid_type_error: "Le champ est requis" })
.nonnegative("Le pourcentage ne peut pas être inférieur à 0")
.lte(100, "Le pourcentage ne peut pas être supérieur à 100%"),
);

const singleAgeRangeSchema = zodFr.object({
women: positiveIntOrEmptyString,
Expand Down Expand Up @@ -109,8 +117,8 @@ const indicateur2or3 = zodFr.discriminatedUnion("calculable", [
pourcentages: zodFr.record(
zodFr.nativeEnum(CSP.Enum),
zodFr.object({
women: positivePercentageFloat,
men: positivePercentageFloat,
women: positivePercentageOrEmptyString,
men: positivePercentageOrEmptyString,
}),
),
}),
Expand Down

0 comments on commit 55c3025

Please sign in to comment.