Skip to content

Commit

Permalink
Merge pull request #14 from mission-apprentissage/fix/erreur-sur-les-…
Browse files Browse the repository at this point in the history
…dates

fix: mise a jour du format de date
  • Loading branch information
Pomarom authored Apr 25, 2024
2 parents 17c3773 + 654e275 commit e58c70e
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 84 deletions.
8 changes: 5 additions & 3 deletions shared/helpers/cerfa/domains/contrat/shouldShowSmc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { differenceInYears, parseISO } from "date-fns";
import { differenceInYears } from "date-fns";
import { CerfaForm } from "shared/helpers/cerfa/types/cerfa.types";

import { customParseISODate } from "../../utils/dates";

export const shouldShowSmc = ({ values }: CerfaForm) => {
if (!values.contrat.dateFinContrat || !values.apprenti.dateNaissance) return false;
const dateFinContrat = parseISO(values.contrat.dateFinContrat);
const dateNaissance = parseISO(values.apprenti.dateNaissance);
const dateFinContrat = customParseISODate(values.contrat.dateFinContrat);
const dateNaissance = customParseISODate(values.apprenti.dateNaissance);

return differenceInYears(dateFinContrat, dateNaissance) >= 21;
};
6 changes: 4 additions & 2 deletions shared/helpers/cerfa/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { differenceInYears, parseISO } from "date-fns";

export const caclAgeAtDate = (dateNaissanceString: string, dateString: string) => {
const dateNaissance = parseISO(dateNaissanceString);
const dateObj = parseISO(dateString);
const dateNaissance = customParseISODate(dateNaissanceString);
const dateObj = customParseISODate(dateString);

// Note: differenceInYears already gives a whole number
const years = differenceInYears(dateObj, dateNaissance);
Expand All @@ -13,3 +13,5 @@ export const caclAgeAtDate = (dateNaissanceString: string, dateString: string) =
exactAge: age, // since differenceInYears already returns a whole number
};
};

export const customParseISODate = (str: string) => parseISO(str.split("/").reverse().join("-"));
2 changes: 0 additions & 2 deletions ui/app/(application)/cerfa/components/CerfaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ const CerfaForm: FC = () => {
const values = methods.watch();
const errors = methods.formState.errors;

console.log({ values, errors });

const onSubmit: SubmitHandler<any> = (values) => {
console.log("submitted", { values, errors });
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { fr } from "@codegouvfr/react-dsfr";
import { Notice } from "@codegouvfr/react-dsfr/Notice";
import { Box, Typography } from "@mui/material";
import { format, getDate, parseISO, setDate, subMonths } from "date-fns";
import { format, getDate, setDate, subMonths } from "date-fns";
import React from "react";
import { useFormContext } from "react-hook-form";
import { useRecoilState } from "recoil";
import { shouldShowRemunerationsAnnuelles } from "shared/helpers/cerfa/domains/contrat/shouldShowRemunerationsAnnuelles";
import { customParseISODate } from "shared/helpers/cerfa/utils/dates";
import { formatDate } from "utils/date.utils";

import InformationMessage from "../../../../components/InformationMessage";
Expand Down Expand Up @@ -142,7 +143,7 @@ En savoir plus sur les situations spécifiques sur le [site du Ministère du Tra
(() => {
const age = annee.newSeuil;
const applicationDate = formatDate(annee.dateDebut);
const birthday = getDate(parseISO(apprentiDateNaissance));
const birthday = getDate(customParseISODate(apprentiDateNaissance));
const birthdayDate = birthday
? format(setDate(subMonths(new Date(annee.dateDebut), 1), birthday), "dd/MM/yyyy")
: undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import {
addYears,
format,
formatISO,
getMonth,
getYear,
isBefore,
parseISO,
setDate,
setMonth,
subDays,
} from "date-fns";

import { addYears, format, formatISO, getMonth, getYear, isBefore, setDate, setMonth, subDays } from "date-fns";
import { customParseISODate } from "shared/helpers/cerfa/utils/dates";
const LATEST_SMIC_YEAR = 2022;

const SMICs = {
Expand All @@ -23,9 +12,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.86,
dateEntreeEnVigueur: "01/05/2022",
dateEntreeEnVigueurObj: parseISO("01/05/2022"),
dateEntreeEnVigueurObj: customParseISODate("01/05/2022"),
dateParutionJo: "20/04/2022",
dateParutionJoObj: parseISO("20/04/2022"),
dateParutionJoObj: customParseISODate("20/04/2022"),
exceptions: {
976: {
departement: 976,
Expand All @@ -43,9 +32,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.76,
dateEntreeEnVigueur: "01/01/2022",
dateEntreeEnVigueurObj: parseISO("01/01/2022"),
dateEntreeEnVigueurObj: customParseISODate("01/01/2022"),
dateParutionJo: "22/12/2021",
dateParutionJoObj: parseISO("22/12/2021"),
dateParutionJoObj: customParseISODate("22/12/2021"),
exceptions: {
976: {
departement: 976,
Expand All @@ -67,9 +56,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.73,
dateEntreeEnVigueur: "01/10/2021",
dateEntreeEnVigueurObj: parseISO("01/10/2021"),
dateEntreeEnVigueurObj: customParseISODate("01/10/2021"),
dateParutionJo: "30/09/2021",
dateParutionJoObj: parseISO("30/09/2021"),
dateParutionJoObj: customParseISODate("30/09/2021"),
exceptions: {
976: {
departement: 976,
Expand All @@ -87,9 +76,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.65,
dateEntreeEnVigueur: "01/01/2021",
dateEntreeEnVigueurObj: parseISO("01/01/2021"),
dateEntreeEnVigueurObj: customParseISODate("01/01/2021"),
dateParutionJo: "17/12/2020",
dateParutionJoObj: parseISO("17/12/2020"),
dateParutionJoObj: customParseISODate("17/12/2020"),
exceptions: {
976: {
departement: 976,
Expand All @@ -111,9 +100,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.65,
dateEntreeEnVigueur: "01/01/2020",
dateEntreeEnVigueurObj: parseISO("01/01/2020"),
dateEntreeEnVigueurObj: customParseISODate("01/01/2020"),
dateParutionJo: "19/12/2019",
dateParutionJoObj: parseISO("19/12/2019"),
dateParutionJoObj: customParseISODate("19/12/2019"),
exceptions: {
976: {
departement: 976,
Expand All @@ -135,9 +124,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.62,
dateEntreeEnVigueur: "01/01/2019",
dateEntreeEnVigueurObj: parseISO("01/01/2019"),
dateEntreeEnVigueurObj: customParseISODate("01/01/2019"),
dateParutionJo: "20/12/2018",
dateParutionJoObj: parseISO("20/12/2018"),
dateParutionJoObj: customParseISODate("20/12/2018"),
exceptions: {
976: {
departement: 976,
Expand All @@ -159,9 +148,9 @@ const SMICs = {
heuresHebdomadaires: 35,
minimumGaranti: 3.57,
dateEntreeEnVigueur: "01/01/2018",
dateEntreeEnVigueurObj: parseISO("01/01/2018"),
dateEntreeEnVigueurObj: customParseISODate("01/01/2018"),
dateParutionJo: "21/12/2017",
dateParutionJoObj: parseISO("21/12/2017"),
dateParutionJoObj: customParseISODate("21/12/2017"),
exceptions: {
976: {
departement: 976,
Expand Down Expand Up @@ -192,9 +181,9 @@ const ceilUp = (x: number) => Math.ceil(x * 100) / 100;

export const buildRemuneration = (data: any) => {
const selectedTaux = data.selectedTaux ?? {};
const dateDebutContrat = parseISO(data.dateDebutContrat);
const dateFinContrat = parseISO(data.dateFinContrat);
const apprentiDateNaissance = parseISO(data.apprentiDateNaissance);
const dateDebutContrat = customParseISODate(data.dateDebutContrat);
const dateFinContrat = customParseISODate(data.dateFinContrat);
const apprentiDateNaissance = customParseISODate(data.apprentiDateNaissance);
const isAnniversaireInLastMonth = getMonth(dateFinContrat) === getMonth(apprentiDateNaissance);

const dateFinA1 = subDays(addYears(dateDebutContrat, 1), 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addMonths, format, getDate, getMonth, getYear, parse, parseISO, setDate, setMonth, subDays } from "date-fns";
import { addMonths, format, getDate, getMonth, getYear, parse, setDate, setMonth, subDays } from "date-fns";
import addYears from "date-fns/addYears";
import { fr } from "date-fns/locale";

import { customParseISODate } from "shared/helpers/cerfa/utils/dates";
const parseDate = (date: string): Date => {
return parse(date, "P", new Date(), { locale: fr });
};
Expand Down Expand Up @@ -275,9 +275,9 @@ const ceilUp = (x: number) => Math.ceil(x * 100) / 100;

export const buildRemuneration = (data: any) => {
const selectedTaux = data.selectedTaux ?? {};
const dateDebutContrat = parseISO(data.dateDebutContrat);
const dateFinContrat = parseISO(data.dateFinContrat);
const apprentiDateNaissance = parseISO(data.apprentiDateNaissance);
const dateDebutContrat = customParseISODate(data.dateDebutContrat);
const dateFinContrat = customParseISODate(data.dateFinContrat);
const apprentiDateNaissance = customParseISODate(data.apprentiDateNaissance);
const isAnniversaireInLastMonth = getMonth(dateFinContrat) === getMonth(apprentiDateNaissance);
const isAnniversaireDebutContrat =
getDate(dateDebutContrat) === getDate(apprentiDateNaissance) &&
Expand Down
39 changes: 19 additions & 20 deletions ui/app/(application)/cerfa/controls/ContratDates.control.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { addMonths, differenceInMonths, isAfter, isBefore, parseISO, startOfDay } from "date-fns";
import { addMonths, differenceInMonths, isAfter, isBefore, startOfDay } from "date-fns";
import { CerfaControl, InformationMessage } from "shared/helpers/cerfa/types/cerfa.types";
import { caclAgeAtDate } from "shared/helpers/cerfa/utils/dates";

import { caclAgeAtDate, customParseISODate } from "shared/helpers/cerfa/utils/dates";
export const ContratDatesControl: CerfaControl[] = [
{
deps: ["contrat.dateDebutContrat"],
Expand Down Expand Up @@ -80,8 +79,8 @@ export const ContratDatesControl: CerfaControl[] = [
contrat: { dateDebutContrat, dateEffetAvenant },
} = values;
if (!dateDebutContrat || !dateEffetAvenant) return;
const dateDebutContratDate = parseISO(dateDebutContrat);
const dateEffetAvenantDate = parseISO(dateEffetAvenant);
const dateDebutContratDate = customParseISODate(dateDebutContrat);
const dateEffetAvenantDate = customParseISODate(dateEffetAvenant);
if (isAfter(dateDebutContratDate, dateEffetAvenantDate)) {
return {
error: "La date de début de contrat ne peut pas être après la date d'effet de l'avenant",
Expand All @@ -97,7 +96,7 @@ export const ContratDatesControl: CerfaControl[] = [
contrat: { dateDebutContrat, dateFinContrat },
} = values;
if (!dateFinContrat) return;
const dateFinContratDate = parseISO(dateFinContrat);
const dateFinContratDate = customParseISODate(dateFinContrat);

if (isBefore(dateFinContratDate, startOfDay(new Date()))) {
return {
Expand Down Expand Up @@ -139,8 +138,8 @@ export const ContratDatesControl: CerfaControl[] = [
contrat: { dateFinContrat, dateEffetAvenant },
} = values;
if (!dateFinContrat || !dateEffetAvenant) return;
const dateFinContratDate = parseISO(dateFinContrat);
const dateEffetAvenantDate = parseISO(dateEffetAvenant);
const dateFinContratDate = customParseISODate(dateFinContrat);
const dateEffetAvenantDate = customParseISODate(dateEffetAvenant);
if (isBefore(dateFinContratDate, dateEffetAvenantDate)) {
return {
error: "La date de fin de contrat ne peut pas être avant la date d'effet de l'avenant",
Expand All @@ -157,8 +156,8 @@ export const ContratDatesControl: CerfaControl[] = [
} = values;

if (!dateSignature || !dateDebutFormation) return;
const dateSignatureDate = parseISO(values.contrat.dateSignature);
const dateDebutFormationDate = parseISO(values.formation.dateDebutFormation);
const dateSignatureDate = customParseISODate(values.contrat.dateSignature);
const dateDebutFormationDate = customParseISODate(values.formation.dateDebutFormation);

if (isBefore(dateDebutFormationDate, dateSignatureDate)) {
return {
Expand All @@ -177,8 +176,8 @@ export const ContratDatesControl: CerfaControl[] = [
} = values;

if (!dateFinContrat || !dateFinFormation) return;
const dateFinContratDate = parseISO(dateFinContrat);
const dateFinFormationDate = parseISO(dateFinFormation);
const dateFinContratDate = customParseISODate(dateFinContrat);
const dateFinFormationDate = customParseISODate(dateFinFormation);
const dateFinFormation3MonthsAfter = addMonths(dateFinFormationDate, 3);

if (isAfter(dateFinContratDate, dateFinFormation3MonthsAfter)) {
Expand All @@ -197,8 +196,8 @@ export const ContratDatesControl: CerfaControl[] = [

if (!dateDebutContrat || !dateFinContrat) return;

const dateDebutContratDate = parseISO(dateDebutContrat);
const dateFinContratDate = parseISO(dateFinContrat);
const dateDebutContratDate = customParseISODate(dateDebutContrat);
const dateFinContratDate = customParseISODate(dateFinContrat);

const dureeContrat = differenceInMonths(dateFinContratDate, dateDebutContratDate);

Expand Down Expand Up @@ -239,7 +238,7 @@ export const ContratDatesControl: CerfaControl[] = [

if (!dateSignature) return {};

const dateSignatureDate = parseISO(dateSignature);
const dateSignatureDate = customParseISODate(dateSignature);

if (dateNaissance) {
const { exactAge: ageApprenti } = caclAgeAtDate(dateNaissance, dateSignature);
Expand All @@ -253,7 +252,7 @@ export const ContratDatesControl: CerfaControl[] = [
}

if (dateDebutContrat) {
const dateDebutContratDate = parseISO(dateDebutContrat);
const dateDebutContratDate = customParseISODate(dateDebutContrat);

if (isAfter(dateSignatureDate, dateDebutContratDate)) {
return {
Expand All @@ -274,10 +273,10 @@ export const ContratDatesControl: CerfaControl[] = [
} = values;

if (!dateDebutFormationPratique) return {};
const dateDebutFormationPratiqueDate = parseISO(dateDebutFormationPratique);
const dateDebutFormationPratiqueDate = customParseISODate(dateDebutFormationPratique);

if (dateDebutContrat) {
const dateDebutContratDate = parseISO(dateDebutContrat);
const dateDebutContratDate = customParseISODate(dateDebutContrat);

if (isBefore(dateDebutFormationPratiqueDate, dateDebutContratDate)) {
return {
Expand All @@ -295,7 +294,7 @@ export const ContratDatesControl: CerfaControl[] = [
}

if (dateSignature) {
const dateSignatureDate = parseISO(dateSignature);
const dateSignatureDate = customParseISODate(dateSignature);

if (isBefore(dateDebutFormationPratiqueDate, dateSignatureDate)) {
return {
Expand All @@ -306,7 +305,7 @@ export const ContratDatesControl: CerfaControl[] = [
}

if (dateFinContrat) {
const dateFinContratDate = parseISO(dateFinContrat);
const dateFinContratDate = customParseISODate(dateFinContrat);

if (isAfter(dateDebutFormationPratiqueDate, dateFinContratDate)) {
return {
Expand Down
6 changes: 3 additions & 3 deletions ui/app/(application)/cerfa/controls/ageApprenti.control.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isFuture, parseISO } from "date-fns";
import { isFuture } from "date-fns";
import { CerfaControl } from "shared/helpers/cerfa/types/cerfa.types";
import { caclAgeAtDate } from "shared/helpers/cerfa/utils/dates";
import { caclAgeAtDate, customParseISODate } from "shared/helpers/cerfa/utils/dates";

export const ageApprentiControl: CerfaControl[] = [
{
deps: ["apprenti.dateNaissance"],
process: ({ values }) => {
const dateNaissance = parseISO(values.apprenti.dateNaissance);
const dateNaissance = customParseISODate(values.apprenti.dateNaissance);

if (isFuture(dateNaissance)) {
return { error: "La date de naissance ne peut pas être dans le futur" };
Expand Down
Loading

0 comments on commit e58c70e

Please sign in to comment.