diff options
Diffstat (limited to 'lib/Travelynx/Model/Traewelling.pm')
-rw-r--r-- | lib/Travelynx/Model/Traewelling.pm | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/lib/Travelynx/Model/Traewelling.pm b/lib/Travelynx/Model/Traewelling.pm index a334c1d..25648cc 100644 --- a/lib/Travelynx/Model/Traewelling.pm +++ b/lib/Travelynx/Model/Traewelling.pm @@ -1,6 +1,6 @@ package Travelynx::Model::Traewelling; -# Copyright (C) 2020 Daniel Friesel +# Copyright (C) 2020-2023 Birte Kristina Friesel # # SPDX-License-Identifier: AGPL-3.0-or-later @@ -38,20 +38,18 @@ sub now { sub link { my ( $self, %opt ) = @_; - my $log = [ [ $self->now->epoch, "Erfolgreich angemeldet" ] ]; + my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ]; - my $data = { - log => $log, - expires => $opt{expires}->epoch, - }; + my $data = { log => $log }; my $user_entry = { - user_id => $opt{uid}, - email => $opt{email}, - push_sync => 0, - pull_sync => 0, - token => $opt{token}, - data => JSON->new->encode($data), + user_id => $opt{uid}, + push_sync => 0, + pull_sync => 0, + token => $opt{token}, + refresh_token => $opt{refresh_token}, + expiry => epoch_to_dt( $self->now->epoch + $opt{expires_in} ), + data => JSON->new->encode($data), }; $self->{pg}->db->insert( @@ -59,7 +57,7 @@ sub link { $user_entry, { on_conflict => \ -'(user_id) do update set email = EXCLUDED.email, token = EXCLUDED.token, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null' +'(user_id) do update set token = EXCLUDED.token, refresh_token = EXCLUDED.refresh_token, expiry = EXCLUDED.expiry, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null' } ); @@ -94,20 +92,24 @@ sub unlink { } sub get { - my ( $self, $uid ) = @_; - $uid //= $self->current_user->{id}; + my ( $self, %opt ) = @_; + + my $uid = $opt{uid}; + my $db = $opt{db} // $self->{pg}->db; my $res_h - = $self->{pg}->db->select( 'traewelling_str', '*', { user_id => $uid } ) + = $db->select( 'traewelling_str', '*', { user_id => $uid } ) ->expand->hash; $res_h->{latest_run} = epoch_to_dt( $res_h->{latest_run_ts} ); for my $log_entry ( @{ $res_h->{data}{log} // [] } ) { $log_entry->[0] = epoch_to_dt( $log_entry->[0] ); } - $res_h->{expires_on} = epoch_to_dt( $res_h->{data}{expires} ); + $res_h->{expires_on} + = epoch_to_dt( $res_h->{expiry_ts} // $res_h->{data}{expires} ); - my $expires_in = ( $res_h->{data}{expires} // 0 ) - $self->now->epoch; + my $expires_in = ( $res_h->{expiry_ts} // $res_h->{data}{expires} // 0 ) + - $self->now->epoch; if ( $expires_in < 0 ) { $res_h->{expired} = 1; @@ -211,16 +213,17 @@ sub get_pushable_accounts { my $res = $self->{pg}->db->query( qq{select t.user_id as uid, t.token as token, t.data as data, i.user_data as user_data, - i.checkin_station_id as dep_eva, i.checkout_station_id as arr_eva, + i.dep_eva as dep_eva, i.arr_eva as arr_eva, i.data as journey_data, i.train_type as train_type, i.train_line as train_line, i.train_no as train_no, - extract(epoch from i.checkin_time) as checkin_ts, - extract(epoch from i.sched_departure) as dep_ts, - extract(epoch from i.sched_arrival) as arr_ts + i.checkin_ts as checkin_ts, + i.sched_dep_ts as dep_ts, + i.sched_arr_ts as arr_ts, + i.effective_visibility as visibility from traewelling as t - join in_transit as i on t.user_id = i.user_id + join in_transit_str as i on t.user_id = i.user_id where t.push_sync = True - and i.checkout_station_id is not null + and i.arr_eva is not null and i.cancelled = False } ); |