From fb83b0ad9fa2a0d745d96b0ee4bba483aa93d3e2 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Sun, 9 Jul 2023 07:16:22 +0200 Subject: checkin: reduce nesting --- lib/Travelynx.pm | 84 +++++++++++++++++++++++++++----------------------------- 1 file 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 ); } ); -- cgit v1.2.3