MT#3937 API speeddials put/patch

ipeshinskaya/InvoiceTemplate5
Gerhard Jungwirth 12 years ago
parent d71f12aff9
commit f7ebfe07c1

@ -30,6 +30,20 @@ class_has 'query_params' => (
is => 'ro',
isa => 'ArrayRef',
default => sub {[
{
param => 'nonempty',
description => 'Filter for subscribers with nonempty speeddials',
query => {
first => sub {
return unless shift;
{ 'voip_speed_dials.id' => { '!=' => undef } };
},
second => sub {
return unless shift;
{ prefetch => { provisioning_voip_subscriber => 'voip_speed_dials' } };
},
},
},
]},
);

@ -101,13 +101,9 @@ sub PUT :Allow {
media_type => 'application/json',
);
last unless $resource;
my $update = 1;
my $r = $self->prepare_resource($c, $schema, $resource, $update);
last unless $r;
$resource = $r->{resource};
my $form = $self->get_form($c);
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
$subscriber = $self->update_item($c, $subscriber, undef, $resource, $form);
last unless $subscriber;
$guard->commit;
@ -117,8 +113,7 @@ sub PUT :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
$resource = $self->transform_resource($c, $subscriber, $form);
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
my $hal = $self->hal_from_item($c, $subscriber);
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
@ -149,16 +144,11 @@ sub PATCH :Allow {
last unless $json;
my $form = $self->get_form($c);
my $old_resource = $self->transform_resource($c, $subscriber, $form);
my $old_resource = $self->hal_from_item($c, $subscriber)->resource;
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
my $update = 1;
my $r = $self->prepare_resource($c, $schema, $resource, $update);
last unless $r;
$resource = $r->{resource};
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
$subscriber = $self->update_item($c, $subscriber, undef, $resource, $form);
last unless $subscriber;
$guard->commit;
@ -168,8 +158,7 @@ sub PATCH :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
$resource = $self->transform_resource($c, $subscriber, $form);
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
my $hal = $self->hal_from_item($c, $subscriber);
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);

@ -0,0 +1,39 @@
package NGCP::Panel::Form::Subscriber::SpeedDialAPI;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
has_field 'speeddials' => (
type => 'Repeatable',
required => 1,
);
has_field 'speeddials.slot' => (
type => 'Text',
label => 'Slot',
required => 1,
);
has_field 'speeddials.destination' => (
type => 'Text',
label => 'Destination',
required => 1,
);
sub validate_speeddials_slot {
my ($self, $field) = @_;
return unless $self->ctx;
my $slots = $self->ctx->config->{speed_dial_vsc_presets}->{vsc};
return unless $slots;
unless(grep {$_ eq $field->value} @{ $slots }) {
my $err_msg = 'Slot invalid.';
$field->add_error($err_msg);
}
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -13,7 +13,7 @@ use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
use JSON::Types;
use Test::More;
use NGCP::Panel::Form::Subscriber::SubscriberAPI;
use NGCP::Panel::Form::Subscriber::SpeedDialAPI;
use NGCP::Panel::Utils::XMLDispatcher;
use NGCP::Panel::Utils::Prosody;
use NGCP::Panel::Utils::Subscriber;
@ -21,7 +21,7 @@ use NGCP::Panel::Utils::Subscriber;
sub get_form {
my ($self, $c) = @_;
return '';# NGCP::Panel::Form::Subscriber::SubscriberAPI->new;
return NGCP::Panel::Form::Subscriber::SpeedDialAPI->new(ctx => $c);
}
sub hal_from_item {
@ -47,6 +47,14 @@ sub hal_from_item {
relation => 'ngcp:'.$self->resource_name,
);
my $form = $self->get_form($c);
return unless $self->validate_form(
c => $c,
form => $form,
resource => $resource,
run => 0,
);
$hal->resource($resource);
return $hal;
}
@ -77,78 +85,41 @@ sub item_by_id {
}
sub update_item {
my ($self, $c, $item, $full_resource, $resource, $form) = @_;
my $subscriber = $item;
my $customer = $full_resource->{customer};
my $admin = $full_resource->{admin};
my $alias_numbers = $full_resource->{alias_numbers};
my $preferences = $full_resource->{preferences};
if($subscriber->status ne $resource->{status}) {
if($resource->{status} eq 'locked') {
$resource->{lock} = 4;
} elsif($subscriber->status eq 'locked' && $resource->{status} eq 'active') {
$resource->{lock} ||= 0;
} elsif($resource->{status} eq 'terminated') {
try {
NGCP::Panel::Utils::Subscriber::terminate(c => $c, subscriber => $subscriber);
} catch($e) {
$c->log->error("failed to terminate subscriber id ".$subscriber->id);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to terminate subscriber");
}
return;
}
}
if(defined $resource->{lock}) {
try {
NGCP::Panel::Utils::Subscriber::lock_provisoning_voip_subscriber(
c => $c,
prov_subscriber => $subscriber->provisioning_voip_subscriber,
level => $resource->{lock},
);
} catch($e) {
$c->log->error("failed to lock subscriber id ".$subscriber->id." with level ".$resource->{lock});
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to update subscriber lock");
return;
};
}
NGCP::Panel::Utils::Subscriber::update_subscriber_numbers(
schema => $c->model('DB'),
primary_number => $resource->{e164},
alias_numbers => $alias_numbers,
reseller_id => $customer->contact->reseller_id,
subscriber_id => $subscriber->id,
);
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
# $old_resource is unused
my $billing_res = {
external_id => $resource->{external_id},
status => $resource->{status},
};
my $provisioning_res = {
password => $resource->{password},
webusername => $resource->{webusername},
webpassword => $resource->{webpassword},
admin => $resource->{administrative},
is_pbx_group => $resource->{is_pbx_group},
pbx_group_id => $resource->{pbx_group_id},
modify_timestamp => NGCP::Panel::Utils::DateTime::current_local,
my $billing_subs = $item;
my $prov_subs = $billing_subs->provisioning_voip_subscriber;
my $speeddials_rs = $prov_subs->voip_speed_dials;
};
if (ref $resource->{speeddials} ne "ARRAY") {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'speeddials'. Must be an array.");
return;
}
$subscriber->update($billing_res);
$subscriber->provisioning_voip_subscriber->update($provisioning_res);
$subscriber->discard_changes;
NGCP::Panel::Utils::Subscriber::update_preferences(
$form //= $self->get_form($c);
return unless $self->validate_form(
c => $c,
prov_subscriber => $subscriber->provisioning_voip_subscriber,
preferences => $preferences,
form => $form,
resource => $resource,
);
# TODO: status handling (termination, ...)
try {
my $domain = $prov_subs->domain->domain // '';
$speeddials_rs->delete;
for my $spd (@{ $resource->{speeddials} }) {
$speeddials_rs->create({
destination => $self->get_sip_uri($spd->{destination}, $domain),
slot => $spd->{slot},
});
}
} catch($e) {
$c->log->error("failed to update speeddials: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to update speeddials.");
return;
};
return $subscriber;
return $billing_subs;
}
sub speeddials_from_subscriber {
@ -161,5 +132,17 @@ sub speeddials_from_subscriber {
return \@speeddials;
}
sub get_sip_uri {
my ($self, $d, $domain) = @_;
if($d !~ /\@/) {
$d .= '@'.$domain;
}
if($d !~ /^sip:/) {
$d = 'sip:' . $d;
}
return $d;
}
1;
# vim: set tabstop=4 expandtab:

Loading…
Cancel
Save