Skip to content
Closed
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 @@ -6,6 +6,7 @@ import {
type SearchResultItemOperationalPoint,
type TrainSchedule,
type MacroNodeForm,
type PathItemLocation,
} from 'common/api/osrdEditoastApi';
import {
createPacedTrain,
Expand Down Expand Up @@ -486,6 +487,46 @@ const handleDeleteTimetableItem = async (
state.timetableItemIdByNgeId.delete(trainrunId);
};

const comparePathItemLocations = async (
pathItemLocation1: PathItemLocation,
pathItemLocation2: PathItemLocation,
infraId: number,
dispatch: AppDispatch
) => {
if ('track' in pathItemLocation1 || 'track' in pathItemLocation2) {
return (
'track' in pathItemLocation1 &&
'track' in pathItemLocation2 &&
pathItemLocation1.track === pathItemLocation2.track &&
pathItemLocation1.offset === pathItemLocation2.offset
);
}
const ops = await dispatch(
osrdEditoastApi.endpoints.matchAllOperationalPoints.initiate({
infraId,
opRefs: [pathItemLocation1, pathItemLocation2],
})
).unwrap();
if (ops[0][0] && ops[1][0]) return ops[0][0].id === ops[1][0].id;
if ('trigram' in pathItemLocation1 && 'trigram' in pathItemLocation2) {
return (
pathItemLocation1.trigram === pathItemLocation2.trigram &&
pathItemLocation1.secondary_code === pathItemLocation2.secondary_code
);
}
if ('uic' in pathItemLocation1 && 'uic' in pathItemLocation2) {
return (
pathItemLocation1.uic === pathItemLocation2.uic &&
pathItemLocation1.secondary_code === pathItemLocation2.secondary_code
);
}
return (
'operational_point' in pathItemLocation1 &&
'operational_point' in pathItemLocation2 &&
pathItemLocation1.operational_point === pathItemLocation2.operational_point
);
};

/**
* Handle the following cases:
* - if the TimetableItem is initially a PacedTrain and the frequency is still PacedTrain (`paced` time window is keep identical and interval to corresponding TrainrunFrequency)
Expand Down Expand Up @@ -514,7 +555,7 @@ const handleUpdateTimetableItem = async ({
addDeletedTimetableItemIds: (timetableItemIds: TimetableItemId[]) => void;
}) => {
const timetableItemIds = state.timetableItemIdByNgeId.get(trainrun.id)!;
const oldForwardTimetableItem = await fetchTimetableItem(timetableItemIds[0], dispatch);
let oldForwardTimetableItem = await fetchTimetableItem(timetableItemIds[0], dispatch);
const { labels, startDate, trainrunSections } = generateTrainrunProperties(
netzgrafikDto,
trainrun,
Expand All @@ -527,6 +568,46 @@ const handleUpdateTimetableItem = async ({
);
await populateSecondaryCodesInPath(forwardPath, infraId, dispatch);

if (trainrun.direction === 'one_way') {
if (timetableItemIds[1]) {
const firstNewStepMatchOldForward = await comparePathItemLocations(
oldForwardTimetableItem.path[0],
forwardPath[0],
infraId,
dispatch
);
const indexToDelete = firstNewStepMatchOldForward ? 1 : 0;
if (!firstNewStepMatchOldForward)
oldForwardTimetableItem = await fetchTimetableItem(timetableItemIds[1], dispatch);

if (isPacedTrainId(oldForwardTimetableItem.id)) {
await dispatch(
osrdEditoastApi.endpoints.postRoundTripsPacedTrains.initiate({
roundTrips: {
one_ways: [extractEditoastIdFromPacedTrainId(oldForwardTimetableItem.id)],
},
})
).unwrap();
} else {
await dispatch(
osrdEditoastApi.endpoints.postRoundTripsTrainSchedules.initiate({
roundTrips: {
one_ways: [extractEditoastIdFromTrainScheduleId(oldForwardTimetableItem.id)],
},
})
).unwrap();
}
await deleteTimetableItemById(
timetableItemIds[indexToDelete]!,
dispatch,
addDeletedTimetableItemIds
);
}

state.timetableItemIdByNgeId.set(trainrun.id, [oldForwardTimetableItem.id, null]);
return;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { id, ...timetableItemBase } = oldForwardTimetableItem;

Expand Down Expand Up @@ -585,34 +666,6 @@ const handleUpdateTimetableItem = async ({
);
}

if (trainrun.direction === 'one_way') {
if (timetableItemIds[1]) {
// NGE always selects the forward trip by default when going from round trip to one way trip,
// thus the trip that needs to be deleted is always the return trip
if (isPacedTrainId(oldForwardTimetableItem.id)) {
await dispatch(
osrdEditoastApi.endpoints.postRoundTripsPacedTrains.initiate({
roundTrips: {
one_ways: [extractEditoastIdFromPacedTrainId(oldForwardTimetableItem.id)],
},
})
).unwrap();
} else {
await dispatch(
osrdEditoastApi.endpoints.postRoundTripsTrainSchedules.initiate({
roundTrips: {
one_ways: [extractEditoastIdFromTrainScheduleId(oldForwardTimetableItem.id)],
},
})
).unwrap();
}
await deleteTimetableItemById(timetableItemIds[1], dispatch, addDeletedTimetableItemIds);
}

state.timetableItemIdByNgeId.set(trainrun.id, [oldForwardTimetableItem.id, null]);
return;
}

const { path: returnPath, schedule: returnSchedule } = generatePathAndSchedule(
trainrunSections,
netzgrafikDto.nodes,
Expand Down
Loading