diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-09 07:16:22 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-09 07:16:22 +0200 |
commit | fb83b0ad9fa2a0d745d96b0ee4bba483aa93d3e2 (patch) | |
tree | a32bbe144701fc76527599e2a7159b13d9ab89ed /lib/Travelynx.pm | |
parent | b5723264a7e88f0d66e3571fe2c8e2d530f98950 (diff) |
checkin: reduce nesting
Diffstat (limited to 'lib/Travelynx.pm')
-rwxr-xr-x | lib/Travelynx.pm | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index ca742b1..6e67237 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -430,56 +430,52 @@ sub startup { if ( $status->{errstr} ) { return ( undef, $status->{errstr} ); } - else { - my ($train) = List::Util::first { $_->train_id eq $train_id } - @{ $status->{results} }; - if ( not defined $train ) { - return ( undef, "Train ${train_id} not found" ); - } - else { - - my $user = $self->get_user_status( $uid, $db ); - if ( $user->{checked_in} or $user->{cancelled} ) { - if ( $user->{train_id} eq $train_id - and $user->{dep_eva} eq $status->{station_eva} ) - { - # checking in twice is harmless - return ( $train, undef ); - } + my ($train) = List::Util::first { $_->train_id eq $train_id } + @{ $status->{results} }; + if ( not defined $train ) { + return ( undef, "Train ${train_id} not found" ); + } - # Otherwise, someone forgot to check out first - $self->checkout( - station => $station, - force => 1, - uid => $uid, - db => $db - ); - } + my $user = $self->get_user_status( $uid, $db ); + if ( $user->{checked_in} or $user->{cancelled} ) { - eval { - $self->in_transit->add( - uid => $uid, - db => $db, - departure_eva => $status->{station_eva}, - train => $train, - route => [ $self->iris->route_diff($train) ], - ); - }; - if ($@) { - $self->app->log->error( - "Checkin($uid): INSERT failed: $@"); - return ( undef, 'INSERT failed: ' . $@ ); - } - if ( not $opt{in_transaction} ) { - - # mustn't be called during a transaction - $self->add_route_timestamps( $uid, $train, 1 ); - $self->run_hook( $uid, 'checkin' ); - } + if ( $user->{train_id} eq $train_id + and $user->{dep_eva} eq $status->{station_eva} ) + { + # checking in twice is harmless return ( $train, undef ); } + + # Otherwise, someone forgot to check out first + $self->checkout( + station => $station, + force => 1, + uid => $uid, + db => $db + ); + } + + eval { + $self->in_transit->add( + uid => $uid, + db => $db, + departure_eva => $status->{station_eva}, + train => $train, + route => [ $self->iris->route_diff($train) ], + ); + }; + if ($@) { + $self->app->log->error("Checkin($uid): INSERT failed: $@"); + return ( undef, 'INSERT failed: ' . $@ ); + } + if ( not $opt{in_transaction} ) { + + # mustn't be called during a transaction + $self->add_route_timestamps( $uid, $train, 1 ); + $self->run_hook( $uid, 'checkin' ); } + return ( $train, undef ); } ); |