MT#6827 API pbxdevices POST

wip
ipeshinskaya/InvoiceTemplate5
Gerhard Jungwirth 11 years ago committed by Andreas Granig
parent 816fbcefb6
commit d2a3348a78

@ -147,66 +147,57 @@ sub POST :Allow {
form => $form,
);
my $dset;
unless(defined $resource->{subscriber_id}) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Required: 'subscriber_id'");
last;
}
my $b_subscriber = $schema->resultset('voip_subscribers')->find({
id => $resource->{subscriber_id},
});
unless($b_subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'subscriber_id'.");
my $iden_device = $schema->resultset('autoprov_field_devices')->find({identifier => $resource->{identifier}});
if ($iden_device) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Entry with given 'identifier' already exists.");
last;
}
my $subscriber = $b_subscriber->provisioning_voip_subscriber;
unless($subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid subscriber.");
last;
}
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.");
last;
for my $line ( @{$resource->{lines}} ) {
unless ($line->{subscriber_id} && $line->{subscriber_id} > 0) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid line. Invalid 'subscriber_id'.");
return;
}
my $b_subs = $schema->resultset('voip_subscribers')->find($line->{subscriber_id});
my $p_subs = $b_subs ? $b_subs->provisioning_voip_subscriber : undef;
unless ($p_subs) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'subscriber_id'. Could not find subscriber.");
return;
}
$resource->{subscriber_id} = $p_subs->id;
unless ($line->{linerange_id} && $line->{linerange_id} > 0) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid line. Invalid 'linerange_id'.");
return;
}
my $tmp_linerange = $schema->resultset('autoprov_device_line_ranges')->find($line->{linerange_id});
unless ($tmp_linerange) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'linerange_id'. Could not find autoprov_device_line_range.");
return;
}
}
my $device;
try {
my $primary_nr_rs = $b_subscriber->primary_number;
my $number;
if ($primary_nr_rs) {
$number = $primary_nr_rs->cc . ($primary_nr_rs->ac //'') . $primary_nr_rs->sn;
} else {
$number = ''
}
my $domain = $subscriber->domain->domain // '';
$dset = $schema->resultset('voip_cf_destination_sets')->create({
name => $resource->{name},
subscriber_id => $subscriber->id,
$device = $schema->resultset('autoprov_field_devices')->create({
profile_id => $resource->{profile_id},
contract_id => $resource->{contract_id},
identifier => $resource->{identifier},
station_name => $resource->{station_name},
});
for my $d ( @{$resource->{destinations}} ) {
delete $d->{destination_set_id};
$d->{destination} = NGCP::Panel::Utils::Subscriber::field_to_destination(
destination => $d->{destination},
number => $number,
domain => $domain,
uri => $d->{destination},
);
$dset->create_related("voip_cf_destinations", $d);
for my $line ( @{$resource->{lines}} ) {
$device->create_related("autoprov_field_device_lines", $line); #TODO error check
}
} catch($e) {
$c->log->error("failed to create cfdestinationset: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create cfdestinationset.");
$c->log->error("failed to create pbxdevice: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create pbxdevice.");
last;
}
};
$guard->commit;
$c->response->status(HTTP_CREATED);
$c->response->header(Location => sprintf('/%s%d', $c->request->path, $dset->id));
$c->response->header(Location => sprintf('/%s%d', $c->request->path, $device->id));
$c->response->body(q());
}
return;

@ -0,0 +1,110 @@
package NGCP::Panel::Form::Customer::PbxFieldDeviceAPI;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
has_field 'profile_id' => (
);
has_field 'profile' => (
type => 'Compound',
);
has_field 'profile.id' => (
type => '+NGCP::Panel::Field::PosInteger',
required => 1,
label => 'Device Profile',
);
has_field 'contract' => (
type => 'Compound',
);
has_field 'contract.id' => (
type => 'Text',
);
has_field 'identifier' => (
type => 'Text',
required => 1,
label => 'MAC Address / Identifier',
);
has_field 'station_name' => (
type => 'Text',
required => 1,
label => 'Station Name',
);
has_field 'lines' => (
type => 'Repeatable',
label => 'Lines/Keys',
setup_for_js => 1,
do_wrapper => 1,
do_label => 1,
required => 1,
tags => {
controls_div => 1,
},
wrapper_class => [qw/hfh-rep-block/],
);
has_field 'lines.id' => (
type => 'Hidden',
);
has_field 'lines.subscriber_id' => (
type => '+NGCP::Panel::Field::PosInteger',
required => 1,
label => 'Subscriber',
options_method => \&build_subscribers,
element_attr => {
rel => ['tooltip'],
title => ['The subscriber to use on this line/key'],
},
);
has_field 'lines.line' => (
type => 'Select',
required => 1,
label => 'Line/Key',
element_attr => {
rel => ['tooltip'],
title => ['The line/key to use'],
},
);
sub validate_line_line {
my ($self, $field) = @_;
$field->clear_errors;
unless($field->value =~ /^\d+\.\d+\.\d+$/) {
my $err_msg = 'Invalid line value';
$field->add_error($err_msg);
}
return;
}
has_field 'lines.type' => (
type => 'Select',
required => 1,
label => 'Line/Key Type',
options => [],
no_option_validation => 1,
element_attr => {
rel => ['tooltip'],
title => ['The type of feature to use on this line/key'],
},
element_class => [qw/ngcp-linetype-select/],
);
sub validate_line_type {
my ($self, $field) = @_;
$field->clear_errors;
unless($field->value eq 'private' ||
$field->value eq 'shared' ||
$field->value eq 'blf') {
my $err_msg = 'Invalid line type, must be private, shared or blf';
$field->add_error($err_msg);
}
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -12,10 +12,11 @@ use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
use JSON::Types;
use NGCP::Panel::Form::Customer::PbxFieldDeviceAPI;
sub get_form {
my ($self, $c) = @_;
return NGCP::Panel::Form::Customer::PbxFieldDevice->new;
return NGCP::Panel::Form::Customer::PbxFieldDeviceAPI->new(ctx => $c);
}
sub hal_from_item {
@ -52,15 +53,16 @@ sub hal_from_item {
],
relation => 'ngcp:'.$self->resource_name,
);
# $form //= $self->get_form($c);
# return unless $self->validate_form(
# c => $c,
# form => $form,
# resource => \%resource,
# run => 0,
# );
use DDP; p %resource;
$form //= $self->get_form($c);
return unless $self->validate_form(
c => $c,
form => $form,
resource => \%resource,
run => 0,
);
$resource{lines} = \@lines;
p %resource;
$hal->resource(\%resource);
return $hal;
}

Loading…
Cancel
Save