diff --git a/front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts b/front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts index 0ba83729f9c..39912f08362 100644 --- a/front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts +++ b/front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts @@ -225,6 +225,34 @@ const castNodeToNge = ( ), }); +/** + * Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled. + */ +const trainrunFrequencyFromLabel = (label: string) => { + if (!label.startsWith('frequency::')) return null; + const n = parseInt(label.split('::', 2)[1], 10); + const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n); + return frequency ?? null; +}; + +/** + * NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`). + * Update the current frequency if the new frequency is smaller. + */ +const getFrequencyFromLabels = (labels: string[]): TrainrunFrequency | null => { + let currentFrequency: TrainrunFrequency | null = null; + labels.forEach((label) => { + const newFrequency = trainrunFrequencyFromLabel(label); + if ( + newFrequency && + (!currentFrequency || newFrequency.frequency < currentFrequency.frequency) + ) { + currentFrequency = newFrequency; + } + }); + return currentFrequency; +}; + /** * Load & index the data of the train schedule for the given scenario */ @@ -298,44 +326,25 @@ export const loadAndIndexNge = async ( }; /** - * Translate the train schedule in NGE "trainruns". + * Translate the train schedule in NGE "trainrun". */ const getNgeTrainruns = (state: MacroEditorState, labels: LabelDto[]) => state.trainSchedules .filter((trainSchedule) => trainSchedule.path.length >= 2) - .map((trainSchedule) => { - // NGE trainrun frequency is stored as OSRD labels ('frequency::30' or 'frequency::120') - let trainrunFrequency: TrainrunFrequency | undefined; - if (trainSchedule.labels) { - trainSchedule.labels.forEach((label) => { - if (!label.match(/^frequency::(?!30$|60$|120$)\d+$/)) { - const frequency = parseInt(label.split('::')[1], 10); - const trainrunFrequencyFound = DEFAULT_TRAINRUN_FREQUENCIES.find( - (freq) => freq.frequency === frequency - ); - if ( - trainrunFrequencyFound && - (!trainrunFrequency || trainrunFrequencyFound.frequency < trainrunFrequency.frequency) - ) { - trainrunFrequency = trainrunFrequencyFound; - } - } - }); - } - - return { - id: trainSchedule.id, - name: trainSchedule.train_name, - categoryId: DEFAULT_TRAINRUN_CATEGORY.id, - frequencyId: trainrunFrequency?.id || DEFAULT_TRAINRUN_FREQUENCY.id, - trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id, - labelIds: (trainSchedule.labels || []) - .filter((l) => l.match(/^frequency::(?!30$|60$|120$)\d+$/)) - .map((l) => - labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id) - ), - }; - }); + .map((trainSchedule) => ({ + id: trainSchedule.id, + name: trainSchedule.train_name, + categoryId: DEFAULT_TRAINRUN_CATEGORY.id, + frequencyId: + getFrequencyFromLabels(trainSchedule.labels || [])?.id ?? DEFAULT_TRAINRUN_FREQUENCY.id, + trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id, + labelIds: (trainSchedule.labels || []) + // we keep only not handled frequencies as labels to be not redundant + .filter((l) => trainrunFrequencyFromLabel(l) === null) + .map((l) => + labels.findIndex((e) => e.label === l && e.labelGroupId === TRAINRUN_LABEL_GROUP.id) + ), + })); /** * Translate the train schedule in NGE "trainrunSection" & "nodes".