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
mr10.2
Rene Krenn 4 years ago
parent fe09f80224
commit d786af1591

@ -62,12 +62,11 @@ sub query_params {
description => 'Filter for contracts with a specific type', description => 'Filter for contracts with a specific type',
query => { query => {
first => sub { first => sub {
my $q = shift; my ($q,$c) = @_;
{ 'product.class' => $q }; 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 {
{ join => 'product' };
}, },
second => sub {},
}, },
}, },
]; ];

@ -322,10 +322,11 @@ sub billingmappings_ajax :Chained('base') :PathPart('billingmappings/ajax') :Arg
sub peering_list :Chained('contract_list') :PathPart('peering') :CaptureArgs(0) { sub peering_list :Chained('contract_list') :PathPart('peering') :CaptureArgs(0) {
my ($self, $c) = @_; 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}; my $base_rs = $c->stash->{contract_select_rs};
$c->stash->{peering_rs} = $base_rs->search({ $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")); $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) { sub reseller_list :Chained('contract_list') :PathPart('reseller') :CaptureArgs(0) {
my ($self, $c) = @_; 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}; my $base_rs = $c->stash->{contract_select_rs};
$c->stash->{reseller_rs} = $base_rs->search({ $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")); $c->stash(ajax_uri => $c->uri_for_action("/contract/reseller_ajax"));
} }

@ -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) { sub ajax_pbx_only :Chained('list_customer') :PathPart('ajax_pbx_only') :Args(0) {
my ($self, $c) = @_; my ($self, $c) = @_;
my $now = NGCP::Panel::Utils::DateTime::current_local; #uniform ts 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 { NGCP::Panel::Utils::Datatables::process($c, $res, $c->stash->{contract_dt_columns}, sub {
my $item = shift; my $item = shift;
my %contact = $item->contact->get_inflated_columns; my %contact = $item->contact->get_inflated_columns;

@ -23,12 +23,13 @@ sub _item_rs {
include_terminated => (defined $include_terminated && $include_terminated ? 1 : 0), include_terminated => (defined $include_terminated && $include_terminated ? 1 : 0),
now => $now, now => $now,
); );
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['pstnpeering','sippeering','reseller'] })->all;
$item_rs = $item_rs->search({ $item_rs = $item_rs->search({
'contact.reseller_id' => undef 'product_id' => { -in => [ @product_ids ] },
},{ },{
join => 'contact', join => 'contact',
}); });
return $item_rs; return $item_rs;
} }

@ -24,7 +24,7 @@ sub _item_rs {
my $item_rs = NGCP::Panel::Utils::Contract::get_customer_rs( my $item_rs = NGCP::Panel::Utils::Contract::get_customer_rs(
c => $c, c => $c,
include_terminated => 1, include_terminated => 1,
#now => $now, now => $now,
); );
return $item_rs; return $item_rs;
} }

@ -349,12 +349,10 @@ sub get_customer {
},{ },{
join => 'contact', join => 'contact',
}); });
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all;
$customer_rs = $customer_rs->search({ $customer_rs = $customer_rs->search({
'-or' => [ 'product_id' => { -in => [ @product_ids ] },
'product.class' => 'sipaccount', });
'product.class' => 'pbxaccount',
],
},undef);
if ($c->user->roles eq "admin" || $c->user->roles eq "ccareadmin") { if ($c->user->roles eq "admin" || $c->user->roles eq "ccareadmin") {
} elsif($c->user->roles eq "reseller" || $c->user->roles eq "ccare") { } elsif($c->user->roles eq "reseller" || $c->user->roles eq "ccare") {
$customer_rs = $customer_rs->search({ $customer_rs = $customer_rs->search({

@ -125,29 +125,26 @@ sub recursively_lock_contract {
} }
sub get_contract_rs { sub get_contract_rs {
my %params = @_; my %params = @_;
my ($c,$schema,$include_terminated) = @params{qw/c schema include_terminated/}; my ($c,$schema,$include_terminated) = @params{qw/c schema include_terminated/};
$schema //= $c->model('DB'); $schema //= $c->model('DB');
my $rs = $schema->resultset('contracts')->search({ my $rs = $schema->resultset('contracts')->search({
$include_terminated ? () : ('me.status' => { '!=' => 'terminated' }), ## no critic (ProhibitCommaSeparatedStatements) $include_terminated ? () : ('me.status' => { '!=' => 'terminated' }), ## no critic (ProhibitCommaSeparatedStatements)
},{ }, undef);
join => 'product',
});
return $rs; return $rs;
} }
sub get_customer_rs { sub get_customer_rs {
my %params = @_; my %params = @_;
my ($c,$schema,$include_terminated) = @params{qw/c schema include_terminated/}; 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( my $rs = get_contract_rs(
c => $c, c => $c,
schema => $schema, schema => $schema,
include_terminated => $include_terminated, include_terminated => $include_terminated,
)->search_rs({ )->search_rs({
'product.class' => { -in => [ 'sipaccount', 'pbxaccount' ] }, 'product_id' => { -in => [ @product_ids ] },
},{ },{
join => 'contact', join => 'contact',
}); });

@ -42,13 +42,16 @@ sub _prepare_peering_sum {
# ok also for contracts without recent balance records: # ok also for contracts without recent balance records:
my ($stime,$etime) = $self->_get_interval(); my ($stime,$etime) = $self->_get_interval();
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['pstnpeering','sippeering'] })->all;
$c->stash( $c->stash(
peering_sum => $c->model('DB')->resultset('contract_balances')->search_rs({ peering_sum => $c->model('DB')->resultset('contract_balances')->search_rs({
'start' => { '>=' => $stime }, 'start' => { '>=' => $stime },
'end' => { '<' => $etime}, 'end' => { '<' => $etime},
'product.class' => { -in => [ 'sippeering', 'pstnpeering' ] }, 'product_id' => { -in => [ @product_ids ] },
},{ },{
join => { contract => 'product', }, join => 'contract',
})->get_column('cash_balance_interval'), })->get_column('cash_balance_interval'),
); );
@ -59,14 +62,16 @@ sub _prepare_reseller_sum {
# ok also for contracts without recent balance records: # 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(); my ($stime,$etime) = $self->_get_interval();
$c->stash( $c->stash(
reseller_sum => $c->model('DB')->resultset('contract_balances')->search_rs({ reseller_sum => $c->model('DB')->resultset('contract_balances')->search_rs({
'start' => { '>=' => $stime }, 'start' => { '>=' => $stime },
'end' => { '<' => $etime}, 'end' => { '<' => $etime},
'product.class' => 'reseller', 'product_id' => { -in => [ @product_ids ] },
},{ },{
join => { contract => 'product', }, join => 'product',
})->get_column('cash_balance_interval'), })->get_column('cash_balance_interval'),
); );

@ -35,16 +35,14 @@ sub _prepare_domains_count {
sub _prepare_customers_count { sub _prepare_customers_count {
my ($self, $c) = @_; my ($self, $c) = @_;
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all;
$c->stash( $c->stash(
customers => $c->model('DB')->resultset('contracts')->search({ customers => $c->model('DB')->resultset('contracts')->search({
'me.status' => { '!=' => 'terminated' }, 'me.status' => { '!=' => 'terminated' },
'contact.reseller_id' => { '-not' => undef }, 'contact.reseller_id' => { '-not' => undef },
'-or' => [ 'product_id' => { -in => [ @product_ids ] },
'product.class' => 'sipaccount',
'product.class' => 'pbxaccount',
],
},{ },{
'join' => [ 'contact', 'product' ], 'join' => 'contact',
}), }),
); );

@ -18,15 +18,16 @@ sub filter {
sub _prepare_customers_count { sub _prepare_customers_count {
my ($self, $c) = @_; my ($self, $c) = @_;
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all;
$c->stash( $c->stash(
customers => $c->model('DB')->resultset('contracts')->search({ customers => $c->model('DB')->resultset('contracts')->search({
'me.status' => { '!=' => 'terminated' }, 'me.status' => { '!=' => 'terminated' },
'product.class' => { 'not in' => [ 'reseller', 'sippeering', 'pstnpeering' ] }, 'product_id' => { -in => [ @product_ids ] },
($c->user->roles eq 'ccare' ($c->user->roles eq 'ccare'
? ('contact.reseller_id' => $c->user->reseller_id) ? ('contact.reseller_id' => $c->user->reseller_id)
: ()) : ())
},{ },{
join => [ 'contact', 'product' ], join => 'contact',
}), }),
); );

@ -18,13 +18,14 @@ sub filter {
sub _prepare_customers_count { sub _prepare_customers_count {
my ($self, $c) = @_; my ($self, $c) = @_;
my @product_ids = map { $_->id; } $c->model('DB')->resultset('products')->search_rs({ 'class' => ['sipaccount','pbxaccount'] })->all;
$c->stash( $c->stash(
customers => $c->model('DB')->resultset('contracts')->search({ customers => $c->model('DB')->resultset('contracts')->search({
'me.status' => { '!=' => 'terminated' }, 'me.status' => { '!=' => 'terminated' },
'contact.reseller_id' => $c->user->reseller_id, 'contact.reseller_id' => $c->user->reseller_id,
'product.class' => { 'not in' => [ 'reseller', 'sippeering', 'pstnpeering' ] }, 'product_id' => { -in => [ @product_ids ] },
},{ },{
join => [ 'contact', 'product' ], join => 'contact',
}), }),
); );

@ -372,7 +372,7 @@ my @allcustomers = ();
is($res->code, 422, "check patched invalid status"); is($res->code, 422, "check patched invalid status");
$req->content(JSON::to_json( $req->content(JSON::to_json(
[ { op => 'replace', path => '/contact_id', value => 99999 } ] [ { op => 'replace', path => '/contact_id', value => 99999999 } ]
)); ));
$res = $ua->request($req); $res = $ua->request($req);
is($res->code, 422, "check patched invalid contact_id"); is($res->code, 422, "check patched invalid contact_id");

Loading…
Cancel
Save