diff --git a/front/public/locales/en/operationalStudies/importTrainSchedule.json b/front/public/locales/en/operationalStudies/importTrainSchedule.json index b31e39f13c3..91a8f5779b0 100644 --- a/front/public/locales/en/operationalStudies/importTrainSchedule.json +++ b/front/public/locales/en/operationalStudies/importTrainSchedule.json @@ -15,6 +15,10 @@ "errorSameFromTo": "Origin and destination must be different", "errorInvalidFile": "Invalid file" }, + "warningMessages": { + "warning": "Warning", + "warningFilteredStepImport": "Some invalid steps had to be removed to allow import for trains: {{trainNumbers}}" + }, "failure": "Operation failed", "from": "Origin", "import": "Import", diff --git a/front/public/locales/fr/operationalStudies/importTrainSchedule.json b/front/public/locales/fr/operationalStudies/importTrainSchedule.json index 42bcab0ef1c..27f08cdad8a 100644 --- a/front/public/locales/fr/operationalStudies/importTrainSchedule.json +++ b/front/public/locales/fr/operationalStudies/importTrainSchedule.json @@ -15,6 +15,10 @@ "errorSameFromTo": "L'origine et la destination doivent être différentes.", "errorInvalidFile": "Fichier invalide" }, + "warningMessages": { + "warning": "Attention", + "warningFilteredStepImport": "Certaines étapes invalides ont dû être supprimées pour permettre l'importation pour les trains : {{trainNumbers}}" + }, "failure": "Opération échouée", "from": "Origine", "import": "Importation", diff --git a/front/src/modules/trainschedule/components/ImportTrainSchedule/ImportTrainScheduleConfig.tsx b/front/src/modules/trainschedule/components/ImportTrainSchedule/ImportTrainScheduleConfig.tsx index 3e9cbfcfc57..6286e0e40b5 100644 --- a/front/src/modules/trainschedule/components/ImportTrainSchedule/ImportTrainScheduleConfig.tsx +++ b/front/src/modules/trainschedule/components/ImportTrainSchedule/ImportTrainScheduleConfig.tsx @@ -19,7 +19,7 @@ import { ModalContext } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider'; import StationCard from 'common/StationCard'; import UploadFileModal from 'common/uploadFileModal'; import StationSelector from 'modules/trainschedule/components/ImportTrainSchedule/ImportTrainScheduleStationSelector'; -import { setFailure } from 'reducers/main'; +import { setFailure, setWarning } from 'reducers/main'; import { useAppDispatch } from 'store'; import { formatIsoDate } from 'utils/date'; @@ -53,6 +53,37 @@ const ImportTrainScheduleConfig = ({ const dispatch = useAppDispatch(); const { openModal, closeModal } = useContext(ModalContext); + function filterInvalidSteps( + importedTrainSchedules: ImportedTrainSchedule[] + ): ImportedTrainSchedule[] { + const trainNumbersOfModifiedTrains: string[] = []; + + const filteredSchedules = importedTrainSchedules.map((trainSchedule) => { + const filteredSteps = trainSchedule.steps.filter( + (step, i) => + i === 0 || + new Date(step.arrivalTime).getTime() >= + new Date(trainSchedule.steps[i - 1].departureTime).getTime() + ); + if (filteredSteps.length < trainSchedule.steps.length) { + trainNumbersOfModifiedTrains.push(trainSchedule.trainNumber); + } + return { ...trainSchedule, steps: filteredSteps }; + }); + + if (trainNumbersOfModifiedTrains.length) + dispatch( + setWarning({ + title: t('warningMessages.warning'), + text: t('warningMessages.warningFilteredStepImport', { + trainNumbers: trainNumbersOfModifiedTrains, + }), + }) + ); + + return filteredSchedules; + } + function validateImportedTrainSchedules( importedTrainSchedules: Record[] ): ImportedTrainSchedule[] | null { @@ -81,7 +112,7 @@ const ImportTrainScheduleConfig = ({ ); return null; } - return importedTrainSchedules as ImportedTrainSchedule[]; + return filterInvalidSteps(importedTrainSchedules as ImportedTrainSchedule[]); } function updateTrainSchedules(importedTrainSchedules: ImportedTrainSchedule[]) { diff --git a/front/src/modules/trainschedule/components/ImportTrainSchedule/generateTrainSchedulesPayloads.ts b/front/src/modules/trainschedule/components/ImportTrainSchedule/generateTrainSchedulesPayloads.ts index 06cc096fbd9..5c15563c97b 100644 --- a/front/src/modules/trainschedule/components/ImportTrainSchedule/generateTrainSchedulesPayloads.ts +++ b/front/src/modules/trainschedule/components/ImportTrainSchedule/generateTrainSchedulesPayloads.ts @@ -34,8 +34,9 @@ export function generateTrainSchedulesPayloads( console.error(`Invalid CH code for step ${step.name}`); return acc; // Skip invalid step } - - acc.path.push({ id: stepId, uic: Number(step.uic), secondary_code: step.chCode }); + if (!step.uic && step.trigram) + acc.path.push({ id: stepId, trigram: step.trigram, secondary_code: step.chCode }); + else acc.path.push({ id: stepId, uic: Number(step.uic), secondary_code: step.chCode }); // Skip first step, handle time differences if (index !== 0) {