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

front: fix graou schedules import #10676

Merged
merged 3 commits into from
Feb 7, 2025
Merged
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
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<string, unknown>[]
): ImportedTrainSchedule[] | null {
Expand Down Expand Up @@ -81,7 +112,7 @@ const ImportTrainScheduleConfig = ({
);
return null;
}
return importedTrainSchedules as ImportedTrainSchedule[];
return filterInvalidSteps(importedTrainSchedules as ImportedTrainSchedule[]);
}

function updateTrainSchedules(importedTrainSchedules: ImportedTrainSchedule[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading