TT#28457 Remove inherited code from faxserversettings

Change-Id: I697eb79ccd04ef6e5f3d0de5297be840cb23b731
changes/15/22515/31
Irina Peshinskaya 7 years ago
parent ffd1b487bf
commit 47ebd02be5

@ -3,12 +3,11 @@ use NGCP::Panel::Utils::Generic qw(:all);
use Sipwise::Base;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::FaxserverSettings/;
__PACKAGE__->set_config({
allowed_roles => [qw/admin reseller subscriber subscriberadmin/],
});
sub allowed_methods{
return [qw/GET OPTIONS HEAD/];
@ -16,7 +15,7 @@ sub allowed_methods{
sub api_description {
return 'Specifies faxserver settings for a specific subscriber.';
};
}
sub query_params {
return [
@ -56,67 +55,6 @@ sub query_params {
];
}
use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::FaxserverSettings/;
sub resource_name{
return 'faxserversettings';
}
sub dispatch_path{
return '/api/faxserversettings/';
}
sub relation{
return 'http://purl.org/sipwise/ngcp-api/#rel-faxserversettings';
}
__PACKAGE__->set_config({
allowed_roles => [qw/admin reseller subscriber subscriberadmin/],
});
sub GET :Allow {
my ($self, $c) = @_;
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
{
my $cfs = $self->item_rs($c);
(my $total_count, $cfs) = $self->paginate_order_collection($c, $cfs);
my (@embedded, @links);
for my $cf ($cfs->all) {
try {
push @embedded, $self->hal_from_item($c, $cf);
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('%s%s', $self->dispatch_path, $cf->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/'),
$self->collection_nav_links($c, $page, $rows, $total_count, $c->request->path, $c->request->query_params);
my $hal = Data::HAL->new(
embedded => [@embedded],
links => [@links],
);
$hal->resource({
total_count => $total_count,
});
my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -3,165 +3,79 @@ use NGCP::Panel::Utils::Generic qw(:all);
use Sipwise::Base;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use NGCP::Panel::Utils::API::Subscribers;
use NGCP::Panel::Utils::ValidateJSON qw();
use Clone qw/clone/;
require Catalyst::ActionRole::ACL;
require NGCP::Panel::Role::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::FaxserverSettings/;
__PACKAGE__->set_config({
allowed_roles => {
Default => [qw/admin reseller subscriber subscriberadmin/],
Journal => [qw/admin reseller subscriber subscriberadmin/],
},
PATCH => { ops => [qw/add replace remove copy/] },
});
sub allowed_methods{
return [qw/GET OPTIONS HEAD PATCH PUT/];
}
use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::FaxserverSettings/;
sub resource_name{
return 'faxserversettings';
sub journal_query_params {
my($self,$query_params) = @_;
return $self->get_journal_query_params($query_params);
}
sub dispatch_path{
return '/api/faxserversettings/';
sub get_journal_methods{
return [qw/handle_item_base_journal handle_journals_get handle_journalsitem_get handle_journals_options handle_journalsitem_options handle_journals_head handle_journalsitem_head/];
}
sub relation{
return 'http://purl.org/sipwise/ngcp-api/#rel-faxserversettings';
}
sub journal_query_params {
my($self,$query_params) = @_;
return $self->get_journal_query_params($query_params);
}
sub update_item_model {
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
__PACKAGE__->set_config({
allowed_roles => {
Default => [qw/admin reseller subscriber subscriberadmin/],
Journal => [qw/admin reseller subscriber subscriberadmin/],
my $billing_subscriber = NGCP::Panel::Utils::API::Subscribers::get_active_subscriber($self, $c, $item->id);
unless($billing_subscriber) {
$c->log->error("invalid subscriber id $item->id for fax send");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Fax subscriber not found.");
return;
}
});
sub GET :Allow {
my ($self, $c, $id) = @_;
{
last unless $self->valid_id($c, $id);
my $subs = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, faxserversettings => $subs);
my $hal = $self->hal_from_item($c, $subs);
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"|r
=~ s/rel=self/rel="item self"/r;
} $hal->http_headers),
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
delete $resource->{id};
my $prov_subs = $item->provisioning_voip_subscriber;
die "need provisioning_voip_subscriber" unless $prov_subs;
my $prov_subscriber_id = $prov_subs->id;
my $destinations_rs = $prov_subs->voip_fax_destinations;
if (! exists $resource->{destinations} ) {
$resource->{destinations} = [];
}
if (ref $resource->{destinations} ne "ARRAY") {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'destinations'. Must be an array.");
return;
}
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',
ops => [qw/add replace remove copy/],
);
last unless $json;
my $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, faxserversettings => $item);
my $old_resource = $self->hal_from_item($c, $item)->resource;
$old_resource = clone($old_resource);
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
my $form = $self->get_form($c);
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
last unless $item;
my $hal = $self->hal_from_item($c, $item);
last unless $self->add_update_journal_item_hal($c,{ hal => $hal, id => $item->id });
$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_item($c, $item);
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;
}
my %update_fields = %{ $resource };
delete $update_fields{destinations};
try {
$prov_subs->delete_related('voip_fax_preference');
$destinations_rs->delete;
$prov_subs->create_related('voip_fax_preference', \%update_fields);
$prov_subs->discard_changes; #reload
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 $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, faxserversettings => $item);
my $resource = $self->get_valid_put_data(
c => $c,
id => $id,
media_type => 'application/json',
);
last unless $resource;
my $old_resource = undef;
my $form = $self->get_form($c);
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
last unless $item;
my $hal = $self->hal_from_item($c, $item);
last unless $self->add_update_journal_item_hal($c,{ hal => $hal, id => $item->id });
$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_item($c, $item);
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);
for my $dest (@{ $resource->{destinations} }) {
$destinations_rs->create($dest);
}
}
return;
}
} catch($e) {
$c->log->error("Error Updating faxserversettings: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "faxserversettings could not be updated.");
return;
};
sub get_journal_methods{
return [qw/handle_item_base_journal handle_journals_get handle_journalsitem_get handle_journals_options handle_journalsitem_options handle_journals_head handle_journalsitem_head/];
return $item;
}
1;
# vim: set tabstop=4 expandtab:

@ -5,7 +5,6 @@ use Sipwise::Base;
use parent 'NGCP::Panel::Role::API';
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
@ -14,74 +13,16 @@ use JSON::Types;
use NGCP::Panel::Utils::Subscriber;
use NGCP::Panel::Utils::API::Subscribers;
sub resource_name{
return 'faxserversettings';
}
sub get_form {
my ($self, $c, $type) = @_;
return NGCP::Panel::Form::get("NGCP::Panel::Form::Faxserver::API", $c);
}
sub hal_from_item {
my ($self, $c, $item) = @_;
my $form;
my $rwr_form = $self->get_form($c);
my $type = 'faxserversettings';
my $prov_subs = $item->provisioning_voip_subscriber;
die "no provisioning_voip_subscriber" unless $prov_subs;
my $fax_preference = $prov_subs->voip_fax_preference;
unless ($fax_preference) {
try {
$fax_preference = $prov_subs->create_related('voip_fax_preference', {});
$fax_preference->discard_changes; # reload
} catch($e) {
$c->log->error("Error creating empty fax_preference on get");
};
}
my %resource = (
$fax_preference ? $fax_preference->get_inflated_columns : (),
subscriber_id => $item->id,
);
delete $resource{id};
my @destinations;
for my $dest ($prov_subs->voip_fax_destinations->all) {
push @destinations, {$dest->get_inflated_columns};
}
$resource{destinations} = \@destinations;
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("%s", $self->dispatch_path)),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%s", $self->dispatch_path, $item->id)),
Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%s", $type, $item->id)),
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->id)),
$self->get_journal_relation_link($item->id),
],
relation => 'ngcp:'.$self->resource_name,
);
$form //= $self->get_form($c);
return unless $self->validate_form(
c => $c,
form => $form,
resource => \%resource,
run => 0,
);
$hal->resource(\%resource);
return $hal;
}
sub _item_rs {
my ($self, $c) = @_;
my $item_rs;
@ -109,14 +50,8 @@ sub _item_rs {
return $item_rs;
}
sub item_by_id {
my ($self, $c, $id) = @_;
return $self->item_rs($c)->search_rs({'me.id' => $id})->first;
}
sub update_item {
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
sub resource_from_item{
my($self, $c, $item) = @_;
my $billing_subscriber = NGCP::Panel::Utils::API::Subscribers::get_active_subscriber($self, $c, $item->id);
unless($billing_subscriber) {
@ -124,47 +59,37 @@ sub update_item {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Fax subscriber not found.");
return;
}
delete $resource->{id};
my $billing_subscriber_id = $item->id;
my $prov_subs = $item->provisioning_voip_subscriber;
die "need provisioning_voip_subscriber" unless $prov_subs;
my $prov_subscriber_id = $prov_subs->id;
my $destinations_rs = $prov_subs->voip_fax_destinations;
return unless $self->validate_form(
c => $c,
form => $form,
resource => $resource,
run => 1,
);
die "no provisioning_voip_subscriber" unless $prov_subs;
if (! exists $resource->{destinations} ) {
$resource->{destinations} = [];
}
if (ref $resource->{destinations} ne "ARRAY") {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'destinations'. Must be an array.");
return;
my $fax_preference = $prov_subs->voip_fax_preference;
unless ($fax_preference) {
try {
$fax_preference = $prov_subs->create_related('voip_fax_preference', {});
$fax_preference->discard_changes; # reload
} catch($e) {
$c->log->error("Error creating empty fax_preference on get");
};
}
my %update_fields = %{ $resource };
delete $update_fields{destinations};
try {
$prov_subs->delete_related('voip_fax_preference');
$destinations_rs->delete;
$prov_subs->create_related('voip_fax_preference', \%update_fields);
$prov_subs->discard_changes; #reload
for my $dest (@{ $resource->{destinations} }) {
$destinations_rs->create($dest);
}
} catch($e) {
$c->log->error("Error Updating faxserversettings: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "faxserversettings could not be updated.");
return;
};
my %resource = (
$fax_preference ? $fax_preference->get_inflated_columns : (),
subscriber_id => $item->id,
);
delete $resource{id};
my @destinations;
for my $dest ($prov_subs->voip_fax_destinations->all) {
push @destinations, {$dest->get_inflated_columns};
}
$resource{destinations} = \@destinations;
return \%resource;
}
return $item;
sub hal_links {
my($self, $c, $item, $resource, $form) = @_;
return [
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->id)),
];
}
1;

@ -0,0 +1,46 @@
use strict;
use warnings;
use Test::Collection;
use Test::FakeData;
use Test::More;
use Data::Dumper;
#init test_machine
my $test_machine = Test::Collection->new(
name => 'faxserversettings',
);
$test_machine->methods->{collection}->{allowed} = {map {$_ => 1} qw(GET OPTIONS HEAD)};
$test_machine->methods->{item}->{allowed} = {map {$_ => 1} qw(GET OPTIONS HEAD PUT PATCH)};
my $fake_data = Test::FakeData->new;
$fake_data->set_data_from_script({
faxserversettings => {
data => {
'name' => 'api_test_'.time(),
'password' => 'api_test_password',
'active' => '1',#0|1,
't38' => '1',#0|1,
'ecm' => '1',#0|1,
'destinations' => [{
'destination' => 'some@email.com',
'filetype' => 'TIFF',#TIFF,PS,PDF,PDF14
'incoming' => '1',#0|1
'outgoing' => '1',#0|1
'status' => '1',#0|1
}],
},
},
});
#for item creation test purposes /post request data/
$test_machine->DATA_ITEM_STORE($fake_data->process('faxserversettings'));
$test_machine->form_data_item( );
$test_machine->check_bundle();
$test_machine->check_get2put();
$test_machine->clear_test_data_all();
done_testing;
# vim: set tabstop=4 expandtab:
Loading…
Cancel
Save