@@ -512,6 +512,7 @@ export const handleUpdateTimetableItem = async ({
512512 netzgrafikDto,
513513 trainrun,
514514 tags,
515+ oneWayDirection,
515516 trainScheduleSetId,
516517 infraId,
517518 state,
@@ -522,6 +523,7 @@ export const handleUpdateTimetableItem = async ({
522523 netzgrafikDto : NetzgrafikDto ;
523524 trainrun : TrainrunDto ;
524525 tags : TrainrunUpdateTag [ ] ;
526+ oneWayDirection ?: 'forward' | 'backward' ;
525527 infraId : number ;
526528 trainScheduleSetId : number ;
527529 state : MacroEditorState ;
@@ -530,7 +532,14 @@ export const handleUpdateTimetableItem = async ({
530532 addDeletedTimetableItemIds : ( timetableItemIds : TimetableItemId [ ] ) => void ;
531533} ) => {
532534 const timetableItemIds = state . timetableItemIdByNgeId . get ( trainrun . id ) ! ;
533- const oldForwardTimetableItem = await fetchTimetableItem ( timetableItemIds [ 0 ] , dispatch ) ;
535+ let oldForwardId = timetableItemIds [ 0 ] ;
536+ if ( oneWayDirection === 'backward' ) {
537+ // Case 1: Switching from round trip to the return trip (now forward)
538+ if ( timetableItemIds [ 1 ] ) oldForwardId = timetableItemIds [ 1 ] ;
539+ // Case 2: Inverting the direction of a one way train (we sadly don't store the old return)
540+ else tags . push ( 'nodes' , 'times' ) ;
541+ }
542+ const oldForwardTimetableItem = await fetchTimetableItem ( oldForwardId , dispatch ) ;
534543 const trainrunSections = getContinuousTrainrunSectionsByTrainrunId ( netzgrafikDto , trainrun . id ) ;
535544 const labels = getTrainrunLabels ( netzgrafikDto , trainrun ) ;
536545 const { path : forwardPath , ...forwardSchedule } = generatePathAndSchedule (
@@ -581,10 +590,10 @@ export const handleUpdateTimetableItem = async ({
581590
582591 if ( trainrun . direction === 'one_way' ) {
583592 if ( timetableItemIds [ 1 ] ) {
584- // NGE always selects the forward trip by default when going from round trip to one way trip,
585- // thus the trip that needs to be deleted is always the return trip
586593 await storeRoundTrip ( dispatch , newForwardTimetableItem . id ) ;
587- await deleteTimetableItemById ( timetableItemIds [ 1 ] , dispatch , addDeletedTimetableItemIds ) ;
594+ const oldReturnId =
595+ oneWayDirection !== 'backward' ? timetableItemIds [ 1 ] : timetableItemIds [ 0 ] ;
596+ await deleteTimetableItemById ( oldReturnId , dispatch , addDeletedTimetableItemIds ) ;
588597 }
589598
590599 state . timetableItemIdByNgeId . set ( trainrun . id , [ newForwardTimetableItem . id , null ] ) ;
@@ -707,6 +716,7 @@ export const handleTrainrunOperation = async ({
707716 netzgrafikDto,
708717 trainrun,
709718 tags : trainrunEvent . tags ,
719+ oneWayDirection : trainrunEvent . oneWayDirection ,
710720 trainScheduleSetId,
711721 infraId,
712722 dispatch,
0 commit comments