From cc06544d45cd4879312990b3dae1be5b2dc1c201 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 11 Nov 2016 17:40:23 +0100 Subject: [PATCH] TT#5479 improve sms journal api resource - fix duplicated "use parent" - add query_params: subscriber_id, customer_id, reseller_id, time_le, time_ge, direction - add field "time" - prepeare field "time" for output Change-Id: I56efd42c4264d81709578be114592d523a0dacfa --- lib/NGCP/Panel/Controller/API/SMS.pm | 85 ++++++++++++++++++++++-- lib/NGCP/Panel/Controller/API/SMSItem.pm | 2 - lib/NGCP/Panel/Field/DateTime.pm | 2 +- lib/NGCP/Panel/Form/SMSAPI.pm | 9 +++ lib/NGCP/Panel/Role/API/SMS.pm | 7 ++ 5 files changed, 98 insertions(+), 7 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/SMS.pm b/lib/NGCP/Panel/Controller/API/SMS.pm index e93f93bb2b..58e3742b6a 100644 --- a/lib/NGCP/Panel/Controller/API/SMS.pm +++ b/lib/NGCP/Panel/Controller/API/SMS.pm @@ -16,10 +16,87 @@ sub api_description { return 'Shows a journal of sent and received messages. New messages can be sent by issuing a POST request to the api collection.'; } -# sub query_params { -# return [ -# ]; -# } +sub query_params { + return [ + { + param => 'subscriber_id', + description => 'Filter for messages belonging to a specific subscriber', + query => { + first => sub { + my $q = shift; + return { 'voip_subscriber.id' => $q }; + }, + second => sub { + return { join => {provisioning_voip_subscriber => 'voip_subscriber'}}; + }, + }, + }, + { + param => 'customer_id', + description => 'Filter for messages belonging to a specific customer', + query => { + first => sub { + my $q = shift; + return { 'contract.id' => $q }; + }, + second => sub { + return { join => {provisioning_voip_subscriber => { 'voip_subscriber' => 'contract' } }}; + }, + }, + }, + { + param => 'reseller_id', + description => 'Filter for messages belonging to a specific reseller', + query => { + first => sub { + my $q = shift; + return { 'contact.id' => $q }; + }, + second => sub { + return { join => {provisioning_voip_subscriber => { 'voip_subscriber' => { 'contract' => 'contact' } } }}; + }, + }, + }, + { + param => 'time_ge', + description => 'Filter for messages sent later or equal the specified time stamp.', + query => { + first => sub { + my $q = shift; + { 'me.time' => { '>=' => $q } }; + }, + second => sub {}, + }, + }, + { + param => 'time_le', + description => 'Filter for messages sent earlier or equal the specified time stamp.', + query => { + first => sub { + my $q = shift; + $q .= ' 23:59:59' if($q =~ /^\d{4}\-\d{2}\-\d{2}$/); + { "me.time" => { '<=' => $q } }; + }, + second => sub {}, + }, + }, + { + param => 'direction', + description => 'Filter for messages sent ("out") or received ("in").', + query => { + first => sub { + my $q = shift; + if ($q eq "out" || $q eq "in") { + return { "me.direction" => $q }; + } else { + return {}, + } + }, + second => sub {}, + }, + }, + ]; +} sub create_item { my ($self, $c, $resource, $form, $process_extras) = @_; diff --git a/lib/NGCP/Panel/Controller/API/SMSItem.pm b/lib/NGCP/Panel/Controller/API/SMSItem.pm index ff2c6575a3..c94e3510d0 100644 --- a/lib/NGCP/Panel/Controller/API/SMSItem.pm +++ b/lib/NGCP/Panel/Controller/API/SMSItem.pm @@ -12,8 +12,6 @@ sub allowed_methods{ return [qw/GET OPTIONS HEAD/]; } -use parent qw/Catalyst::Controller NGCP::Panel::Role::API::CFSourceSets/; - 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Field/DateTime.pm b/lib/NGCP/Panel/Field/DateTime.pm index 0b7b846b00..160dfcf8b8 100644 --- a/lib/NGCP/Panel/Field/DateTime.pm +++ b/lib/NGCP/Panel/Field/DateTime.pm @@ -9,7 +9,7 @@ has '+deflate_method' => ( default => sub { \&datetime_deflate } ); sub datetime_deflate { my ( $self, $value ) = @_; if(blessed($value) && $value->isa('DateTime')) { - return $value->ymd('-') . ' ' . $value->hms(':');; + return $value->ymd('-') . ' ' . $value->hms(':'); } else { return $value; } diff --git a/lib/NGCP/Panel/Form/SMSAPI.pm b/lib/NGCP/Panel/Form/SMSAPI.pm index 451a06efe8..0571bb0789 100644 --- a/lib/NGCP/Panel/Form/SMSAPI.pm +++ b/lib/NGCP/Panel/Form/SMSAPI.pm @@ -91,6 +91,15 @@ has_field 'reason' => ( }, ); +has_field 'time' => ( + type => '+NGCP::Panel::Field::DateTime', # Readonly + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The timestamp of the message'], + }, +); + has_field 'save' => ( type => 'Submit', value => 'Save', diff --git a/lib/NGCP/Panel/Role/API/SMS.pm b/lib/NGCP/Panel/Role/API/SMS.pm index c0aa5e9dd9..99aefb96c9 100644 --- a/lib/NGCP/Panel/Role/API/SMS.pm +++ b/lib/NGCP/Panel/Role/API/SMS.pm @@ -9,6 +9,7 @@ use parent 'NGCP::Panel::Role::API'; use Data::HAL::Link qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::SMS; +use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Form::SMSAPI; sub item_name { @@ -32,6 +33,12 @@ sub hal_links { ]; } +sub process_hal_resource { + my($self, $c, $item, $resource, $form) = @_; + $resource->{time} = NGCP::Panel::Utils::DateTime::to_string($resource->{time}); + return $resource; +} + sub _item_rs { my ($self, $c) = @_; my $item_rs;