From 445197df537877117f2f150bf4519c76af1f201c Mon Sep 17 00:00:00 2001 From: Flaviu Mates Date: Mon, 2 Mar 2020 17:59:21 +0200 Subject: [PATCH] TT#76360 - Implement /api/activesubscriberpreferences endpoint * A new endpoint is now available that will return subscriber's preferences and also the preferences that the subscriber inherits from its domain Change-Id: Iaa29fbe57d520f79ac7051dc8fd151d26df41384 --- .../API/ActiveSubscriberPreferences.pm | 61 +++++++++++++++++++ .../API/ActiveSubscriberPreferencesItem.pm | 25 ++++++++ lib/NGCP/Panel/Role/API/Preferences.pm | 2 +- lib/NGCP/Panel/Utils/Preferences.pm | 39 ++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferences.pm create mode 100644 lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferencesItem.pm diff --git a/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferences.pm b/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferences.pm new file mode 100644 index 0000000000..a8d2781390 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferences.pm @@ -0,0 +1,61 @@ +package NGCP::Panel::Controller::API::ActiveSubscriberPreferences; + +use Sipwise::Base; + +use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::Preferences/; + +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin/], +}); + +sub allowed_methods{ + return [qw/GET OPTIONS HEAD/]; +} + +sub api_description { + return 'Specifies certain properties (preferences) for a Subscriber. The full list of properties can be obtained via SubscriberPreferenceDefs.'; +}; + +sub container_resource_type{ + return 'active'; +} + +sub resource_name{ + return 'activesubscriberpreferences'; +} + +sub query_params { + return [ + { + param => 'reseller_id', + description => 'Filter for subscribers of customers belonging to a specific reseller', + query => { + first => sub { + my $q = shift; + { 'contact.reseller_id' => $q }; + }, + second => sub { + { join => { 'contract' => 'contact' } }; + }, + }, + }, + { + param => 'contact_id', + description => 'Filter for subscribers of contracts with a specific contact id', + query => { + first => sub { + my $q = shift; + { 'contract.contact_id' => $q }; + }, + second => sub {}, + }, + }, + { + param => 'location_ip', + description => 'Filter for contract preferences for a specific location', + }, + ]; +} +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferencesItem.pm b/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferencesItem.pm new file mode 100644 index 0000000000..e1077705c1 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/ActiveSubscriberPreferencesItem.pm @@ -0,0 +1,25 @@ +package NGCP::Panel::Controller::API::ActiveSubscriberPreferencesItem; + +use Sipwise::Base; + +use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::Preferences/; + +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin/], +}); + +sub allowed_methods{ + return [qw/GET OPTIONS HEAD/]; +} + +sub container_resource_type{ + return 'active'; +} + +sub resource_name{ + return 'activesubscriberpreferences'; +} + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/Preferences.pm b/lib/NGCP/Panel/Role/API/Preferences.pm index 6df2a441ff..91a276cb9c 100644 --- a/lib/NGCP/Panel/Role/API/Preferences.pm +++ b/lib/NGCP/Panel/Role/API/Preferences.pm @@ -94,7 +94,7 @@ sub _item_rs { join => 'profile_set', }); } - } elsif($type eq "subscribers") { + } elsif($type eq "subscribers" || $type eq "active") { if($c->user->roles eq "admin" || $c->user->roles eq "ccareadmin") { $item_rs = $c->model('DB')->resultset('voip_subscribers')->search({ 'me.status' => { '!=' => 'terminated' } diff --git a/lib/NGCP/Panel/Utils/Preferences.pm b/lib/NGCP/Panel/Utils/Preferences.pm index 439a9e1075..575bee7262 100644 --- a/lib/NGCP/Panel/Utils/Preferences.pm +++ b/lib/NGCP/Panel/Utils/Preferences.pm @@ -110,6 +110,45 @@ sub prepare_resource { $prefs = $item->voip_devprof_preferences; } elsif($type eq "pbxdevices") { $prefs = $item->voip_fielddev_preferences; + } elsif($type eq "active") { + my $sub_prefs = $item->provisioning_voip_subscriber->voip_usr_preferences->search(undef, {columns => ['value', 'attribute_id']}); + my $profile = $item->provisioning_voip_subscriber->voip_subscriber_profile; + if ($profile) { + $has_profile = 1; + %profile_allowed_attrs = map { $_ => 1 } $profile->profile_attributes->get_column('attribute_id')->all; + } + my $dom_prefs = $item->domain->provisioning_voip_domain->voip_dom_preferences->search( + undef, + { + columns => ['value', 'attribute_id'], + result_class => $sub_prefs->result_class + } + ); + #search for location if IP is provided + my $location_id; + if ($c->request->param('location_ip')) { + my $location = $schema->resultset('voip_contract_locations')->search( + { + 'voip_contract_location_blocks.ip' => $c->request->param('location_ip') + }, + { + join => 'voip_contract_location_blocks' + } + )->first; + $location_id = $location->id if ($location); + } + my $ct_prefs = $item->contract->voip_contract_preferences->search( + { + location_id => $location_id || undef + }, + { + columns => ['value', 'attribute_id'], + result_class => $sub_prefs->result_class + } + ); + + $prefs = $sub_prefs->union($ct_prefs->search({attribute_id => {-not_in => [map {$_->get_column('attribute_id')} $sub_prefs->all]}})); + $prefs = $prefs->union($dom_prefs->search({attribute_id => {-not_in => [map {$_->get_column('attribute_id')} $prefs->all]}})); } $prefs = $prefs->search({ }, {