From 8173249197be4cc308b6bdea41e0813fa6cba2d6 Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Mon, 23 Jul 2018 15:57:32 +0200 Subject: [PATCH] TT#41022 calllist, faxes, vm: use owner's inherited tz Change-Id: Icf7bb86055b8008e18856579907386137fc67e7d --- lib/NGCP/Panel/Controller/API/CallLists.pm | 8 ++-- .../Panel/Controller/API/CallListsItem.pm | 6 +-- lib/NGCP/Panel/Controller/API/Faxes.pm | 14 +++++-- lib/NGCP/Panel/Controller/API/Voicemails.pm | 10 ++++- lib/NGCP/Panel/Role/API/CallLists.pm | 13 +++--- lib/NGCP/Panel/Role/API/Faxes.pm | 5 ++- lib/NGCP/Panel/Role/API/Voicemails.pm | 9 +++- lib/NGCP/Panel/Utils/API/Calllist.pm | 41 +++++++++++++++++-- 8 files changed, 80 insertions(+), 26 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/CallLists.pm b/lib/NGCP/Panel/Controller/API/CallLists.pm index f8aa27541b..acb21a7fc7 100644 --- a/lib/NGCP/Panel/Controller/API/CallLists.pm +++ b/lib/NGCP/Panel/Controller/API/CallLists.pm @@ -28,6 +28,10 @@ sub query_params { param => 'tz', description => 'Format start_time according to the optional time zone provided here, e.g. Europe/Berlin.', }, + { + param => 'use_owner_tz', + description => 'Format start_time according to the filtered customer\'s/subscribers\'s inherited time zone.', + }, { param => 'subscriber_id', description => 'Filter for calls for a specific subscriber. Either this or customer_id is mandatory if called by admin, reseller or subscriberadmin to filter list down to a specific subscriber in order to properly determine the direction of calls.', @@ -284,10 +288,6 @@ sub GET :Allow { my $rows = $c->request->params->{rows} // 10; my $schema = $c->model('DB'); { - if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone"); - return; - } my $owner = NGCP::Panel::Utils::API::Calllist::get_owner_data($self, $c, $schema); last unless $owner; diff --git a/lib/NGCP/Panel/Controller/API/CallListsItem.pm b/lib/NGCP/Panel/Controller/API/CallListsItem.pm index 9c81d8fc80..5dc4bc8c90 100644 --- a/lib/NGCP/Panel/Controller/API/CallListsItem.pm +++ b/lib/NGCP/Panel/Controller/API/CallListsItem.pm @@ -38,17 +38,13 @@ __PACKAGE__->set_config({ sub GET :Allow { my ($self, $c, $id) = @_; { - if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone"); - return; - } my $schema = $c->model('DB'); last unless $self->valid_id($c, $id); my $owner = NGCP::Panel::Utils::API::Calllist::get_owner_data($self, $c, $schema); last unless $owner; - my $href_data = $owner->{subscriber} ? + my $href_data = $owner->{subscriber} ? "subscriber_id=".$owner->{subscriber}->id : "customer_id=".$owner->{customer}->id; diff --git a/lib/NGCP/Panel/Controller/API/Faxes.pm b/lib/NGCP/Panel/Controller/API/Faxes.pm index b94e16a8a4..3b493ad04c 100644 --- a/lib/NGCP/Panel/Controller/API/Faxes.pm +++ b/lib/NGCP/Panel/Controller/API/Faxes.pm @@ -33,6 +33,14 @@ sub api_description { sub query_params { return [ + { + param => 'tz', + description => 'Format timestamp according to the optional time zone provided here, e.g. Europe/Berlin.', + }, + { + param => 'use_owner_tz', + description => 'Format timestamp according to the filtered customer\'s/subscribers\'s inherited time zone.', + }, { param => 'subscriber_id', description => 'Filter for faxes belonging to a specific subscriber', @@ -47,7 +55,7 @@ sub query_params { }, { param => 'time_from', - description => 'Filter for faxes performed after or at the given time stamp.', + description => 'Filter for faxes performed after or at the given timestamp.', query => { first => sub { my $q = shift; @@ -59,7 +67,7 @@ sub query_params { }, { param => 'time_to', - description => 'Filter for faxes performed before or at the given time stamp.', + description => 'Filter for faxes performed before or at the given timestamp.', query => { first => sub { my $q = shift; @@ -68,7 +76,7 @@ sub query_params { }, second => sub { }, }, - }, + }, { param => 'sid', description => 'Filter for a fax with the specific session id', diff --git a/lib/NGCP/Panel/Controller/API/Voicemails.pm b/lib/NGCP/Panel/Controller/API/Voicemails.pm index 5884b4a06e..904c27dbae 100644 --- a/lib/NGCP/Panel/Controller/API/Voicemails.pm +++ b/lib/NGCP/Panel/Controller/API/Voicemails.pm @@ -20,6 +20,14 @@ sub api_description { sub query_params { return [ + { + param => 'tz', + description => 'Format timestamp according to the optional time zone provided here, e.g. Europe/Berlin.', + }, + { + param => 'use_owner_tz', + description => 'Format timestamp according to the filtered customer\'s/subscribers\'s inherited time zone.', + }, { param => 'subscriber_id', description => 'Filter for voicemails belonging to a specific subscriber', @@ -97,7 +105,7 @@ sub GET :Allow { $hal->resource({ total_count => $total_count, }); - my $response = HTTP::Response->new(HTTP_OK, undef, + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); $c->response->headers($response->headers); $c->response->body($response->content); diff --git a/lib/NGCP/Panel/Role/API/CallLists.pm b/lib/NGCP/Panel/Role/API/CallLists.pm index 407214ebd1..a0902d5e93 100644 --- a/lib/NGCP/Panel/Role/API/CallLists.pm +++ b/lib/NGCP/Panel/Role/API/CallLists.pm @@ -13,6 +13,7 @@ use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::CallList; use NGCP::Panel::Utils::Subscriber; use NGCP::Panel::Utils::CallList qw(); +use NGCP::Panel::Utils::API::Calllist qw(); sub _item_rs { my ($self, $c) = @_; @@ -78,21 +79,17 @@ sub resource_from_item { my $datetime_fmt = DateTime::Format::Strptime->new( pattern => '%F %T', ); - if($c->req->param('tz') && DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { - # valid tz is checked in the controllers' GET already, but just in case - # it passes through via POST or something, then just ignore wrong tz - $item->start_time->set_time_zone($c->req->param('tz')); - } + my $start_time = NGCP::Panel::Utils::API::Calllist::apply_owner_timezone($self,$c,$item->start_time,$owner); - $resource->{start_time} = $datetime_fmt->format_datetime($item->start_time); - $resource->{start_time} .= '.'.$item->start_time->millisecond if $item->start_time->millisecond > 0.0; + $resource->{start_time} = $datetime_fmt->format_datetime($start_time); + $resource->{start_time} .= '.'.$start_time->millisecond if $start_time->millisecond > 0.0; return $resource; } sub get_mandatory_params { my ($self, $c, $href_type, $item, $resource, $params) = @_; my $owner = $c->stash->{owner}; - return $owner->{subscriber} + return $owner->{subscriber} ? { subscriber_id => $owner->{subscriber}->id } : { customer_id => $owner->{customer}->id }; diff --git a/lib/NGCP/Panel/Role/API/Faxes.pm b/lib/NGCP/Panel/Role/API/Faxes.pm index 0d85942053..23f13b908c 100644 --- a/lib/NGCP/Panel/Role/API/Faxes.pm +++ b/lib/NGCP/Panel/Role/API/Faxes.pm @@ -73,7 +73,10 @@ sub resource_from_item { my %resource = (); $resource{id} = int($item->id); - $resource{time} = $datetime_fmt->format_datetime($item->time); + $resource{time} = $datetime_fmt->format_datetime( + NGCP::Panel::Utils::API::Calllist::apply_owner_timezone($self,$c,$item->time, + NGCP::Panel::Utils::API::Calllist::get_owner_data($self,$c, undef, undef, 1) + )); $resource{subscriber_id} = int($subscriber->id); foreach(qw/direction caller callee reason status quality filename/){ $resource{$_} = $item->$_; diff --git a/lib/NGCP/Panel/Role/API/Voicemails.pm b/lib/NGCP/Panel/Role/API/Voicemails.pm index 76d14d2991..543d049e73 100644 --- a/lib/NGCP/Panel/Role/API/Voicemails.pm +++ b/lib/NGCP/Panel/Role/API/Voicemails.pm @@ -11,6 +11,7 @@ use Data::HAL qw(); use Data::HAL::Link qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::Subscriber; +use NGCP::Panel::Utils::API::Calllist qw(); sub _item_rs { my ($self, $c) = @_; @@ -75,10 +76,16 @@ sub hal_from_item { sub resource_from_item { my ($self, $c, $item, $form) = @_; + my $datetime_fmt = DateTime::Format::Strptime->new( + pattern => '%F %T', + ); + my %resource = (); $resource{id} = int($item->id); $resource{duration} = is_int($item->duration) ? int($item->duration) : 0; - $resource{time} = "" . $item->origtime; + $resource{time} = "" . NGCP::Panel::Utils::API::Calllist::apply_owner_timezone($self,$c,$item->origtime, + NGCP::Panel::Utils::API::Calllist::get_owner_data($self,$c, undef, undef, 1) + ); $resource{caller} = $item->callerid; $resource{subscriber_id} = int($item->mailboxuser->provisioning_voip_subscriber->voip_subscriber->id); diff --git a/lib/NGCP/Panel/Utils/API/Calllist.pm b/lib/NGCP/Panel/Utils/API/Calllist.pm index 23fcf6b49b..24b031abca 100644 --- a/lib/NGCP/Panel/Utils/API/Calllist.pm +++ b/lib/NGCP/Panel/Utils/API/Calllist.pm @@ -6,13 +6,15 @@ use HTTP::Status qw(:constants); sub get_owner_data { - my ($self, $c, $schema, $source) = @_; + my ($self, $c, $schema, $source, $optional_for_admin_reseller) = @_; my $ret; $source //= $c->req->params; my $src_subscriber_id = $source->{subscriber_id}; my $src_customer_id = $source->{customer_id}; - + + $schema //= $c->model('DB'); + if($c->user->roles eq "admin" || $c->user->roles eq "reseller") { if($src_subscriber_id) { my $sub = $schema->resultset('voip_subscribers')->find($src_subscriber_id); @@ -42,7 +44,7 @@ sub get_owner_data { subscriber => undef, customer => $cust, }; - } else { + } elsif (not $optional_for_admin_reseller) { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Mandatory parameter 'subscriber_id' or 'customer_id' missing in request"); return; } @@ -82,6 +84,39 @@ sub get_owner_data { return; } } + +sub apply_owner_timezone { + my ($self,$c,$dt,$owner) = @_; + my $result = $dt->clone; + if($c->req->param('tz')) { + if (DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { + # valid tz is checked in the controllers' GET already, but just in case + # it passes through via POST or something, then just ignore wrong tz + $result->set_time_zone($c->req->param('tz')); + } else { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone"); + return; + } + } elsif ($owner and $c->req->param('use_owner_tz')) { + my $tz; + my $sub = $owner->{subscriber}; + my $cust = $owner->{customer}; + if ($owner->{subscriber}) { + $tz = $c->model('DB')->resultset('voip_subscriber_timezone')->search_rs({ + subscriber_id => $owner->{subscriber}->id + })->first; + } elsif ($owner->{customer}) { + $tz = $c->model('DB')->resultset('contract_timezone')->search_rs({ + contract_id => $owner->{customer}->id + })->first; + } else { + # shouldnt go here. + } + $result->set_time_zone($tz->name) if $tz; + } + return $result; +} + 1; =head1 NAME