Change-Id: I29ebef545d0f92945d13765d963693949f6d8264 (cherry picked from commit e2b5411af98a5374552a2c80cf256e6cf1a22e30)changes/17/2717/8
parent
5b0851f948
commit
1a3e7dd75b
@ -0,0 +1,73 @@
|
||||
package NGCP::Panel::Controller::API::FaxRecordings;
|
||||
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 Path::Tiny qw(path);
|
||||
use Safe::Isa qw($_isa);
|
||||
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::CheckTrailingSlash;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
class_has 'api_description' => (
|
||||
is => 'ro',
|
||||
isa => 'Str',
|
||||
default =>
|
||||
'Defines the actual recording of fax messages. It is referred to by the <a href="#faxs">Faxs</a> relation. A GET on an item returns the binary blob of the recording with Content-Type "audio/x-wav".',
|
||||
);
|
||||
|
||||
with 'NGCP::Panel::Role::API::FaxRecordings';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxrecordings');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxrecordings/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxrecordings');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub OPTIONS :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $allowed_methods = $self->allowed_methods_filtered($c);
|
||||
$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 end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
return;
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,88 @@
|
||||
package NGCP::Panel::Controller::API::FaxRecordingsItem;
|
||||
use Sipwise::Base;
|
||||
use namespace::sweep;
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use MooseX::ClassAttribute qw(class_has);
|
||||
use NGCP::Panel::Utils::DateTime;
|
||||
use NGCP::Panel::Utils::ValidateJSON qw();
|
||||
use Path::Tiny qw(path);
|
||||
use Safe::Isa qw($_isa);
|
||||
use File::Type;
|
||||
use File::Slurp;
|
||||
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
with 'NGCP::Panel::Role::API::FaxRecordings';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxrecordings');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxrecordings/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxrecordings');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
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 $item = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, faxrecording => $item);
|
||||
my $content = '';
|
||||
if( -e $item->filename ){
|
||||
my $ft = File::Type->new();
|
||||
$c->response->header ('Content-Disposition' => 'attachment; filename="' . $item->id . '-' . $item->filename);
|
||||
$content = read_file($item->filename, binmode => ':raw');
|
||||
$c->response->content_type($ft->mime_type($content));
|
||||
$c->response->body($content);
|
||||
}
|
||||
}
|
||||
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_filtered($c);
|
||||
$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 end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
#$self->log_response($c);
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,194 @@
|
||||
package NGCP::Panel::Controller::API::Faxes;
|
||||
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 Path::Tiny qw(path);
|
||||
use Safe::Isa qw($_isa);
|
||||
use NGCP::Panel::Utils::API::Subscribers;
|
||||
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::CheckTrailingSlash;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
class_has 'api_description' => (
|
||||
is => 'ro',
|
||||
isa => 'Str',
|
||||
default =>
|
||||
'Defines the meta information like duration, sender etc for fax recordings. The actual recordings can be fetched via the <a href="#faxrecordings">FaxRecordings</a> relation. NOTE: There is no Location header in the POST method response, as creation is asynchronous.',
|
||||
);
|
||||
|
||||
class_has 'query_params' => (
|
||||
is => 'ro',
|
||||
isa => 'ArrayRef',
|
||||
default => sub {[
|
||||
{
|
||||
param => 'subscriber_id',
|
||||
description => 'Filter for faxes belonging to a specific subscriber',
|
||||
query => {
|
||||
first => sub {
|
||||
my $q = shift;
|
||||
# join is already done in get_item_rs
|
||||
{ 'voip_subscriber.id' => $q };
|
||||
},
|
||||
second => sub { },
|
||||
},
|
||||
},
|
||||
]},
|
||||
);
|
||||
|
||||
with 'NGCP::Panel::Role::API::Faxes';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxes');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxes/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxes');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
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 $items = $self->item_rs($c);
|
||||
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
|
||||
my (@embedded, @links);
|
||||
for my $item ($items->all) {
|
||||
push @embedded, $self->hal_from_item($c, $item);
|
||||
push @links, Data::HAL::Link->new(
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
href => sprintf('/%s%d', $c->request->path, $item->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 $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;
|
||||
}
|
||||
|
||||
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_filtered($c);
|
||||
$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) = @_;
|
||||
{
|
||||
last unless $self->forbid_link_header($c);
|
||||
last unless $self->valid_media_type($c, 'multipart/form-data');
|
||||
last unless $self->require_wellformed_json($c, 'application/json', $c->req->param('json'));
|
||||
my $resource = JSON::from_json($c->req->param('json'), { utf8 => 1 });
|
||||
$resource->{faxfile} = $self->get_upload($c, 'faxfile');
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
last unless $self->validate_form(
|
||||
c => $c,
|
||||
resource => $resource,
|
||||
form => $form,
|
||||
exceptions => [qw/subscriber_id/],
|
||||
);
|
||||
my $billing_subscriber = NGCP::Panel::Utils::API::Subscribers::get_active_subscriber($self, $c, $resource->{subscriber_id});
|
||||
my $prov_subscriber = $billing_subscriber->provisioning_voip_subscriber;
|
||||
last unless $prov_subscriber;
|
||||
my $faxpref = $prov_subscriber->voip_fax_preference;
|
||||
unless ($faxpref && $faxpref->active && $faxpref->password){
|
||||
$c->log->error("invalid subscriber fax preferences");
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid subscriber fax preferences");
|
||||
last;
|
||||
}
|
||||
try {
|
||||
my $output = NGCP::Panel::Utils::Hylafax::send_fax(
|
||||
c => $c,
|
||||
subscriber => $billing_subscriber,
|
||||
destination => $form->values->{destination},
|
||||
(defined $form->values->{faxfile})
|
||||
?
|
||||
( upload => $form->values->{faxfile} ) :
|
||||
( data => $form->values->{data} ),
|
||||
);
|
||||
$c->log->debug("faxserver output:\n");
|
||||
$c->log->debug($output);
|
||||
} catch($e) {
|
||||
$c->log->error("failed to send fax: $e");
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
|
||||
return;
|
||||
};
|
||||
$c->response->status(HTTP_CREATED);
|
||||
$c->response->body(q());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,112 @@
|
||||
package NGCP::Panel::Controller::API::FaxesItem;
|
||||
use Sipwise::Base;
|
||||
use namespace::sweep;
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use MooseX::ClassAttribute qw(class_has);
|
||||
use NGCP::Panel::Utils::DateTime;
|
||||
use NGCP::Panel::Utils::ValidateJSON qw();
|
||||
use NGCP::Panel::Utils::Subscriber;
|
||||
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::Faxes';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxes');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxes/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxes');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
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 $item = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, fax => $item);
|
||||
|
||||
my $hal = $self->hal_from_item($c, $item);
|
||||
|
||||
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_filtered($c);
|
||||
$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 DELETE :Allow {
|
||||
# my ($self, $c, $id) = @_;
|
||||
#
|
||||
# my $guard = $c->model('DB')->txn_scope_guard;
|
||||
# {
|
||||
# my $item = $self->item_by_id($c, $id);
|
||||
# last unless $self->resource_exists($c, fax => $item);
|
||||
#
|
||||
# $item->delete;
|
||||
# NGCP::Panel::Utils::Subscriber::vmnotify( 'c' => $c, 'fax' => $item );
|
||||
# $guard->commit;
|
||||
#
|
||||
# $c->response->status(HTTP_NO_CONTENT);
|
||||
# $c->response->body(q());
|
||||
# }
|
||||
# return;
|
||||
#}
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,23 @@
|
||||
package NGCP::Panel::Form::Subscriber::WebfaxAPI;
|
||||
|
||||
use HTML::FormHandler::Moose;
|
||||
extends 'NGCP::Panel::Form::Subscriber::Webfax';
|
||||
use Moose::Util::TypeConstraints;
|
||||
|
||||
has_field 'subscriber_id' => (
|
||||
type => 'PosInteger',
|
||||
required => 1,
|
||||
element_attr => {
|
||||
rel => ['tooltip'],
|
||||
title => ['The subscriber the fax belongs to.']
|
||||
},
|
||||
);
|
||||
|
||||
has_block 'fields' => (
|
||||
tag => 'div',
|
||||
class => [qw/modal-body/],
|
||||
render_list => [qw/subscriber_id destination data faxfile/],
|
||||
);
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,42 @@
|
||||
package NGCP::Panel::Role::API::FaxRecordings;
|
||||
use Moose::Role;
|
||||
use Sipwise::Base;
|
||||
with 'NGCP::Panel::Role::API' => {
|
||||
-alias =>{ item_rs => '_item_rs', },
|
||||
-excludes => [ 'item_rs' ],
|
||||
};
|
||||
|
||||
sub item_rs {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $item_rs = $c->model('DB')->resultset('fax_journal')->search({
|
||||
filename => { '!=' => '' },
|
||||
'voip_subscriber.id' => { '!=' => undef },
|
||||
},{
|
||||
join => { subscriber => { provisioning_voip_subscriber => 'voip_subscriber' } }
|
||||
});
|
||||
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($c->user->roles eq "reseller") {
|
||||
$item_rs = $item_rs->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id
|
||||
},{
|
||||
join => { subscriber => { provisioning_voip_subscriber => { voip_subscriber => { contract => 'contact' } } } }
|
||||
});
|
||||
}
|
||||
return $item_rs;
|
||||
}
|
||||
|
||||
sub get_form {
|
||||
my ($self, $c) = @_;
|
||||
return;
|
||||
}
|
||||
|
||||
sub item_by_id {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $item_rs = $self->item_rs($c);
|
||||
return $item_rs->find($id);
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,91 @@
|
||||
package NGCP::Panel::Role::API::Faxes;
|
||||
use Moose::Role;
|
||||
use Sipwise::Base;
|
||||
with 'NGCP::Panel::Role::API' => {
|
||||
-alias =>{ item_rs => '_item_rs', },
|
||||
-excludes => [ 'item_rs' ],
|
||||
};
|
||||
|
||||
use boolean qw(true);
|
||||
use TryCatch;
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use NGCP::Panel::Form::Subscriber::WebfaxAPI;
|
||||
use NGCP::Panel::Utils::Subscriber;
|
||||
|
||||
sub item_rs {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $item_rs = $c->model('DB')->resultset('fax_journal')->search({
|
||||
'voip_subscriber.id' => { '!=' => undef },
|
||||
},{
|
||||
join => { subscriber => { provisioning_voip_subscriber => 'voip_subscriber' } }
|
||||
});
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($c->user->roles eq "reseller") {
|
||||
$item_rs = $item_rs->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id
|
||||
},{
|
||||
join => { subscriber => { provisioning_voip_subscriber => { voip_subscriber => { contract => 'contact' } } } }
|
||||
});
|
||||
}
|
||||
return $item_rs;
|
||||
}
|
||||
|
||||
sub get_form {
|
||||
my ($self, $c) = @_;
|
||||
return NGCP::Panel::Form::Subscriber::WebfaxAPI->new;
|
||||
}
|
||||
|
||||
sub hal_from_item {
|
||||
my ($self, $c, $item, $form) = @_;
|
||||
|
||||
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, $item->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->subscriber->provisioning_voip_subscriber->voip_subscriber->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:faxrecordings', href => sprintf("/api/faxrecordings/%d", $item->id)),
|
||||
],
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
);
|
||||
|
||||
my $resource = $self->resource_from_item($c, $item, $form);
|
||||
$hal->resource($resource);
|
||||
return $hal;
|
||||
}
|
||||
|
||||
sub resource_from_item {
|
||||
my ($self, $c, $item, $form) = @_;
|
||||
|
||||
$form //= $self->get_form($c);
|
||||
|
||||
my %resource = ();
|
||||
$resource{id} = int($item->id);
|
||||
$resource{time} = "" . $item->the_timestamp;
|
||||
$resource{subscriber_id} = int($item->subscriber->provisioning_voip_subscriber->voip_subscriber->id);
|
||||
foreach(qw/direction peer_name peer_number reason status quality filename/){
|
||||
$resource{$_} = $item->$_;
|
||||
}
|
||||
foreach(qw/duration pages signal_rate/){
|
||||
$resource{$_} = $item->$_->is_int ? $item->$_ : 0;
|
||||
}
|
||||
return \%resource;
|
||||
}
|
||||
|
||||
sub item_by_id {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $item_rs = $self->item_rs($c);
|
||||
return $item_rs->find($id);
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,56 @@
|
||||
package NGCP::Panel::Utils::API::Subscribers;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use HTTP::Status qw(:constants);
|
||||
|
||||
sub get_active_subscriber{
|
||||
my($api, $c, $id, $params) = @_;
|
||||
|
||||
my $sub_rs = $c->model('DB')->resultset('voip_subscribers')->search({
|
||||
'me.id' => $id,
|
||||
'me.status' => { '!=' => 'terminated' },
|
||||
});
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($c->user->roles eq "reseller") {
|
||||
$sub_rs = $sub_rs->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id,
|
||||
},{
|
||||
join => { contract => 'contact' },
|
||||
});
|
||||
}
|
||||
my $sub = $sub_rs->first;
|
||||
unless($sub && $sub->provisioning_voip_subscriber) {
|
||||
$c->log->error($params->{error_log} ? $params->{error_log} : "invalid subscriber_id '$id'"); # TODO: user, message, trace, ...
|
||||
$api->error($c, HTTP_UNPROCESSABLE_ENTITY, $params->{error} ? $params->{error} : "No subscriber for subscriber_id found");
|
||||
return;
|
||||
}
|
||||
return $sub;
|
||||
}
|
||||
1;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NGCP::Panel::Utils::API::Subscribers
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
A temporary helper to manipulate subscribers related data in REST API moules
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=head2 get_active_subscriber
|
||||
|
||||
Get subscriber NGCP::Schema::Result object of the active subscriber by the mandatory form parameter subscriber_id.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Irina Peshinskaya
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This library is free software. You can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
=cut
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,54 @@
|
||||
#use Sipwise::Base;
|
||||
use strict;
|
||||
|
||||
#use Moose;
|
||||
use Sipwise::Base;
|
||||
use Test::Collection;
|
||||
use Test::FakeData;
|
||||
use Net::Domain qw(hostfqdn);
|
||||
use LWP::UserAgent;
|
||||
use HTTP::Request::Common;
|
||||
use JSON;
|
||||
use Test::More;
|
||||
use Data::Dumper;
|
||||
use File::Basename;
|
||||
|
||||
#init test_machine
|
||||
my $fake_data = Test::FakeData->new;
|
||||
$fake_data->set_data_from_script({
|
||||
'faxes' => {
|
||||
'data' => {
|
||||
json => {
|
||||
subscriber_id => sub { return shift->get_id('subscribers',@_); },
|
||||
destination => "Cisco",
|
||||
},
|
||||
faxfile => [ dirname($0).'/resources/empty.txt' ],
|
||||
},
|
||||
'create_special'=> sub {
|
||||
my ($self,$name) = @_;
|
||||
my $prev_params = $self->test_machine->get_cloned('content_type');
|
||||
@{$self->test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
|
||||
$self->test_machine->check_create_correct(1);
|
||||
$self->test_machine->set(%$prev_params);
|
||||
},
|
||||
'no_delete_available' => 1,
|
||||
},
|
||||
});
|
||||
my $test_machine = Test::Collection->new(
|
||||
name => 'faxes',
|
||||
embedded => [qw/subscribers/]
|
||||
);
|
||||
$test_machine->DATA_ITEM_STORE($fake_data->process('faxes'));
|
||||
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
|
||||
$test_machine->methods->{collection}->{allowed} = {map {$_ => 1} qw(GET HEAD OPTIONS POST)};
|
||||
$test_machine->methods->{item}->{allowed} = {map {$_ => 1} qw(GET HEAD OPTIONS)};
|
||||
|
||||
|
||||
$test_machine->form_data_item( );
|
||||
$test_machine->check_create_correct( 1 );
|
||||
#$test_machine->check_bundle();
|
||||
#$test_machine->check_get2put( sub { $_[0] = { json => JSON::to_json($_[0]), 'faxfile' => $test_machine->DATA_ITEM_STORE->{faxfile} }; } );
|
||||
|
||||
done_testing;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
Loading…
Reference in new issue