MT#14979 filter calllists by direction properly

Change-Id: I73112f94361592ccce082269e414d5f5a16b42e5
changes/07/2707/6
Gerhard Jungwirth 11 years ago
parent e214d316c0
commit a356152416

@ -130,14 +130,24 @@ class_has 'query_params' => (
query => {
first => sub {
my ($q, $c) = @_;
if($q eq "out") {
{
source_user_id => $c->user->uuid,
};
} elsif($q eq "in") {
{
destination_user_id => $c->user->uuid,
return unless ($q eq "out" || $q eq "in");
my $owner = $c->stash->{owner} // {};
if ($owner->{subscriber}) {
my $field = ($q eq "out") ? "source_user_id" : "destination_user_id";
return {
$field => $owner->{subscriber}->uuid,
};
} elsif ($owner->{customer}) {
if ($q eq "out") {
return {
'source_account_id' => $owner->{customer}->id,
};
} else {
return {
'destination_account_id' => $owner->{customer}->id,
'source_account_id' => {'!=' => $owner->{customer}->id},
};
}
}
},
second => sub {},
@ -150,7 +160,7 @@ class_has 'query_params' => (
first => sub {
my $q = shift;
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
{ start_time => { '>=' => $dt->epoch } },
{ start_time => { '>=' => $dt->epoch } };
},
second => sub {},
},
@ -163,7 +173,7 @@ class_has 'query_params' => (
my $q = shift;
$q .= ' 23:59:59' if($q =~ /^\d{4}\-\d{2}\-\d{2}$/);
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
{ start_time => { '<=' => $dt->epoch } },
{ start_time => { '<=' => $dt->epoch } };
},
second => sub {},
},
@ -196,6 +206,7 @@ sub auto :Private {
$self->set_body($c);
$self->log_request($c);
return 1;
}
sub GET :Allow {
@ -206,6 +217,7 @@ sub GET :Allow {
{
my $owner = $self->get_owner_data($c, $schema);
last unless $owner;
$c->stash(owner => $owner); # for query_param: direction
my $items = $self->item_rs($c);
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
@ -275,6 +287,7 @@ sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -23,8 +23,6 @@ use NGCP::Panel::Utils::Journal qw();
#use Data::HAL::Link qw();
has('last_modified', is => 'rw', isa => InstanceOf['DateTime']);
has('ctx', is => 'rw', isa => InstanceOf['NGCP::Panel']);
sub get_valid_post_data {
my ($self, %params) = @_;
@ -120,7 +118,7 @@ sub validate_form {
if($run) {
# check keys/vals
$form->process(params => $resource, posted => 1, %$form_params );
$form->process(params => $resource, posted => 1, %{$form_params} );
unless($form->validated) {
my $e = join '; ', map {
sprintf 'field=\'%s\', input=\'%s\', errors=\'%s\'',
@ -213,7 +211,7 @@ sub valid_media_type {
my $type;
if(ref $media_type eq "ARRAY") {
$type = join ' or ', @{ $media_type };
return 1 if $ctype && grep { $ctype eq $_ } @$media_type;
return 1 if $ctype && grep { $ctype eq $_ } @{$media_type};
} else {
$type = $media_type;
return 1 if($ctype && index($ctype, $media_type) == 0);
@ -322,7 +320,7 @@ sub require_valid_patch {
};
for my $o(keys %{ $valid_ops }) {
unless(grep { /^$o$/ } @{ $ops }) {
delete $valid_ops->{$o}
delete $valid_ops->{$o};
}
}
@ -521,8 +519,6 @@ sub apply_patch {
sub set_body {
my ($self, $c) = @_;
#Ctx could be initialized in Root::get_collections - wouldn't it be better?
$self->ctx($c);
$c->stash->{body} = $c->request->body ? (do { local $/; $c->request->body->getline }) : '';
}
@ -574,24 +570,6 @@ around 'item_rs' => sub {
}
return $item_rs;
## no query params defined in collection controller
#unless($self->can('query_params') && @{ $self->query_params }) {
# return $item_rs;
#}
#
#my $c = $orig_params[0];
#foreach my $param(keys %{ $c->req->query_params }) {
# my @p = grep { $_->{param} eq $param } @{ $self->query_params };
# next unless($p[0]->{query}); # skip "dummy" query parameters
# my $q = $c->req->query_params->{$param}; # TODO: arrayref?
# $q =~ s/\*/\%/g;
# if(@p) {
# #ctx config may be necessary
# $item_rs = $item_rs->search($p[0]->{query}->{first}($q,$self->ctx), $p[0]->{query}->{second}($q,$self->ctx));
# }
#}
#return $item_rs;
};
sub apply_query_params {
@ -608,13 +586,12 @@ sub apply_query_params {
my $q = $c->req->query_params->{$param}; # TODO: arrayref?
$q =~ s/\*/\%/g;
if(@p) {
#ctx config may be necessary
$item_rs = $item_rs->search($p[0]->{query}->{first}($q,$self->ctx), $p[0]->{query}->{second}($q,$self->ctx));
$item_rs = $item_rs->search($p[0]->{query}->{first}($q,$c), $p[0]->{query}->{second}($q,$c));
}
}
return $item_rs;
};
}
sub is_true {
my ($self, $v) = @_;
@ -622,7 +599,7 @@ sub is_true {
if(ref $v eq "") {
$val = $v;
} else {
$val = $$v;
$val = ${$v};
}
return 1 if(defined $val && $val == 1);
return;
@ -634,7 +611,7 @@ sub is_false {
if(ref $v eq "") {
$val = $v;
} else {
$val = $$v;
$val = ${$v};
}
return 1 unless(defined $val && $val == 1);
return;

@ -0,0 +1,80 @@
use strict;
use warnings;
use Test::More;
use Test::Collection;
my $test_machine = Test::Collection->new(
name => 'calllists',
);
diag('Note that the next tests require at least one subscriber to be present');
# test with a subscriber
SKIP:
{
my ($res,$sub1,$sub1_id,$cl_collection, $cl_collection_in, $cl_collection_out);
if($ENV{API_FORCE_SUBSCRIBER_ID}) {
$sub1_id = $ENV{API_FORCE_SUBSCRIBER_ID};
} else {
($res, $sub1) = $test_machine->request_get('/api/subscribers/?page=1&rows=1');
is($res->code, 200, "fetch a subscriber for testing");
if ($sub1->{total_count} < 1) {
skip("Precondition not met: need a subscriber",1);
}
($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!;
}
cmp_ok ($sub1_id, '>', 0, "should be positive integer");
($res, $cl_collection) = $test_machine->request_get('/api/calllists/?page=1&rows=10&subscriber_id='.$sub1_id);
is($res->code, 200, "fetch calllists collection of subscriber ($sub1_id)");
($res, $cl_collection_in) = $test_machine->request_get('/api/calllists/?page=1&rows=10&direction=in&subscriber_id='.$sub1_id);
is($res->code, 200, "fetch calllists collection of subscriber ($sub1_id) with direction filter in");
($res, $cl_collection_out) = $test_machine->request_get('/api/calllists/?page=1&rows=10&direction=out&subscriber_id='.$sub1_id);
is($res->code, 200, "fetch calllists collection of subscriber ($sub1_id) with direction filter out");
is($cl_collection_in->{total_count}+$cl_collection_out->{total_count}, $cl_collection->{total_count},
"Incoming and outgoing calls should add up to total number of calls");
diag("Total number of calls: " . $cl_collection->{total_count});
}
# test with a customer
SKIP:
{
my ($res,$cust1,$cust1_id,$cl_collection, $cl_collection_in, $cl_collection_out);
if($ENV{API_FORCE_CUSTOMER_ID}) {
$cust1_id = $ENV{API_FORCE_CUSTOMER_ID};
} else {
($res, $cust1) = $test_machine->request_get('/api/customers/?page=1&rows=1');
is($res->code, 200, "fetch a customer for testing");
if ($cust1->{total_count} < 1) {
skip("Precondition not met: need a customer",1);
}
($cust1_id) = $cust1->{_embedded}->{'ngcp:customers'}->{_links}{self}{href} =~ m!customers/([0-9]*)$!;
}
cmp_ok ($cust1_id, '>', 0, "should be positive integer");
($res, $cl_collection) = $test_machine->request_get('/api/calllists/?page=1&rows=10&customer_id='.$cust1_id);
is($res->code, 200, "fetch calllists collection of customer ($cust1_id)");
($res, $cl_collection_in) = $test_machine->request_get('/api/calllists/?page=1&rows=10&direction=in&customer_id='.$cust1_id);
is($res->code, 200, "fetch calllists collection of customer ($cust1_id) with direction filter in");
($res, $cl_collection_out) = $test_machine->request_get('/api/calllists/?page=1&rows=10&direction=out&customer_id='.$cust1_id);
is($res->code, 200, "fetch calllists collection of customer ($cust1_id) with direction filter out");
is($cl_collection_in->{total_count}+$cl_collection_out->{total_count}, $cl_collection->{total_count},
"Incoming and outgoing calls should add up to total number of calls");
diag("Total number of calls: " . $cl_collection->{total_count});
}
done_testing;
# vim: set tabstop=4 expandtab:
Loading…
Cancel
Save