From d786af15918aeb97269843952a0d1f6345af63e2 Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Mon, 15 Nov 2021 15:36:16 +0100 Subject: [PATCH] TT#147151 finish refactoring of avoiding JOIN billing.product follow up on TT#147151 (fast loading/paging/searching panel datatables), which broke restapi tests. Change-Id: I799cb9087b9405c71dec4c690e7a7bab5dfdbdde --- lib/NGCP/Panel/Controller/API/Contracts.pm | 9 ++++----- lib/NGCP/Panel/Controller/Contract.pm | 10 ++++++---- lib/NGCP/Panel/Controller/Customer.pm | 5 ++++- lib/NGCP/Panel/Role/API/Contracts.pm | 5 +++-- lib/NGCP/Panel/Role/API/Customers.pm | 2 +- lib/NGCP/Panel/Role/API/Subscribers.pm | 8 +++----- lib/NGCP/Panel/Utils/Contract.pm | 11 ++++------- .../Panel/Widget/Dashboard/AdminBillingOverview.pm | 13 +++++++++---- .../Panel/Widget/Dashboard/AdminResellerOverview.pm | 8 +++----- .../Panel/Widget/Dashboard/CCareCustomerOverview.pm | 5 +++-- .../Widget/Dashboard/ResellerCustomerOverview.pm | 5 +++-- t/api-rest/api-customers.t | 2 +- 12 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/Contracts.pm b/lib/NGCP/Panel/Controller/API/Contracts.pm index def4722563..2074419c5e 100644 --- a/lib/NGCP/Panel/Controller/API/Contracts.pm +++ b/lib/NGCP/Panel/Controller/API/Contracts.pm @@ -62,12 +62,11 @@ sub query_params { description => 'Filter for contracts with a specific type', query => { first => sub { - my $q = shift; - { 'product.class' => $q }; - }, - second => sub { - { join => 'product' }; + my ($q,$c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => [split(/\s*[,;]\s*/,$q)] })->all; + { 'product_id' => { -in => [ @product_ids ] }, }; }, + second => sub {}, }, }, ]; diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index 025b027cc0..1c70bb0fc6 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -322,10 +322,11 @@ sub billingmappings_ajax :Chained('base') :PathPart('billingmappings/ajax') :Arg sub peering_list :Chained('contract_list') :PathPart('peering') :CaptureArgs(0) { my ($self, $c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sippeering'] })->all; my $base_rs = $c->stash->{contract_select_rs}; $c->stash->{peering_rs} = $base_rs->search({ - 'product.class' => 'sippeering', - }); + 'product_id' => { -in => [ @product_ids ] }, + }); $c->stash(ajax_uri => $c->uri_for_action("/contract/peering_ajax")); } @@ -431,10 +432,11 @@ sub peering_create :Chained('peering_list') :PathPart('create') :Args(0) { sub reseller_list :Chained('contract_list') :PathPart('reseller') :CaptureArgs(0) { my ($self, $c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['reseller'] })->all; my $base_rs = $c->stash->{contract_select_rs}; $c->stash->{reseller_rs} = $base_rs->search({ - 'product.class' => 'reseller', - }); + 'product_id' => { -in => [ @product_ids ] }, + }); $c->stash(ajax_uri => $c->uri_for_action("/contract/reseller_ajax")); } diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 277b01149a..d5d0b7b43a 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -158,7 +158,10 @@ sub ajax_package_filter :Chained('list_customer') :PathPart('ajax/package') :Arg sub ajax_pbx_only :Chained('list_customer') :PathPart('ajax_pbx_only') :Args(0) { my ($self, $c) = @_; my $now = NGCP::Panel::Utils::DateTime::current_local; #uniform ts - my $res = $c->stash->{contract_select_rs}->search_rs({'product.class' => 'pbxaccount'}); + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['pbxaccount'] })->all; + my $res = $c->stash->{contract_select_rs}->search_rs({ + 'product_id' => { -in => [ @product_ids ] }, + }); NGCP::Panel::Utils::Datatables::process($c, $res, $c->stash->{contract_dt_columns}, sub { my $item = shift; my %contact = $item->contact->get_inflated_columns; diff --git a/lib/NGCP/Panel/Role/API/Contracts.pm b/lib/NGCP/Panel/Role/API/Contracts.pm index b3a612920b..ff46322bd2 100644 --- a/lib/NGCP/Panel/Role/API/Contracts.pm +++ b/lib/NGCP/Panel/Role/API/Contracts.pm @@ -23,12 +23,13 @@ sub _item_rs { include_terminated => (defined $include_terminated && $include_terminated ? 1 : 0), now => $now, ); + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['pstnpeering','sippeering','reseller'] })->all; $item_rs = $item_rs->search({ - 'contact.reseller_id' => undef + 'product_id' => { -in => [ @product_ids ] }, },{ join => 'contact', }); - + return $item_rs; } diff --git a/lib/NGCP/Panel/Role/API/Customers.pm b/lib/NGCP/Panel/Role/API/Customers.pm index 131f050317..8cb1f58e75 100644 --- a/lib/NGCP/Panel/Role/API/Customers.pm +++ b/lib/NGCP/Panel/Role/API/Customers.pm @@ -24,7 +24,7 @@ sub _item_rs { my $item_rs = NGCP::Panel::Utils::Contract::get_customer_rs( c => $c, include_terminated => 1, - #now => $now, + now => $now, ); return $item_rs; } diff --git a/lib/NGCP/Panel/Role/API/Subscribers.pm b/lib/NGCP/Panel/Role/API/Subscribers.pm index 91f60ec418..dfc860c79e 100644 --- a/lib/NGCP/Panel/Role/API/Subscribers.pm +++ b/lib/NGCP/Panel/Role/API/Subscribers.pm @@ -349,12 +349,10 @@ sub get_customer { },{ join => 'contact', }); + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all; $customer_rs = $customer_rs->search({ - '-or' => [ - 'product.class' => 'sipaccount', - 'product.class' => 'pbxaccount', - ], - },undef); + 'product_id' => { -in => [ @product_ids ] }, + }); if ($c->user->roles eq "admin" || $c->user->roles eq "ccareadmin") { } elsif($c->user->roles eq "reseller" || $c->user->roles eq "ccare") { $customer_rs = $customer_rs->search({ diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index 10f562f4e2..833fd67232 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -125,29 +125,26 @@ sub recursively_lock_contract { } sub get_contract_rs { - my %params = @_; my ($c,$schema,$include_terminated) = @params{qw/c schema include_terminated/}; $schema //= $c->model('DB'); my $rs = $schema->resultset('contracts')->search({ $include_terminated ? () : ('me.status' => { '!=' => 'terminated' }), ## no critic (ProhibitCommaSeparatedStatements) - },{ - join => 'product', - }); + }, undef); return $rs; - } sub get_customer_rs { my %params = @_; my ($c,$schema,$include_terminated) = @params{qw/c schema include_terminated/}; - + $schema //= $c->model('DB'); + my @product_ids = map { $_->id; } $schema->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all; my $rs = get_contract_rs( c => $c, schema => $schema, include_terminated => $include_terminated, )->search_rs({ - 'product.class' => { -in => [ 'sipaccount', 'pbxaccount' ] }, + 'product_id' => { -in => [ @product_ids ] }, },{ join => 'contact', }); diff --git a/lib/NGCP/Panel/Widget/Dashboard/AdminBillingOverview.pm b/lib/NGCP/Panel/Widget/Dashboard/AdminBillingOverview.pm index 19ea2673c7..6b6bae67fa 100644 --- a/lib/NGCP/Panel/Widget/Dashboard/AdminBillingOverview.pm +++ b/lib/NGCP/Panel/Widget/Dashboard/AdminBillingOverview.pm @@ -42,13 +42,16 @@ sub _prepare_peering_sum { # ok also for contracts without recent balance records: my ($stime,$etime) = $self->_get_interval(); + + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['pstnpeering','sippeering'] })->all; + $c->stash( peering_sum => $c->model('DB')->resultset('contract_balances')->search_rs({ 'start' => { '>=' => $stime }, 'end' => { '<' => $etime}, - 'product.class' => { -in => [ 'sippeering', 'pstnpeering' ] }, + 'product_id' => { -in => [ @product_ids ] }, },{ - join => { contract => 'product', }, + join => 'contract', })->get_column('cash_balance_interval'), ); @@ -59,14 +62,16 @@ sub _prepare_reseller_sum { # ok also for contracts without recent balance records: + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['reseller'] })->all; + my ($stime,$etime) = $self->_get_interval(); $c->stash( reseller_sum => $c->model('DB')->resultset('contract_balances')->search_rs({ 'start' => { '>=' => $stime }, 'end' => { '<' => $etime}, - 'product.class' => 'reseller', + 'product_id' => { -in => [ @product_ids ] }, },{ - join => { contract => 'product', }, + join => 'product', })->get_column('cash_balance_interval'), ); diff --git a/lib/NGCP/Panel/Widget/Dashboard/AdminResellerOverview.pm b/lib/NGCP/Panel/Widget/Dashboard/AdminResellerOverview.pm index be61d3ffb2..990cda0ec4 100644 --- a/lib/NGCP/Panel/Widget/Dashboard/AdminResellerOverview.pm +++ b/lib/NGCP/Panel/Widget/Dashboard/AdminResellerOverview.pm @@ -35,16 +35,14 @@ sub _prepare_domains_count { sub _prepare_customers_count { my ($self, $c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all; $c->stash( customers => $c->model('DB')->resultset('contracts')->search({ 'me.status' => { '!=' => 'terminated' }, 'contact.reseller_id' => { '-not' => undef }, - '-or' => [ - 'product.class' => 'sipaccount', - 'product.class' => 'pbxaccount', - ], + 'product_id' => { -in => [ @product_ids ] }, },{ - 'join' => [ 'contact', 'product' ], + 'join' => 'contact', }), ); diff --git a/lib/NGCP/Panel/Widget/Dashboard/CCareCustomerOverview.pm b/lib/NGCP/Panel/Widget/Dashboard/CCareCustomerOverview.pm index a9cec05203..8e7ca1a9e8 100644 --- a/lib/NGCP/Panel/Widget/Dashboard/CCareCustomerOverview.pm +++ b/lib/NGCP/Panel/Widget/Dashboard/CCareCustomerOverview.pm @@ -18,15 +18,16 @@ sub filter { sub _prepare_customers_count { my ($self, $c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all; $c->stash( customers => $c->model('DB')->resultset('contracts')->search({ 'me.status' => { '!=' => 'terminated' }, - 'product.class' => { 'not in' => [ 'reseller', 'sippeering', 'pstnpeering' ] }, + 'product_id' => { -in => [ @product_ids ] }, ($c->user->roles eq 'ccare' ? ('contact.reseller_id' => $c->user->reseller_id) : ()) },{ - join => [ 'contact', 'product' ], + join => 'contact', }), ); diff --git a/lib/NGCP/Panel/Widget/Dashboard/ResellerCustomerOverview.pm b/lib/NGCP/Panel/Widget/Dashboard/ResellerCustomerOverview.pm index 74dfa3d468..6e752b623f 100644 --- a/lib/NGCP/Panel/Widget/Dashboard/ResellerCustomerOverview.pm +++ b/lib/NGCP/Panel/Widget/Dashboard/ResellerCustomerOverview.pm @@ -18,13 +18,14 @@ sub filter { sub _prepare_customers_count { my ($self, $c) = @_; + my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all; $c->stash( customers => $c->model('DB')->resultset('contracts')->search({ 'me.status' => { '!=' => 'terminated' }, 'contact.reseller_id' => $c->user->reseller_id, - 'product.class' => { 'not in' => [ 'reseller', 'sippeering', 'pstnpeering' ] }, + 'product_id' => { -in => [ @product_ids ] }, },{ - join => [ 'contact', 'product' ], + join => 'contact', }), ); diff --git a/t/api-rest/api-customers.t b/t/api-rest/api-customers.t index 19a29bb1f9..9745d369fe 100644 --- a/t/api-rest/api-customers.t +++ b/t/api-rest/api-customers.t @@ -372,7 +372,7 @@ my @allcustomers = (); is($res->code, 422, "check patched invalid status"); $req->content(JSON::to_json( - [ { op => 'replace', path => '/contact_id', value => 99999 } ] + [ { op => 'replace', path => '/contact_id', value => 99999999 } ] )); $res = $ua->request($req); is($res->code, 422, "check patched invalid contact_id");