MT#6497 API cfmappings PUT, PATCH

ipeshinskaya/InvoiceTemplate5
Gerhard Jungwirth 12 years ago
parent a11b53aa3e
commit 63ac99218e

@ -100,15 +100,15 @@ sub PATCH :Allow {
);
last unless $json;
my $dset = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, destinationset => $dset);
my $old_resource = $self->hal_from_item($c, $dset, "destinationsets")->resource;
my $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subs_for_cfmapping => $item);
my $old_resource = $self->hal_from_item($c, $item, "cfmappings")->resource;
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
my $form = $self->get_form($c);
$dset = $self->update_item($c, $dset, $old_resource, $resource, $form);
last unless $dset;
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
last unless $item;
$guard->commit;
@ -117,7 +117,7 @@ sub PATCH :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $dset, "destinationsets");
my $hal = $self->hal_from_item($c, $item, "cfmappings");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
@ -136,19 +136,19 @@ sub PUT :Allow {
my $preference = $self->require_preference($c);
last unless $preference;
my $dset = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, destinationset => $dset);
my $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subs_for_cfmapping => $item);
my $resource = $self->get_valid_put_data(
c => $c,
id => $id,
media_type => 'application/json',
);
last unless $resource;
my $old_resource = { $dset->get_inflated_columns };
my $old_resource = undef; # unused: { $dset->get_inflated_columns };
my $form = $self->get_form($c);
$dset = $self->update_item($c, $dset, $old_resource, $resource, $form);
last unless $dset;
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
last unless $item;
$guard->commit;
@ -157,7 +157,7 @@ sub PUT :Allow {
$c->response->header(Preference_Applied => 'return=minimal');
$c->response->body(q());
} else {
my $hal = $self->hal_from_item($c, $dset, "destinationsets");
my $hal = $self->hal_from_item($c, $item, "cfmappings");
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
$hal->http_headers,
), $hal->as_json);
@ -169,27 +169,6 @@ sub PUT :Allow {
return;
}
sub DELETE :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
{
my $rule = $self->item_by_id($c, $id, "rules");
last unless $self->resource_exists($c, rule => $rule);
try {
$rule->delete;
} catch($e) {
$c->log->error("Failed to delete rewriterule with id '$id': $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
last;
}
$guard->commit;
$c->response->status(HTTP_NO_CONTENT);
$c->response->body(q());
}
return;
}
sub end : Private {
my ($self, $c) = @_;

@ -95,6 +95,11 @@ sub item_by_id {
sub update_item {
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
if (ref $resource ne "HASH") {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Must be a hash.");
return;
}
delete $resource->{id};
my $schema = $c->model('DB');
@ -104,55 +109,51 @@ sub update_item {
resource => $resource,
);
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;
}
for my $d (@{ $resource->{destinations} }) {
if (exists $d->{timeout} && ! $d->{timeout}->is_integer) {
$c->log->error("Invalid field 'timeout'.");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'timeout'.");
my $mappings_rs = $item->provisioning_voip_subscriber->voip_cf_mappings;
my $p_subs_id = $item->provisioning_voip_subscriber->id;
my @new_mappings;
my $dsets_rs = $c->model('DB')->resultset('voip_cf_destination_sets');
my $tsets_rs = $c->model('DB')->resultset('voip_cf_time_sets');
for my $type ( qw/cfu cfb cft cfna/) {
if (ref $resource->{$type} ne "ARRAY") {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field '$type'. Must be an array.");
return;
}
}
my $subscriber = $schema->resultset('provisioning_voip_subscribers')->find($resource->{subscriber_id});
unless ($subscriber) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'subscriber_id'.");
return;
for my $mapping (@{ $resource->{$type} }) {
unless ($mapping->{destinationset}) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'destinationset' in '$type'. Must be defined.");
return;
}
my $dset = $dsets_rs->find({subscriber_id => $p_subs_id, name => $mapping->{destinationset}, });
unless ($dset) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'destinationset'. Could not be found.");
return;
}
my $tset;
if ($mapping->{timeset}) {
$tset = $tsets_rs->find({subscriber_id => $p_subs_id, name => $mapping->{timeset}, });
unless ($tset) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'timeset'. Could not be found.");
return;
}
}
push @new_mappings, $mappings_rs->new_result({
destination_set_id => $dset->id,
time_set_id => $tset ? $tset->id : undef,
type => $type,
});
}
}
try {
my $primary_nr_rs = $subscriber->voip_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 // '';
$item->update({
name => $resource->{name},
subscriber_id => $resource->{subscriber_id},
})->discard_changes;
$item->voip_cf_destinations->delete;
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},
);
$item->create_related("voip_cf_destinations", $d);
$mappings_rs->delete;
for my $mapping ( @new_mappings ) {
$mapping->insert;
}
} 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 cfmapping: $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create cfmapping.");
return;
};

Loading…
Cancel
Save