Skip to content

Commit

Permalink
feat: simulation prevent decimal numbers (#1858)
Browse files Browse the repository at this point in the history
* fix: must discard "" for isNaN

---------

Co-authored-by: Pierre-Olivier Mauguet <[email protected]>
  • Loading branch information
jonat75 and pom421 authored Nov 23, 2023
1 parent 01d55a8 commit 65d2b31
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 231 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export const CSPModeTable = ({ computer, staff }: CSPModeTableProps) => {
const category = categories[categoryIndex];
for (const ageRange of ageRanges) {
if (
funnel.effectifs.csp[category].ageRanges[ageRange].women < 3 ||
funnel.effectifs.csp[category].ageRanges[ageRange].men < 3
(funnel.effectifs.csp[category].ageRanges[ageRange].women || 0) < 3 ||
(funnel.effectifs.csp[category].ageRanges[ageRange].men || 0) < 3
) {
continue;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ export const CSPModeTable = ({ computer, staff }: CSPModeTableProps) => {
}

const totalCategory = [...Object.values(effectifsCspCategory.ageRanges)].reduce(
(acc, ageRange) => acc + ageRange.women + ageRange.men,
(acc, ageRange) => acc + (ageRange.women || 0) + (ageRange.men || 0),
0,
);

Expand All @@ -159,8 +159,8 @@ export const CSPModeTable = ({ computer, staff }: CSPModeTableProps) => {
errors,
register,
firstCols: [csp.women || "-", csp.men || "-"],
menCount: csp.men,
womenCount: csp.women,
menCount: csp.men || 0,
womenCount: csp.women || 0,
});
})(),
})) as [AlternativeTableProps.SubRow, ...AlternativeTableProps.SubRow[]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export const Indic1Form = () => {
category: ageRanges.reduce(
(newAgeGroups, ageRange) => ({
...newAgeGroups,
...(funnel.effectifs!.csp[categoryName].ageRanges[ageRange].women >= 3 &&
funnel.effectifs!.csp[categoryName].ageRanges[ageRange].men >= 3
...((funnel.effectifs!.csp[categoryName].ageRanges[ageRange].women || 0) >= 3 &&
(funnel.effectifs!.csp[categoryName].ageRanges[ageRange].men || 0) >= 3
? {
[ageRange]: {
womenCount: funnel.effectifs!.csp[categoryName].ageRanges[ageRange].women,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const Indic2and3Form = () => {
if (data.calculable && funnel) {
const [totalCspWomen, totalCspMen] = getTotalsCsp(funnel as CreateSimulationDTO);

if (data.raisedCount.women > totalCspWomen) {
if (data.raisedCount.women !== "" && data.raisedCount.women > totalCspWomen) {
ctx.addIssue({
code: z.ZodIssueCode.too_big,
inclusive: true,
Expand All @@ -83,7 +83,7 @@ export const Indic2and3Form = () => {
});
}

if (data.raisedCount.men > totalCspMen) {
if (data.raisedCount.men != "" && data.raisedCount.men > totalCspMen) {
ctx.addIssue({
code: z.ZodIssueCode.too_big,
inclusive: true,
Expand Down Expand Up @@ -245,11 +245,11 @@ export const Indic2and3Form = () => {
stateRelatedMessage={whenCalculableErrors.raisedCount?.women?.message}
nativeInputProps={{
...register("raisedCount.women", {
setValueAs: value => (value === "" ? void 0 : +value),
setValueAs: value => (!isNaN(value) ? parseFloat(value) : ""),
deps: "raisedCount.men",
}),
type: "number",
min: raisedCount && raisedCount.men > 0 ? 0 : 1,
min: raisedCount && raisedCount.men !== "" && raisedCount.men > 0 ? 0 : 1,
max: totalCspWomen,
}}
/>
Expand All @@ -262,11 +262,11 @@ export const Indic2and3Form = () => {
stateRelatedMessage={whenCalculableErrors.raisedCount?.men?.message}
nativeInputProps={{
...register("raisedCount.men", {
setValueAs: value => (value === "" ? void 0 : +value),
setValueAs: value => (!isNaN(value) ? parseFloat(value) : ""),
deps: "raisedCount.women",
}),
type: "number",
min: raisedCount && raisedCount.women > 0 ? 0 : 1,
min: raisedCount && raisedCount.women !== "" && raisedCount.women > 0 ? 0 : 1,
max: totalCspMen,
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export const Indic4Form = () => {
const computableCheck = watch("calculable");
const count = watch("count");

indicateur4Computer.setInput(count ?? { total: 0, raised: 0 });
indicateur4Computer.setInput(
{
total: count?.total || 0,
raised: count?.raised || 0,
} ?? { total: 0, raised: 0 },
);

const canCompute = indicateur4Computer.canCompute();
const computed = indicateur4Computer.compute();
Expand Down Expand Up @@ -229,7 +234,11 @@ export const Indic4Form = () => {
</GridCol>
</Grid>
</Container>
<Indicateur4Note computer={indicateur4Computer} count={count} isValid={isValid} />
<Indicateur4Note
computer={indicateur4Computer}
count={{ total: count?.total || 0, raised: count?.raised || 0 }}
isValid={isValid}
/>
</>
) : (
computableCheck === false && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ export const RecapSimu = () => {
return (
<>
<IndicatorPercentResult result={resultIndicateurQuatre.result} />
<Indicateur4Note noBorder computer={computerIndicateurQuatre} count={count} isValid />
<Indicateur4Note
noBorder
computer={computerIndicateurQuatre}
count={{ total: count.total || 0, raised: count.raised || 0 }}
isValid
/>
</>
);
})()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const computerHelper = (funnel: CreateSimulationDTO) => {
if (isLessThan250) {
if (funnel.indicateur2and3.calculable) {
computerIndicateurDeuxTrois.setInput({
...funnel.indicateur2and3.raisedCount,
men: funnel.indicateur2and3.raisedCount.men || 0,
women: funnel.indicateur2and3.raisedCount.women || 0,
menCount: totalMen,
womenCount: totalWomen,
});
Expand All @@ -62,7 +63,10 @@ export const computerHelper = (funnel: CreateSimulationDTO) => {
}
}
if (funnel.indicateur4.calculable) {
computerIndicateurQuatre.setInput(funnel.indicateur4.count);
computerIndicateurQuatre.setInput({
total: funnel.indicateur4.count.total || 0,
raised: funnel.indicateur4.count.raised || 0,
});
}
computerIndicateurCinq.setInput(funnel.indicateur5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export const getPourcentagesAugmentationPromotionsWithCount = (
...newPourcentages,
[category]: {
menCount: ageRanges.reduce(
(totalCategoryCount, ageRange) => totalCategoryCount + (funnelCsp[category].ageRanges[ageRange].men ?? 0),
(totalCategoryCount, ageRange) => totalCategoryCount + (funnelCsp[category].ageRanges[ageRange].men || 0),
0,
),
womenCount: ageRanges.reduce(
(totalCategoryCount, ageRange) => totalCategoryCount + (funnelCsp[category].ageRanges[ageRange].women ?? 0),
(totalCategoryCount, ageRange) => totalCategoryCount + (funnelCsp[category].ageRanges[ageRange].women || 0),
0,
),
men: pourcentages?.[category]?.men ?? 0,
Expand Down
66 changes: 33 additions & 33 deletions packages/app/src/common/core-domain/dtos/CreateSimulationDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { AgeRange } from "../domain/valueObjects/declaration/AgeRange";
import { CompanyWorkforceRange } from "../domain/valueObjects/declaration/CompanyWorkforceRange";
import { RemunerationsMode } from "../domain/valueObjects/declaration/indicators/RemunerationsMode";

const nonnegativeNanSafe = zodFr
.number()
.nonnegative()
.default(0)
.transform(v => (isNaN(v) ? 0 : v));
const positiveIntOrEmptyString = zodFr
.literal("")
.or(zodFr.number().int("La valeur doit être un entier").nonnegative());

const singleAgeRangeSchema = zodFr.object({
women: nonnegativeNanSafe,
men: nonnegativeNanSafe,
women: positiveIntOrEmptyString,
men: positiveIntOrEmptyString,
});
const ageRangesSchema = zodFr.object({
[AgeRange.Enum.LESS_THAN_30]: singleAgeRangeSchema,
Expand All @@ -41,31 +39,33 @@ const cspAgeRangeNumbers = zodFr.object({

const otherAgeRangesSchema = zodFr
.object({
womenCount: nonnegativeNanSafe,
menCount: nonnegativeNanSafe,
womenSalary: nonnegativeNanSafe,
menSalary: nonnegativeNanSafe,
womenCount: positiveIntOrEmptyString,
menCount: positiveIntOrEmptyString,
womenSalary: positiveIntOrEmptyString,
menSalary: positiveIntOrEmptyString,
})
.superRefine((obj, ctx) => {
if (obj.womenCount >= 3 && obj.menCount >= 3) {
if (obj.womenSalary === 0) {
ctx.addIssue({
path: ["womenSalary"],
code: zodFr.ZodIssueCode.too_small,
minimum: 0,
inclusive: false,
type: "number",
});
}
if (obj.womenCount && obj.menCount) {
if (obj.womenCount >= 3 && obj.menCount >= 3) {
if (obj.womenSalary === 0) {
ctx.addIssue({
path: ["womenSalary"],
code: zodFr.ZodIssueCode.too_small,
minimum: 0,
inclusive: false,
type: "number",
});
}

if (obj.menSalary === 0) {
ctx.addIssue({
path: ["menSalary"],
code: zodFr.ZodIssueCode.too_small,
minimum: 0,
inclusive: false,
type: "number",
});
if (obj.menSalary === 0) {
ctx.addIssue({
path: ["menSalary"],
code: zodFr.ZodIssueCode.too_small,
minimum: 0,
inclusive: false,
type: "number",
});
}
}
}
});
Expand Down Expand Up @@ -141,8 +141,8 @@ export const createSteps = {
calculable: zodFr.literal(true),
raisedCount: zodFr
.object({
women: zodFr.number().nonnegative(),
men: zodFr.number().nonnegative(),
women: positiveIntOrEmptyString,
men: positiveIntOrEmptyString,
})
.refine(({ women, men }) => !(!women && !men), {
message: "Tous les champs ne peuvent pas être à 0 s'il y a eu des augmentations.",
Expand All @@ -158,8 +158,8 @@ export const createSteps = {
calculable: zodFr.literal(true),
count: zodFr
.object({
total: zodFr.number().nonnegative(),
raised: zodFr.number().nonnegative().default(0),
total: positiveIntOrEmptyString,
raised: positiveIntOrEmptyString.default(0),
})
.refine(({ total, raised }) => raised <= total, {
message:
Expand Down

0 comments on commit 65d2b31

Please sign in to comment.