parent
6dbb4b584a
commit
456df97df0
@ -0,0 +1,251 @@
|
||||
package NGCP::Panel::Controller::API::Customers;
|
||||
use Sipwise::Base;
|
||||
use namespace::sweep;
|
||||
use boolean qw(true);
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use MooseX::ClassAttribute qw(class_has);
|
||||
use NGCP::Panel::Utils::DateTime;
|
||||
use NGCP::Panel::Utils::Contract;
|
||||
use NGCP::Panel::Form::Contract::ProductSelect qw();
|
||||
use Path::Tiny qw(path);
|
||||
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::CheckTrailingSlash;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
with 'NGCP::Panel::Role::API';
|
||||
with 'NGCP::Panel::Role::API::Customers';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'customers');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/customers/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-customers');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => 'api_admin',
|
||||
Args => 0,
|
||||
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
|
||||
Method => $_,
|
||||
Path => __PACKAGE__->dispatch_path,
|
||||
} } @{ __PACKAGE__->allowed_methods }
|
||||
},
|
||||
action_roles => [qw(HTTPMethods)],
|
||||
);
|
||||
|
||||
sub auto :Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->set_body($c);
|
||||
$self->log_request($c);
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $page = $c->request->params->{page} // 1;
|
||||
my $rows = $c->request->params->{rows} // 10;
|
||||
{
|
||||
my $customers = NGCP::Panel::Utils::Contract::get_contract_rs(
|
||||
schema => $c->model('DB'),
|
||||
);
|
||||
$customers = $customers->search({
|
||||
'contact.reseller_id' => { '-not' => undef },
|
||||
},{
|
||||
join => 'contact'
|
||||
});
|
||||
|
||||
$customers = $customers->search({
|
||||
'-or' => [
|
||||
'product.class' => 'sipaccount',
|
||||
'product.class' => 'pbxaccount',
|
||||
],
|
||||
},{
|
||||
join => {'billing_mappings' => 'product' },
|
||||
'+select' => 'billing_mappings.id',
|
||||
'+as' => 'bmid',
|
||||
});
|
||||
|
||||
if($c->user->roles eq "api_admin") {
|
||||
} elsif($c->user->roles eq "api_reseller") {
|
||||
$customers = $customers->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id,
|
||||
});
|
||||
}
|
||||
|
||||
my $total_count = int($customers->count);
|
||||
$customers = $customers->search(undef, {
|
||||
page => $page,
|
||||
rows => $rows,
|
||||
});
|
||||
my (@embedded, @links);
|
||||
my $form = NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
for my $customer($customers->all) {
|
||||
push @embedded, $self->hal_from_customer($c, $customer, $form);
|
||||
push @links, Data::HAL::Link->new(
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
href => sprintf('/%s%d', $c->request->path, $customer->id),
|
||||
);
|
||||
}
|
||||
push @links,
|
||||
Data::HAL::Link->new(
|
||||
relation => 'curies',
|
||||
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
|
||||
name => 'ngcp',
|
||||
templated => true,
|
||||
),
|
||||
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
|
||||
Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s', $c->request->path, $page, $rows));
|
||||
|
||||
if(($total_count / $rows) > $page ) {
|
||||
push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows));
|
||||
}
|
||||
if($page > 1) {
|
||||
push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows));
|
||||
}
|
||||
|
||||
my $hal = Data::HAL->new(
|
||||
embedded => [@embedded],
|
||||
links => [@links],
|
||||
);
|
||||
$hal->resource({
|
||||
total_count => $total_count,
|
||||
});
|
||||
my $rname = $self->resource_name;
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
(map { # XXX Data::HAL must be able to generate links with multiple relations
|
||||
s|rel="(http://purl.org/sipwise/ngcp-api/#rel-$rname)"|rel="item $1"|;
|
||||
s/rel=self/rel="collection self"/;
|
||||
$_
|
||||
} $hal->http_headers),
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->body($response->content);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub HEAD :Allow {
|
||||
my ($self, $c) = @_;
|
||||
$c->forward(qw(GET));
|
||||
$c->response->body(q());
|
||||
return;
|
||||
}
|
||||
|
||||
sub OPTIONS :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $allowed_methods = $self->allowed_methods;
|
||||
$c->response->headers(HTTP::Headers->new(
|
||||
Allow => $allowed_methods->join(', '),
|
||||
Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name,
|
||||
));
|
||||
$c->response->content_type('application/json');
|
||||
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
|
||||
return;
|
||||
}
|
||||
|
||||
sub POST :Allow {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $schema = $c->model('DB');
|
||||
my $resource = $self->get_valid_post_data(
|
||||
c => $c,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
|
||||
my $product_class = delete $resource->{type};
|
||||
unless($product_class eq "sipaccount" || $product_class eq "pbxaccount") {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'type', must be 'sipaccount' or 'pbxaccount'.");
|
||||
last;
|
||||
}
|
||||
my $product = $schema->resultset('products')->find({ class => $product_class });
|
||||
unless($product) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'type'.");
|
||||
last;
|
||||
}
|
||||
unless(defined $resource->{billing_profile_id}) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', not defined.");
|
||||
last;
|
||||
}
|
||||
|
||||
# add product_id just for form check (not part of the actual contract item)
|
||||
# and remove it after the check
|
||||
$resource->{product_id} = $product->id;
|
||||
|
||||
$resource->{contact_id} //= undef;
|
||||
my $form = NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
last unless $self->validate_form(
|
||||
c => $c,
|
||||
resource => $resource,
|
||||
form => $form,
|
||||
);
|
||||
delete $resource->{product_id};
|
||||
|
||||
my $now = NGCP::Panel::Utils::DateTime::current_local;
|
||||
$resource->{create_timestamp} = $now;
|
||||
$resource->{modify_timestamp} = $now;
|
||||
my $customer;
|
||||
|
||||
my $billing_profile_id = delete $resource->{billing_profile_id};
|
||||
my $billing_profile = $schema->resultset('billing_profiles')->find($billing_profile_id);
|
||||
unless($billing_profile) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'.");
|
||||
last;
|
||||
}
|
||||
try {
|
||||
$customer = $schema->resultset('contracts')->create($resource);
|
||||
} catch($e) {
|
||||
$c->log->error("failed to create customer contract: $e"); # TODO: user, message, trace, ...
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create customer.");
|
||||
last;
|
||||
}
|
||||
|
||||
unless($customer->contact->reseller_id) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "The contact_id is not a valid ngcp:customercontacts item, but an ngcp:systemcontacts item");
|
||||
last;
|
||||
}
|
||||
unless($customer->contact->reseller_id == $billing_profile->reseller_id) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "The reseller of the contact doesn't match the reseller of the billing profile");
|
||||
last;
|
||||
}
|
||||
|
||||
try {
|
||||
$customer->billing_mappings->create({
|
||||
billing_profile_id => $billing_profile->id,
|
||||
product_id => $product->id,
|
||||
});
|
||||
NGCP::Panel::Utils::Contract::create_contract_balance(
|
||||
c => $c,
|
||||
profile => $billing_profile,
|
||||
contract => $customer,
|
||||
);
|
||||
} catch($e) {
|
||||
$c->log->error("failed to create customer contract: $e"); # TODO: user, message, trace, ...
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create customer.");
|
||||
last;
|
||||
}
|
||||
|
||||
$guard->commit;
|
||||
|
||||
$c->response->status(HTTP_CREATED);
|
||||
$c->response->header(Location => sprintf('/%s%d', $c->request->path, $customer->id));
|
||||
$c->response->body(q());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,211 @@
|
||||
package NGCP::Panel::Controller::API::CustomersItem;
|
||||
use Sipwise::Base;
|
||||
use namespace::sweep;
|
||||
use boolean qw(true);
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use MooseX::ClassAttribute qw(class_has);
|
||||
use NGCP::Panel::Form::Contract::ProductSelect qw();
|
||||
use NGCP::Panel::Utils::ValidateJSON qw();
|
||||
use NGCP::Panel::Utils::DateTime;
|
||||
use Path::Tiny qw(path);
|
||||
use Safe::Isa qw($_isa);
|
||||
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
with 'NGCP::Panel::Role::API';
|
||||
with 'NGCP::Panel::Role::API::Customers';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'customers');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/customers/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-customers');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => 'api_admin',
|
||||
Args => 1,
|
||||
Does => [qw(ACL RequireSSL)],
|
||||
Method => $_,
|
||||
Path => __PACKAGE__->dispatch_path,
|
||||
} } @{ __PACKAGE__->allowed_methods }
|
||||
},
|
||||
action_roles => [qw(HTTPMethods)],
|
||||
);
|
||||
|
||||
sub auto :Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->set_body($c);
|
||||
$self->log_request($c);
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
{
|
||||
last unless $self->valid_id($c, $id);
|
||||
my $customer = $self->customer_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, customer => $customer);
|
||||
|
||||
my $hal = $self->hal_from_customer($c, $customer);
|
||||
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
(map { # XXX Data::HAL must be able to generate links with multiple relations
|
||||
s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|;
|
||||
s/rel=self/rel="item self"/;
|
||||
$_
|
||||
} $hal->http_headers),
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->body($response->content);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub HEAD :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
$c->forward(qw(GET));
|
||||
$c->response->body(q());
|
||||
return;
|
||||
}
|
||||
|
||||
sub OPTIONS :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $allowed_methods = $self->allowed_methods;
|
||||
$c->response->headers(HTTP::Headers->new(
|
||||
Allow => $allowed_methods->join(', '),
|
||||
Accept_Patch => 'application/json-patch+json',
|
||||
));
|
||||
$c->response->content_type('application/json');
|
||||
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
|
||||
return;
|
||||
}
|
||||
|
||||
sub PATCH :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $json = $self->get_valid_patch_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json-patch+json',
|
||||
);
|
||||
last unless $json;
|
||||
|
||||
my $customer = $self->customer_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, customer => $customer);
|
||||
|
||||
my $old_resource = { $customer->get_inflated_columns };
|
||||
my $billing_mapping = $customer->billing_mappings->find($customer->get_column('bmid'));
|
||||
$old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id;
|
||||
|
||||
my $resource = $self->apply_patch($c, $old_resource, $json);
|
||||
last unless $resource;
|
||||
|
||||
my $form = NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
$customer = $self->update_customer($c, $customer, $old_resource, $resource, $form);
|
||||
last unless $customer;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
my $hal = $self->hal_from_customer($c, $customer, $form);
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
$hal->http_headers,
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->header(Preference_Applied => 'return=representation');
|
||||
$c->response->body($response->content);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub PUT :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $customer = $self->customer_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, customer => $customer);
|
||||
my $resource = $self->get_valid_put_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
my $old_resource = { $customer->get_inflated_columns };
|
||||
|
||||
my $form = NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
$customer = $self->update_customer($c, $customer, $old_resource, $resource, $form);
|
||||
last unless $customer;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
my $hal = $self->hal_from_customer($c, $customer, $form);
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
$hal->http_headers,
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->header(Preference_Applied => 'return=representation');
|
||||
$c->response->body($response->content);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
=pod
|
||||
# we don't allow to delete customers
|
||||
sub DELETE :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $customer = $self->customer_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, customer => $customer);
|
||||
|
||||
# TODO: do we want to prevent deleting used customers?
|
||||
#my $customer_count = $c->model('DB')->resultset('customers')->search({
|
||||
# contact_id => $id
|
||||
#});
|
||||
#if($customer_count > 0) {
|
||||
# $self->error($c, HTTP_LOCKED, "Contact is still in use.");
|
||||
# last;
|
||||
#} else {
|
||||
$customer->delete;
|
||||
#}
|
||||
$guard->commit;
|
||||
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->body(q());
|
||||
}
|
||||
return;
|
||||
}
|
||||
=cut
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,192 @@
|
||||
package NGCP::Panel::Role::API::Customers;
|
||||
use Moose::Role;
|
||||
use Sipwise::Base;
|
||||
|
||||
use boolean qw(true);
|
||||
use Try::Tiny;
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use NGCP::Panel::Utils::DateTime;
|
||||
use NGCP::Panel::Utils::Contract;
|
||||
use NGCP::Panel::Form::Contract::ProductSelect qw();
|
||||
|
||||
sub hal_from_customer {
|
||||
my ($self, $c, $customer, $form) = @_;
|
||||
|
||||
my $billing_mapping = $customer->billing_mappings->find($customer->get_column('bmid'));
|
||||
my $billing_profile_id = $billing_mapping->billing_profile->id;
|
||||
my $stime = NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month');
|
||||
my $etime = $stime->clone->add(months => 1);
|
||||
my $contract_balance = $customer->contract_balances
|
||||
->find({
|
||||
start => { '>=' => $stime },
|
||||
end => { '<' => $etime },
|
||||
});
|
||||
unless($contract_balance) {
|
||||
try {
|
||||
NGCP::Panel::Utils::Contract::create_contract_balance(
|
||||
c => $c,
|
||||
profile => $billing_mapping->billing_profile,
|
||||
contract => $customer,
|
||||
);
|
||||
} catch {
|
||||
$self->log->error("Failed to create current contract balance for customer contract id '".$customer->id."': $_");
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error.");
|
||||
return;
|
||||
};
|
||||
$contract_balance = $customer->contract_balances->find({
|
||||
start => { '>=' => $stime },
|
||||
end => { '<' => $etime },
|
||||
});
|
||||
}
|
||||
|
||||
my %resource = $customer->get_inflated_columns;
|
||||
|
||||
my $hal = Data::HAL->new(
|
||||
links => [
|
||||
Data::HAL::Link->new(
|
||||
relation => 'curies',
|
||||
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
|
||||
name => 'ngcp',
|
||||
templated => true,
|
||||
),
|
||||
Data::HAL::Link->new(relation => 'collection', href => sprintf('/api/%s/', $self->resource_name)),
|
||||
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
|
||||
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $customer->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:customercontacts', href => sprintf("/api/customercontacts/%d", $customer->contact->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:billingprofiles', href => sprintf("/api/billingprofiles/%d", $billing_profile_id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:contractbalances', href => sprintf("/api/contractbalances/%d", $contract_balance->id)),
|
||||
],
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
);
|
||||
|
||||
$form //= NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
return unless $self->validate_form(
|
||||
c => $c,
|
||||
form => $form,
|
||||
resource => \%resource,
|
||||
run => 0,
|
||||
);
|
||||
|
||||
# return the virtual "type" instead of the actual product id
|
||||
delete $resource{product_id};
|
||||
$resource{type} = $billing_mapping->product->class;
|
||||
|
||||
$resource{id} = int($customer->id);
|
||||
$resource{billing_profile_id} = int($billing_profile_id);
|
||||
$hal->resource({%resource});
|
||||
return $hal;
|
||||
}
|
||||
|
||||
sub customer_by_id {
|
||||
my ($self, $c, $id) = @_;
|
||||
|
||||
# we only return customers, that is, contracts with contacts with a
|
||||
# reseller
|
||||
my $customers = NGCP::Panel::Utils::Contract::get_contract_rs(
|
||||
schema => $c->model('DB'),
|
||||
);
|
||||
$customers = $customers->search({
|
||||
'contact.reseller_id' => { '-not' => undef },
|
||||
},{
|
||||
join => 'contact'
|
||||
});
|
||||
|
||||
$customers = $customers->search({
|
||||
'-or' => [
|
||||
'product.class' => 'sipaccount',
|
||||
'product.class' => 'pbxaccount',
|
||||
],
|
||||
},{
|
||||
join => {'billing_mappings' => 'product' },
|
||||
'+select' => 'billing_mappings.id',
|
||||
'+as' => 'bmid',
|
||||
});
|
||||
|
||||
if($c->user->roles eq "api_admin") {
|
||||
} elsif($c->user->roles eq "api_reseller") {
|
||||
$customers = $customers->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id,
|
||||
});
|
||||
}
|
||||
|
||||
return $customers->find($id);
|
||||
}
|
||||
|
||||
sub update_customer {
|
||||
my ($self, $c, $customer, $old_resource, $resource, $form) = @_;
|
||||
|
||||
my $billing_mapping = $customer->billing_mappings->find($customer->get_column('bmid'));
|
||||
$old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id;
|
||||
unless($resource->{billing_profile_id}) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', not defined");
|
||||
return;
|
||||
}
|
||||
|
||||
$form //= NGCP::Panel::Form::Contract::ProductSelect->new;
|
||||
# TODO: for some reason, formhandler lets missing contact_id slip thru
|
||||
$resource->{contact_id} //= undef;
|
||||
return unless $self->validate_form(
|
||||
c => $c,
|
||||
form => $form,
|
||||
resource => $resource,
|
||||
);
|
||||
|
||||
my $now = NGCP::Panel::Utils::DateTime::current_local;
|
||||
$resource->{modify_timestamp} = $now;
|
||||
my $billing_profile;
|
||||
|
||||
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;
|
||||
}
|
||||
$customer->billing_mappings->create({
|
||||
start_date => NGCP::Panel::Utils::DateTime::current_local,
|
||||
billing_profile_id => $resource->{billing_profile_id},
|
||||
product_id => $billing_mapping->product_id,
|
||||
});
|
||||
}
|
||||
delete $resource->{billing_profile_id};
|
||||
|
||||
|
||||
if($old_resource->{contact_id} != $resource->{contact_id}) {
|
||||
my $custcontact = $c->model('DB')->resultset('contacts')
|
||||
->search({ reseller_id => { '-not' => undef }})
|
||||
->find($resource->{contact_id});
|
||||
unless($custcontact) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'contact_id', doesn't exist");
|
||||
return;
|
||||
}
|
||||
unless($billing_profile->reseller_id == $custcontact->reseller_id) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'contact_id', reseller doesn't match billing profile reseller");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$customer->update($resource);
|
||||
|
||||
if($old_resource->{status} ne $resource->{status}) {
|
||||
if($customer->id == 1) {
|
||||
$self->error($c, HTTP_FORBIDDEN, "Cannot set customer status to '".$resource->{status}."' for customer id '1'");
|
||||
return;
|
||||
}
|
||||
NGCP::Panel::Utils::Contract::recursively_lock_contract(
|
||||
c => $c,
|
||||
contract => $customer,
|
||||
);
|
||||
}
|
||||
|
||||
# TODO: what about changed product, do we allow it?
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -1 +1,433 @@
|
||||
# TODO: try to unset reseller_id of contact of a customer contract, which should fail
|
||||
# TODO: try to set reseller_id of contact of a system customer, which should fail
|
||||
|
||||
use Sipwise::Base;
|
||||
use Net::Domain qw(hostfqdn);
|
||||
use LWP::UserAgent;
|
||||
use JSON qw();
|
||||
use Test::More;
|
||||
|
||||
my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443');
|
||||
|
||||
my $valid_ssl_client_cert = $ENV{API_SSL_CLIENT_CERT} ||
|
||||
"/etc/ssl/ngcp/api/NGCP-API-client-certificate.pem";
|
||||
my $valid_ssl_client_key = $ENV{API_SSL_CLIENT_KEY} ||
|
||||
$valid_ssl_client_cert;
|
||||
my $ssl_ca_cert = $ENV{API_SSL_CA_CERT} || "/etc/ssl/ngcp/api/ca-cert.pem";
|
||||
|
||||
my ($ua, $req, $res);
|
||||
$ua = LWP::UserAgent->new;
|
||||
|
||||
$ua->ssl_opts(
|
||||
SSL_cert_file => $valid_ssl_client_cert,
|
||||
SSL_key_file => $valid_ssl_client_key,
|
||||
SSL_ca_file => $ssl_ca_cert,
|
||||
);
|
||||
|
||||
# OPTIONS tests
|
||||
{
|
||||
$req = HTTP::Request->new('OPTIONS', $uri.'/api/customers/');
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "check options request");
|
||||
ok($res->header('Accept-Post') eq "application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-customers", "check Accept-Post header in options response");
|
||||
my $opts = JSON::from_json($res->decoded_content);
|
||||
my @hopts = split /\s*,\s*/, $res->header('Allow');
|
||||
ok(exists $opts->{methods} && ref $opts->{methods} eq "ARRAY", "check for valid 'methods' in body");
|
||||
foreach my $opt(qw( GET HEAD OPTIONS POST )) {
|
||||
ok(grep(/^$opt$/, @hopts), "check for existence of '$opt' in Allow header");
|
||||
ok(grep(/^$opt$/, @{ $opts->{methods} }), "check for existence of '$opt' in body");
|
||||
}
|
||||
}
|
||||
|
||||
my $t = time;
|
||||
my $reseller_id = 1;
|
||||
|
||||
$req = HTTP::Request->new('POST', $uri.'/api/billingprofiles/');
|
||||
$req->header('Content-Type' => 'application/json');
|
||||
$req->header('Prefer' => 'return=representation');
|
||||
$req->content(JSON::to_json({
|
||||
name => "test profile $t",
|
||||
handle => "testprofile$t",
|
||||
reseller_id => $reseller_id,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 201, "create test billing profile");
|
||||
# TODO: get id from body once the API returns it
|
||||
my $billing_profile_id = $res->header('Location');
|
||||
$billing_profile_id =~ s/^.+\/(\d+)$/$1/;
|
||||
|
||||
# fetch a system contact for later tests
|
||||
$req = HTTP::Request->new('GET', $uri.'/api/systemcontacts/?page=1&rows=1');
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "fetch system contacts");
|
||||
my $sysct = JSON::from_json($res->decoded_content);
|
||||
my $system_contact_id = $sysct->{_embedded}->{'ngcp:systemcontacts'}->{id};
|
||||
|
||||
# collection test
|
||||
my $firstcustomer = undef;
|
||||
my $custcontact = undef;
|
||||
my @allcustomers = ();
|
||||
{
|
||||
# first, create a contact
|
||||
$req = HTTP::Request->new('POST', $uri.'/api/customercontacts/');
|
||||
$req->header('Content-Type' => 'application/json');
|
||||
$req->content(JSON::to_json({
|
||||
firstname => "cust_contact_first",
|
||||
lastname => "cust_contact_last",
|
||||
email => "cust_contact\@custcontact.invalid",
|
||||
reseller_id => $reseller_id,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 201, "create customer contact");
|
||||
$req = HTTP::Request->new('GET', $uri.'/'.$res->header('Location'));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "fetch system contact");
|
||||
$custcontact = JSON::from_json($res->decoded_content);
|
||||
|
||||
# create 6 new customers
|
||||
my %customers = ();
|
||||
for(my $i = 1; $i <= 6; ++$i) {
|
||||
$req = HTTP::Request->new('POST', $uri.'/api/customers/');
|
||||
$req->header('Content-Type' => 'application/json');
|
||||
$req->content(JSON::to_json({
|
||||
status => "active",
|
||||
contact_id => $custcontact->{id},
|
||||
type => "sipaccount",
|
||||
billing_profile_id => $billing_profile_id,
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 201, "create test customer $i");
|
||||
$customers{$res->header('Location')} = 1;
|
||||
push @allcustomers, $res->header('Location');
|
||||
$firstcustomer = $res->header('Location') unless $firstcustomer;
|
||||
}
|
||||
|
||||
# try to create invalid customer with wrong type
|
||||
$req = HTTP::Request->new('POST', $uri.'/api/customers/');
|
||||
$req->header('Content-Type' => 'application/json');
|
||||
$req->content(JSON::to_json({
|
||||
status => "active",
|
||||
contact_id => $custcontact->{id},
|
||||
billing_profile_id => $billing_profile_id,
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
type => "invalid",
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer with invalid type");
|
||||
my $err = JSON::from_json($res->decoded_content);
|
||||
ok($err->{code} eq "422", "check error code in body");
|
||||
ok($err->{message} =~ /Invalid 'type'/, "check error message in body");
|
||||
|
||||
# try to create invalid customer with wrong billing profile
|
||||
$req->content(JSON::to_json({
|
||||
status => "active",
|
||||
contact_id => $custcontact->{id},
|
||||
type => "sipaccount",
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
billing_profile_id => 999999,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer with invalid billing profile");
|
||||
$err = JSON::from_json($res->decoded_content);
|
||||
ok($err->{code} eq "422", "check error code in body");
|
||||
ok($err->{message} =~ /Invalid 'billing_profile_id'/, "check error message in body");
|
||||
|
||||
# try to create invalid customer with systemcontact
|
||||
$req->content(JSON::to_json({
|
||||
status => "active",
|
||||
type => "sipaccount",
|
||||
billing_profile_id => $billing_profile_id,
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
contact_id => $system_contact_id,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer with invalid contact");
|
||||
$err = JSON::from_json($res->decoded_content);
|
||||
ok($err->{code} eq "422", "check error code in body");
|
||||
ok($err->{message} =~ /The contact_id is not a valid ngcp:customercontacts item/, "check error message in body");
|
||||
|
||||
# try to create invalid customer without contact
|
||||
$req->content(JSON::to_json({
|
||||
status => "active",
|
||||
type => "sipaccount",
|
||||
billing_profile_id => $billing_profile_id,
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer without contact");
|
||||
|
||||
# try to create invalid customer with invalid status
|
||||
$req->content(JSON::to_json({
|
||||
type => "sipaccount",
|
||||
billing_profile_id => $billing_profile_id,
|
||||
contact_id => $custcontact->{id},
|
||||
max_subscribers => undef,
|
||||
external_id => undef,
|
||||
status => "invalid",
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer with invalid status");
|
||||
$err = JSON::from_json($res->decoded_content);
|
||||
ok($err->{code} eq "422", "check error code in body");
|
||||
ok($err->{message} =~ /field='status'/, "check error message in body");
|
||||
|
||||
# try to create invalid customer with invalid max_subscribers
|
||||
$req->content(JSON::to_json({
|
||||
type => "sipaccount",
|
||||
billing_profile_id => $billing_profile_id,
|
||||
contact_id => $custcontact->{id},
|
||||
max_subscribers => "abc",
|
||||
external_id => undef,
|
||||
status => "active",
|
||||
}));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "create customer with invalid max_subscribers");
|
||||
$err = JSON::from_json($res->decoded_content);
|
||||
ok($err->{code} eq "422", "check error code in body");
|
||||
ok($err->{message} =~ /field='max_subscribers'/, "check error message in body");
|
||||
|
||||
# iterate over customers collection to check next/prev links and status
|
||||
my $nexturi = $uri.'/api/customers/?page=1&rows=5';
|
||||
do {
|
||||
$res = $ua->get($nexturi);
|
||||
ok($res->code == 200, "fetch contacts page");
|
||||
my $collection = JSON::from_json($res->decoded_content);
|
||||
my $selfuri = $uri . $collection->{_links}->{self}->{href};
|
||||
ok($selfuri eq $nexturi, "check _links.self.href of collection");
|
||||
my $colluri = URI->new($selfuri);
|
||||
|
||||
ok($collection->{total_count} > 0, "check 'total_count' of collection");
|
||||
|
||||
my %q = $colluri->query_form;
|
||||
ok(exists $q{page} && exists $q{rows}, "check existence of 'page' and 'row' in 'self'");
|
||||
my $page = int($q{page});
|
||||
my $rows = int($q{rows});
|
||||
if($page == 1) {
|
||||
ok(!exists $collection->{_links}->{prev}->{href}, "check absence of 'prev' on first page");
|
||||
} else {
|
||||
ok(exists $collection->{_links}->{prev}->{href}, "check existence of 'prev'");
|
||||
}
|
||||
if(($collection->{total_count} / $rows) <= $page) {
|
||||
ok(!exists $collection->{_links}->{next}->{href}, "check absence of 'next' on last page");
|
||||
} else {
|
||||
ok(exists $collection->{_links}->{next}->{href}, "check existence of 'next'");
|
||||
}
|
||||
|
||||
if($collection->{_links}->{next}->{href}) {
|
||||
$nexturi = $uri . $collection->{_links}->{next}->{href};
|
||||
} else {
|
||||
$nexturi = undef;
|
||||
}
|
||||
|
||||
# TODO: I'd expect that to be an array ref in any case!
|
||||
ok((ref $collection->{_links}->{'ngcp:customers'} eq "ARRAY" ||
|
||||
ref $collection->{_links}->{'ngcp:customers'} eq "HASH"), "check if 'ngcp:customers' is array/hash-ref");
|
||||
|
||||
# remove any contact we find in the collection for later check
|
||||
if(ref $collection->{_links}->{'ngcp:customers'} eq "HASH") {
|
||||
ok($collection->{_embedded}->{'ngcp:customers'}->{type} eq "sipaccount" || $collection->{_embedded}->{'ngcp:customers'}->{type} eq "pbxaccount", "check for correct customer contract type");
|
||||
ok($collection->{_embedded}->{'ngcp:customers'}->{status} ne "terminated", "check if we don't have terminated customers in response");
|
||||
ok(exists $collection->{_embedded}->{'ngcp:customers'}->{_links}->{'ngcp:customercontacts'}, "check presence of ngcp:customercontacts relation");
|
||||
ok(exists $collection->{_embedded}->{'ngcp:customers'}->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation");
|
||||
ok(exists $collection->{_embedded}->{'ngcp:customers'}->{_links}->{'ngcp:contractbalances'}, "check presence of ngcp:contractbalances relation");
|
||||
delete $customers{$collection->{_links}->{'ngcp:customers'}->{href}};
|
||||
} else {
|
||||
foreach my $c(@{ $collection->{_links}->{'ngcp:customers'} }) {
|
||||
delete $customers{$c->{href}};
|
||||
}
|
||||
foreach my $c(@{ $collection->{_embedded}->{'ngcp:customers'} }) {
|
||||
ok($c->{type} eq "sipaccount" || $c->{type} eq "pbxaccount", "check for correct customer contract type");
|
||||
ok($c->{status} ne "terminated", "check if we don't have terminated customers in response");
|
||||
ok(exists $c->{_links}->{'ngcp:customercontacts'}, "check presence of ngcp:customercontacts relation");
|
||||
ok(exists $c->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation");
|
||||
ok(exists $c->{_links}->{'ngcp:contractbalances'}, "check presence of ngcp:contractbalances relation");
|
||||
|
||||
delete $customers{$c->{_links}->{self}->{href}};
|
||||
}
|
||||
}
|
||||
|
||||
} while($nexturi);
|
||||
|
||||
ok(keys %customers == 0, "check if all test customers have been found");
|
||||
}
|
||||
|
||||
# test contacts item
|
||||
{
|
||||
$req = HTTP::Request->new('OPTIONS', $uri.'/'.$firstcustomer);
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "check options on item");
|
||||
my @hopts = split /\s*,\s*/, $res->header('Allow');
|
||||
my $opts = JSON::from_json($res->decoded_content);
|
||||
ok(exists $opts->{methods} && ref $opts->{methods} eq "ARRAY", "check for valid 'methods' in body");
|
||||
foreach my $opt(qw( GET HEAD OPTIONS PUT PATCH )) {
|
||||
ok(grep(/^$opt$/, @hopts), "check for existence of '$opt' in Allow header");
|
||||
ok(grep(/^$opt$/, @{ $opts->{methods} }), "check for existence of '$opt' in body");
|
||||
}
|
||||
foreach my $opt(qw( POST DELETE )) {
|
||||
ok(!grep(/^$opt$/, @hopts), "check for absence of '$opt' in Allow header");
|
||||
ok(!grep(/^$opt$/, @{ $opts->{methods} }), "check for absence of '$opt' in body");
|
||||
}
|
||||
|
||||
$req = HTTP::Request->new('GET', $uri.'/'.$firstcustomer);
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "fetch one customer item");
|
||||
my $customer = JSON::from_json($res->decoded_content);
|
||||
ok(exists $customer->{status}, "check existence of status");
|
||||
ok(exists $customer->{type}, "check existence of type");
|
||||
ok(exists $customer->{billing_profile_id} && $customer->{billing_profile_id}->is_int, "check existence of billing_profile_id");
|
||||
ok(exists $customer->{contact_id} && $customer->{contact_id}->is_int, "check existence of contact_id");
|
||||
ok(exists $customer->{id} && $customer->{id}->is_int, "check existence of id");
|
||||
ok(exists $customer->{max_subscribers}, "check existence of max_subscribers");
|
||||
ok(!exists $customer->{product_id}, "check absence of product_id");
|
||||
|
||||
# PUT same result again
|
||||
my $old_customer = { %$customer };
|
||||
delete $customer->{_links};
|
||||
delete $customer->{_embedded};
|
||||
$req = HTTP::Request->new('PUT', $uri.'/'.$firstcustomer);
|
||||
|
||||
# check if it fails without content type
|
||||
$req->remove_header('Content-Type');
|
||||
$req->header('Prefer' => "return=minimal");
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 415, "check put missing content type");
|
||||
|
||||
# check if it fails with unsupported content type
|
||||
$req->header('Content-Type' => 'application/xxx');
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 415, "check put invalid content type");
|
||||
|
||||
$req->remove_header('Content-Type');
|
||||
$req->header('Content-Type' => 'application/json');
|
||||
|
||||
# check if it fails with missing Prefer
|
||||
$req->remove_header('Prefer');
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 400, "check put missing prefer");
|
||||
|
||||
# check if it fails with invalid Prefer
|
||||
$req->header('Prefer' => "return=invalid");
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 400, "check put invalid prefer");
|
||||
|
||||
|
||||
$req->remove_header('Prefer');
|
||||
$req->header('Prefer' => "return=representation");
|
||||
|
||||
# check if it fails with missing body
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 400, "check put no body");
|
||||
|
||||
# check if put is ok
|
||||
$req->content(JSON::to_json($customer));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "check put successful");
|
||||
|
||||
my $new_customer = JSON::from_json($res->decoded_content);
|
||||
is_deeply($old_customer, $new_customer, "check put if unmodified put returns the same");
|
||||
|
||||
# check if we have the proper links
|
||||
ok(exists $new_customer->{_links}->{'ngcp:customercontacts'}, "check put presence of ngcp:customercontacts relation");
|
||||
ok(exists $new_customer->{_links}->{'ngcp:billingprofiles'}, "check put presence of ngcp:billingprofiles relation");
|
||||
ok(exists $new_customer->{_links}->{'ngcp:contractbalances'}, "check put presence of ngcp:contractbalances relation");
|
||||
|
||||
$req = HTTP::Request->new('PATCH', $uri.'/'.$firstcustomer);
|
||||
$req->header('Prefer' => 'return=representation');
|
||||
$req->header('Content-Type' => 'application/json-patch+json');
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/status', value => 'pending' } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "check patched customer item");
|
||||
my $mod_contact = JSON::from_json($res->decoded_content);
|
||||
ok($mod_contact->{status} eq "pending", "check patched replace op");
|
||||
ok($mod_contact->{_links}->{self}->{href} eq $firstcustomer, "check patched self link");
|
||||
ok($mod_contact->{_links}->{collection}->{href} eq '/api/customers/', "check patched collection link");
|
||||
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/status', value => undef } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched undef status");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/status', value => 'invalid' } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched invalid status");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/contact_id', value => 99999 } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched invalid contact_id");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/contact_id', value => $system_contact_id } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched system contact_id");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/billing_profile_id', value => undef } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched undef billing_profile_id");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/billing_profile_id', value => 99999 } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched invalid billing_profile_id");
|
||||
|
||||
$req->content(JSON::to_json(
|
||||
[ { op => 'replace', path => '/max_subscribers', value => "abc" } ]
|
||||
));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 422, "check patched invalid max_subscribers");
|
||||
}
|
||||
|
||||
# terminate
|
||||
{
|
||||
# check if deletion of contact fails before terminating the customers
|
||||
$req = HTTP::Request->new('DELETE', $uri.'/'.$custcontact->{_links}->{self}->{href});
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 423, "check locked status for deleting used contact");
|
||||
|
||||
my $pc;
|
||||
foreach my $customer(@allcustomers) {
|
||||
$req = HTTP::Request->new('PATCH', $uri.'/'.$customer);
|
||||
$req->header('Content-Type' => 'application/json-patch+json');
|
||||
$req->header('Prefer' => 'return=representation');
|
||||
$req->content(JSON::to_json([
|
||||
{ "op" => "replace", "path" => "/status", "value" => "terminated" }
|
||||
]));
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 200, "check termination of customer");
|
||||
$pc = JSON::from_json($res->decoded_content);
|
||||
ok($pc->{status} eq "terminated", "check termination status of customer");
|
||||
}
|
||||
|
||||
# check if we can still get the terminated customer
|
||||
$req = HTTP::Request->new('GET', $uri.'/'.$pc->{_links}->{self}->{href});
|
||||
$res = $ua->request($req);
|
||||
ok($res->code == 404, "check fetching of terminated customer");
|
||||
|
||||
# check if deletion of contact is now ok
|
||||
# TODO: are we supposed to be able to delete a contact for a terminated
|
||||
# customer? there are still DB contstraints in the way!
|
||||
#$req = HTTP::Request->new('DELETE', $uri.'/'.$custcontact->{_links}->{self}->{href});
|
||||
#$res = $ua->request($req);
|
||||
#ok($res->code == 204, "check deletion of unused contact");
|
||||
}
|
||||
|
||||
done_testing;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
|
||||
Loading…
Reference in new issue