From a8a3aa17cec8fbbfe3e4d4cf4df0331e0b5b80d8 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 31 Mar 2019 08:45:51 +0200 Subject: [PATCH] Perform undo by deleting the erroneous database entry --- lib/Travelynx.pm | 81 ++++++++++------------- lib/Travelynx/Controller/Traveling.pm | 2 +- public/static/js/travelynx-actions.js | 1 + public/static/js/travelynx-actions.min.js | 2 +- public/static/{v3 => v5} | 0 templates/departures.html.ep | 4 +- templates/landingpage.html.ep | 8 +-- templates/layouts/default.html.ep | 2 +- 8 files changed, 46 insertions(+), 54 deletions(-) rename public/static/{v3 => v5} (100%) diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index f453f56..ae50815 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -269,7 +269,8 @@ sub startup { return $self->app->dbh->prepare( qq{ - select action_id, extract(epoch from action_time), stations.ds100, stations.name, + select user_actions.id, action_id, extract(epoch from action_time), + stations.ds100, stations.name, train_type, train_line, train_no, train_id, extract(epoch from sched_time), extract(epoch from real_time), route, messages @@ -287,7 +288,8 @@ sub startup { return $self->app->dbh->prepare( qq{ - select action_id, extract(epoch from action_time), stations.ds100, stations.name, + select user_actions.id, action_id, extract(epoch from action_time), + stations.ds100, stations.name, train_type, train_line, train_no, train_id, extract(epoch from sched_time), extract(epoch from real_time), route, messages @@ -309,7 +311,8 @@ sub startup { # lack both sched_time and real_time. return $self->app->dbh->prepare( qq{ - select action_id, extract(epoch from action_time), stations.ds100, stations.name, + select user_actions.id, action_id, extract(epoch from action_time), + stations.ds100, stations.name, train_type, train_line, train_no, train_id, extract(epoch from sched_time), extract(epoch from real_time), route, messages @@ -329,7 +332,8 @@ sub startup { return $self->app->dbh->prepare( qq{ - select action_id, extract(epoch from action_time), stations.ds100, stations.name, + select user_actions.id, action_id, extract(epoch from action_time), + stations.ds100, stations.name, train_type, train_line, train_no, train_id, extract(epoch from sched_time), extract(epoch from real_time), route, messages @@ -483,12 +487,8 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} return $self->app->dbh->prepare( qq{ - insert into user_actions ( - user_id, action_id, action_time - ) values ( - ?, $self->app->action_type->{undo}, to_timestamp(?) - ) - } + delete from user_actions where id = ? + } ); }, ); @@ -609,36 +609,24 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} $self->helper( 'undo' => sub { - my ($self) = @_; + my ($self, $action_id) = @_; - my $uid = $self->current_user->{id}; - $self->app->get_last_actions_query->execute($uid); - my $rows = $self->app->get_last_actions_query->fetchall_arrayref; + my $status = $self->get_user_status; - if ( @{$rows} and $rows->[0][0] == $self->app->action_type->{undo} ) - { - return 'Nested undo (undoing an undo) is not supported'; + if ($action_id < 1 or $status->{action_id} != $action_id) { + return "Invalid action ID: $action_id != $status->{action_id}. Note that you can only undo your latest action."; } - if ( @{$rows} > 1 - and $rows->[1][0] == $self->app->action_type->{undo} ) - { - return 'Repeated undo is not supported'; - } + my $success = $self->app->undo_query->execute($action_id); - my $success = $self->app->undo_query->execute( - $self->current_user->{id}, - DateTime->now( time_zone => 'Europe/Berlin' )->epoch, - ); - - if ( defined $success ) { + if (defined $success) { return; } else { my $uid = $self->current_user->{id}; - my $err = $self->app->action_query->errstr; - $self->app->log->error("Undo($uid): INSERT failed: $err"); - return 'INSERT failed: ' . $err; + my $err = $self->app->undo_query->errstr; + $self->app->log->error("Undo($uid): DELETE failed: $err"); + return 'DELETE failed: ' . $err; } } ); @@ -962,7 +950,7 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} while ( my @row = $query->fetchrow_array ) { my ( - $action, $raw_ts, $ds100, + $action_id, $action, $raw_ts, $ds100, $name, $train_type, $train_line, $train_no, $train_id, $raw_sched_ts, $raw_real_ts, $raw_route, $raw_messages @@ -977,6 +965,7 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} push( @travels, { + ids => [undef, $action_id], to_name => $name, sched_arrival => epoch_to_dt($raw_sched_ts), rt_arrival => epoch_to_dt($raw_real_ts), @@ -1004,6 +993,7 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} ) { my $ref = $travels[-1]; + $ref->{ids}->[0] = $action_id; $ref->{from_name} = $name; $ref->{completed} = 1; $ref->{sched_departure} = epoch_to_dt($raw_sched_ts); @@ -1091,16 +1081,16 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} my @cols = @{ $rows->[0] }; if ( @{$rows} > 2 - and $rows->[0][0] == $self->app->action_type->{undo} ) + and $rows->[0][1] == $self->app->action_type->{undo} ) { @cols = @{ $rows->[2] }; } - my $action_ts = epoch_to_dt( $cols[1] ); - my $sched_ts = epoch_to_dt( $cols[8] ); - my $real_ts = epoch_to_dt( $cols[9] ); - my $checkin_station_name = $cols[3]; - my @route = split( qr{[|]}, $cols[10] // q{} ); + my $action_ts = epoch_to_dt( $cols[2] ); + my $sched_ts = epoch_to_dt( $cols[9] ); + my $real_ts = epoch_to_dt( $cols[10] ); + my $checkin_station_name = $cols[4]; + my @route = split( qr{[|]}, $cols[11] // q{} ); my @route_after; my $is_after = 0; for my $station (@route) { @@ -1114,19 +1104,20 @@ qq{select * from pending_mails where email = ? and num_tries > 1;} } return { checked_in => - ( $cols[0] == $self->app->action_type->{checkin} ), + ( $cols[1] == $self->app->action_type->{checkin} ), cancelled => - ( $cols[0] == $self->app->action_type->{cancelled_from} ), + ( $cols[1] == $self->app->action_type->{cancelled_from} ), timestamp => $action_ts, timestamp_delta => $now->epoch - $action_ts->epoch, + action_id => $cols[0], sched_ts => $sched_ts, real_ts => $real_ts, - station_ds100 => $cols[2], + station_ds100 => $cols[3], station_name => $checkin_station_name, - train_type => $cols[4], - train_line => $cols[5], - train_no => $cols[6], - train_id => $cols[7], + train_type => $cols[5], + train_line => $cols[6], + train_no => $cols[7], + train_id => $cols[8], route => \@route, route_after => \@route_after, }; diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 3f5a181..c3ce11e 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -117,7 +117,7 @@ sub log_action { } } elsif ( $params->{action} eq 'undo' ) { - my $error = $self->undo; + my $error = $self->undo( $params->{undo_id} ); if ($error) { $self->render( json => { diff --git a/public/static/js/travelynx-actions.js b/public/static/js/travelynx-actions.js index d47df77..8235889 100644 --- a/public/static/js/travelynx-actions.js +++ b/public/static/js/travelynx-actions.js @@ -43,6 +43,7 @@ $(document).ready(function() { var link = $(this); var req = { action: 'undo', + undo_id: link.data('id'), }; tvly_run(link, req, '/'); }); diff --git a/public/static/js/travelynx-actions.min.js b/public/static/js/travelynx-actions.min.js index 4fe0e76..2999816 100644 --- a/public/static/js/travelynx-actions.min.js +++ b/public/static/js/travelynx-actions.min.js @@ -1 +1 @@ -function tvly_run(n,t,a,c){var i='error',o=$('
');n.hide(),n.after(o),$.post("/action",t,function(t){t.success?$(location).attr("href",a):(M.toast({html:i+" "+t.error}),o.remove(),c&&c(),n.append(" "+i),n.show())})}$(document).ready(function(){$(".action-checkin").click(function(){var t=$(this);tvly_run(t,{action:"checkin",station:t.data("station"),train:t.data("train")},"/")}),$(".action-checkout").click(function(){var t=$(this),n={action:"checkout",station:t.data("station"),force:t.data("force")};tvly_run(t,n,"/s/"+n.station,function(){t.append(" – Ohne Echtzeitdaten auschecken?"),t.data("force",!0)})}),$(".action-undo").click(function(){tvly_run($(this),{action:"undo"},"/")}),$(".action-cancelled-from").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_from",station:t.data("station"),train:t.data("train")},"/")}),$(".action-cancelled-to").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_to",station:t.data("station"),force:!0},"/")})}); +function tvly_run(t,a,n,c){var i='error',o=$('
');t.hide(),t.after(o),$.post("/action",a,function(a){a.success?$(location).attr("href",n):(M.toast({html:i+" "+a.error}),o.remove(),c&&c(),t.append(" "+i),t.show())})}$(document).ready(function(){$(".action-checkin").click(function(){var t=$(this);tvly_run(t,{action:"checkin",station:t.data("station"),train:t.data("train")},"/")}),$(".action-checkout").click(function(){var t=$(this),a={action:"checkout",station:t.data("station"),force:t.data("force")};tvly_run(t,a,"/s/"+a.station,function(){t.append(" – Ohne Echtzeitdaten auschecken?"),t.data("force",!0)})}),$(".action-undo").click(function(){var t=$(this);tvly_run(t,{action:"undo",undo_id:t.data("id")},"/")}),$(".action-cancelled-from").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_from",station:t.data("station"),train:t.data("train")},"/")}),$(".action-cancelled-to").click(function(){var t=$(this);tvly_run(t,{action:"cancelled_to",station:t.data("station"),force:!0},"/")})}); diff --git a/public/static/v3 b/public/static/v5 similarity index 100% rename from public/static/v3 rename to public/static/v5 diff --git a/templates/departures.html.ep b/templates/departures.html.ep index 00c1c0e..1240d1d 100644 --- a/templates/departures.html.ep +++ b/templates/departures.html.ep @@ -15,7 +15,7 @@ % } - % elsif ($status->{timestamp_delta} < 600) { + % elsif ($status->{timestamp_delta} < 180) {
Ausgecheckt @@ -23,7 +23,7 @@ bis <%= $status->{station_name} %>

diff --git a/templates/landingpage.html.ep b/templates/landingpage.html.ep index 83ad2a1..dc559f9 100644 --- a/templates/landingpage.html.ep +++ b/templates/landingpage.html.ep @@ -21,8 +21,8 @@

Du bist gerade eingecheckt in <%= $status->{train_type} %> <%= $status->{train_no} %> ab <%= $status->{station_name} %>. - % if ($status->{timestamp_delta} < 3600) { - undo Rückgängig + % if ($status->{timestamp_delta} < 180) { + undo Rückgängig % }

Bei Ankunft: Station auswählen zum Auschecken.

@@ -44,8 +44,8 @@

Prinzipiell wärest du nun eingecheckt in <%= $status->{train_type} %> <%= $status->{train_no} %> ab <%= $status->{station_name} %>, doch dieser Zug fällt aus. - % if ($status->{timestamp_delta} < 3600) { - undo Checkinversuch rückgängig + % if ($status->{timestamp_delta} < 180) { + undo Checkinversuch rückgängig % }

Falls du den Zugausfall z.B. für ein Fahrgastrechteformular diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index eadc143..51bc25f 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -5,7 +5,7 @@ - % my $av = 'v4'; # asset version + % my $av = 'v5'; # asset version %= stylesheet "/static/${av}/css/materialize.min.css" %= stylesheet "/static/${av}/css/material-icons.css" %= stylesheet "/static/${av}/css/local.css"