diff options
| author | Daniel Friesel <derf@finalrewind.org> | 2020-02-17 21:13:07 +0100 | 
|---|---|---|
| committer | Daniel Friesel <derf@finalrewind.org> | 2020-02-17 21:13:07 +0100 | 
| commit | cc7439dd5e051904aa03e21a7a50bda106c43ae0 (patch) | |
| tree | 8d0e84931ca4807ca53c61f38e6df7c5700b4433 | |
| parent | a1865fa69e3a90e484e8423e256e1cb969b7e04d (diff) | |
handle departure cancellation after checkin
| -rwxr-xr-x | lib/Travelynx.pm | 41 | ||||
| -rw-r--r-- | lib/Travelynx/Command/work.pm | 39 | ||||
| -rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 4 | ||||
| -rw-r--r-- | templates/departures.html.ep | 7 | 
4 files changed, 90 insertions, 1 deletions
| diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 661572e..92d2973 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -2684,6 +2684,10 @@ sub startup {  			my $db        = $opt{db} //= $self->pg->db;  			my $min_count = $opt{min_count} // 3; +			if ( $opt{destination_name} ) { +				return ( $opt{destination_name} ); +			} +  			my $dest_id = $opt{eva} // $self->get_latest_dest_id(%opt);  			if ( not $dest_id ) { @@ -2730,6 +2734,9 @@ sub startup {  				if ( $use_history & 0x01 ) {  					$eva = $opt{eva};  				} +				elsif ( $opt{destination_name} ) { +					$eva = $opt{eva}; +				}  			}  			else {  				if ( $use_history & 0x02 ) { @@ -3357,6 +3364,38 @@ sub startup {  				}  			)->expand->hash; +			my $latest_cancellation = $db->select( +				'journeys_str', +				'*', +				{ +					user_id => $uid, +				}, +				{ +					order_by => { -desc => 'journey_id' }, +					limit    => 1 +				} +			)->expand->hash; + +			if ( $latest_cancellation and $latest_cancellation->{cancelled} ) { +				if ( my $station +					= $self->app->station_by_eva +					->{ $latest_cancellation->{dep_eva} } ) +				{ +					$latest_cancellation->{dep_ds100} = $station->[0]; +					$latest_cancellation->{dep_name}  = $station->[1]; +				} +				if ( my $station +					= $self->app->station_by_eva +					->{ $latest_cancellation->{arr_eva} } ) +				{ +					$latest_cancellation->{arr_ds100} = $station->[0]; +					$latest_cancellation->{arr_name}  = $station->[1]; +				} +			} +			else { +				$latest_cancellation = undef; +			} +  			if ($latest) {  				my $ts          = $latest->{checkout_ts};  				my $action_time = epoch_to_dt($ts); @@ -3375,6 +3414,7 @@ sub startup {  				return {  					checked_in      => 0,  					cancelled       => 0, +					cancellation    => $latest_cancellation,  					journey_id      => $latest->{journey_id},  					timestamp       => $action_time,  					timestamp_delta => $now->epoch - $action_time->epoch, @@ -3401,6 +3441,7 @@ sub startup {  			return {  				checked_in      => 0,  				cancelled       => 0, +				cancellation    => $latest_cancellation,  				no_journeys_yet => 1,  				timestamp       => epoch_to_dt(0),  				timestamp_delta => $now->epoch, diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 04ea95b..04dbd2d 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -70,7 +70,44 @@ sub run {  						train_no => $train->train_no  					}  				); -				$self->app->add_route_timestamps( $uid, $train, 1 ); +				if ( $train->departure_is_cancelled and $arr ) { + +					# depending on the amount of users in transit, some time may +					# have passed between fetching $entry from the database and +					# now. Ensure that the user is still checked into this train +					# before calling checkout to mark the cancellation. +					if ( +						$db->select( +							'in_transit', +							'count(*) as count', +							{ +								user_id            => $uid, +								train_no           => $train->train_no, +								checkin_station_id => $dep +							} +						)->hash->{count} +					  ) +					{ +						$db->update( +							'in_transit', +							{ +								cancelled => 1, +							}, +							{ +								user_id            => $uid, +								train_no           => $train->train_no, +								checkin_station_id => $dep +							} +						); + +                  # check out (adds a cancelled journey and resets journey state +                  # to checkin +						$self->app->checkout( $arr, 1, $uid ); +					} +				} +				else { +					$self->app->add_route_timestamps( $uid, $train, 1 ); +				}  			}  		};  		if ($@) { diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 7a9ab9e..59466f3 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -133,6 +133,10 @@ sub status_card {  	if ( $status->{checked_in} ) {  		$self->render( '_checked_in', journey => $status );  	} +	elsif ( $status->{cancellation} ) { +		$self->render( '_cancelled_departure', +			journey => $status->{cancellation} ); +	}  	else {  		$self->render( '_checked_out', journey => $status );  	} diff --git a/templates/departures.html.ep b/templates/departures.html.ep index ae0dd2d..706a441 100644 --- a/templates/departures.html.ep +++ b/templates/departures.html.ep @@ -26,6 +26,13 @@  		</div>  	</div>  % } +% elsif ($status->{cancellation} and $station eq $status->{cancellation}{dep_name}) { +	<div class="row"> +		<div class="col s12"> +			%= include '_cancelled_departure', journey => $status->{cancellation}; +		</div> +	</div> +% }  % elsif ($status->{timestamp_delta} < 180) {  	<div class="row">  		<div class="col s12"> | 
