From 9115b48f703b566c170f1f08ab36387b0d26ff5c Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Mon, 30 Jun 2014 10:01:33 +0200 Subject: [PATCH] MT#7797 Filter for terminated customers. - Allow status=xxx in collection - Fix moving customercontacts --- .../Controller/API/CustomerPreferencesItem.pm | 2 -- lib/NGCP/Panel/Controller/API/Customers.pm | 11 +++++++++++ lib/NGCP/Panel/Role/API/Customers.pm | 15 +++++++++------ lib/NGCP/Panel/Role/API/Preferences.pm | 8 -------- lib/NGCP/Panel/Utils/Contract.pm | 3 ++- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm b/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm index 5328335149..e46e324c34 100644 --- a/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm +++ b/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm @@ -147,8 +147,6 @@ sub PUT :Allow { last unless $resource; my $old_resource = $self->get_resource($c, $customer, "contracts"); - use Data::Printer; print ">>>>>>>>>>>>>>>> old resource\n"; p $old_resource; - # last param is "replace" to delete all existing prefs # for proper PUT behavior $customer = $self->update_item($c, $customer, $old_resource, $resource, 1, "contracts"); diff --git a/lib/NGCP/Panel/Controller/API/Customers.pm b/lib/NGCP/Panel/Controller/API/Customers.pm index 0a19b37837..85d02459dc 100644 --- a/lib/NGCP/Panel/Controller/API/Customers.pm +++ b/lib/NGCP/Panel/Controller/API/Customers.pm @@ -27,6 +27,17 @@ class_has 'query_params' => ( is => 'ro', isa => 'ArrayRef', default => sub {[ + { + param => 'status', + description => 'Filter for customers with a specific status', + query => { + first => sub { + my $q = shift; + { 'me.status' => $q }; + }, + second => sub { }, + }, + }, { param => 'reseller_id', description => 'Filter for customers belonging to a specific reseller', diff --git a/lib/NGCP/Panel/Role/API/Customers.pm b/lib/NGCP/Panel/Role/API/Customers.pm index dabc7423b4..8943375253 100644 --- a/lib/NGCP/Panel/Role/API/Customers.pm +++ b/lib/NGCP/Panel/Role/API/Customers.pm @@ -20,7 +20,10 @@ sub item_rs { my ($self, $c) = @_; # returns a contracts rs filtered based on role - my $item_rs = NGCP::Panel::Utils::Contract::get_customer_rs(c => $c); + my $item_rs = NGCP::Panel::Utils::Contract::get_customer_rs( + c => $c, + include_terminated => 1, + ); return $item_rs; } @@ -132,12 +135,12 @@ sub update_customer { $resource->{modify_timestamp} = $now; my $billing_profile; + $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id}); + unless($billing_profile) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', doesn't exist"); + return; + } if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) { - $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id}); - unless($billing_profile) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', doesn't exist"); - return; - } unless($billing_profile->reseller_id == $customer->contact->reseller_id) { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', reseller doesn't match customer contact reseller"); return; diff --git a/lib/NGCP/Panel/Role/API/Preferences.pm b/lib/NGCP/Panel/Role/API/Preferences.pm index b343bb81a5..1569063824 100644 --- a/lib/NGCP/Panel/Role/API/Preferences.pm +++ b/lib/NGCP/Panel/Role/API/Preferences.pm @@ -242,9 +242,6 @@ sub item_by_id { sub get_preference_rs { my ($self, $c, $type, $elem, $attr) = @_; - - use Data::Printer; print ">>>>>>>>>>>>>>>> get_preference_rs, type=$type, elem=$elem, attr=$attr\n"; - my $rs; if($type eq "domains") { $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs( @@ -315,7 +312,6 @@ sub update_item { $pref_type = 'peer_pref'; $reseller_id = 1; } elsif($type eq "contracts") { - use Data::Printer; print ">>>>>>>>>>>>>>>> prepare resource\n"; p $resource; p $old_resource; delete $resource->{customer_id}; delete $old_resource->{customer_id}; $accessor = $item->id; @@ -354,7 +350,6 @@ sub update_item { $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error."); return; }; - use Data::Printer; print ">>>>>>>>>>>>>>>> done deleteting old for PUT\n"; } else { # in case of PATCH, we remove only those entries marked for removal in the patch try { @@ -407,11 +402,8 @@ sub update_item { } foreach my $pref(keys %{ $resource }) { - use Data::Printer; print ">>>>>>>>>>>>>>>> handling pref $pref\n"; next unless(defined $resource->{$pref}); - use Data::Printer; print ">>>>>>>>>>>>>>>> $pref is defined, get rs for type $type\n"; my $rs = $self->get_preference_rs($c, $type, $elem, $pref); - use Data::Printer; print ">>>>>>>>>>>>>>>> got an rs\n"; unless($rs) { $c->log->debug("removing unknown preference '$pref' from update"); next; diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index 4455ba7000..019e9ff808 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -204,7 +204,7 @@ sub get_contract_rs { my $dtf = $schema->storage->datetime_parser; my $rs = $schema->resultset('contracts') ->search({ - 'me.status' => { '!=' => 'terminated' }, + $params{include_terminated} ? () : ('me.status' => { '!=' => 'terminated' }), 'billing_mappings.id' => { '=' => $mapping_rs->search({ contract_id => { -ident => 'me.id' }, @@ -246,6 +246,7 @@ sub get_customer_rs { my $customers = get_contract_rs( schema => $c->model('DB'), + include_terminated => $params{include_terminated}, ); $customers = $customers->search({