MT#6497 API: edit callforwards

we use the billing.voip_subscribers id now (as everywhere else)
agranig/subprof
Gerhard Jungwirth 12 years ago
parent fc1c89a7c1
commit b343f16e5e

@ -70,12 +70,14 @@ sub GET :Allow {
rows => $rows,
});
my (@embedded, @links);
for my $cf ($cfs->search({}, {order_by => ['subscriber_id', 'type']})->all) {
push @embedded, $self->hal_from_item($c, $cf, "callforwards");
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('%s%s', $self->dispatch_path, $cf->subscriber_id),
);
for my $cf ($cfs->all) {
try {
push @embedded, $self->hal_from_item($c, $cf, "callforwards");
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(

@ -85,6 +85,112 @@ sub OPTIONS :Allow {
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 $callforward = $self->item_by_id($c, $id, "callforwards");
last unless $self->resource_exists($c, callforward => $callforward);
my $old_resource = $self->hal_from_item($c, $callforward, "callforwards")->resource;
my $resource = $self->apply_patch($c, $old_resource, $json);
last unless $resource;
my $form = $self->get_form($c);
$callforward = $self->update_item($c, $callforward, $old_resource, $resource, $form);
last unless $callforward;
$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, $callforward, "callforwards");
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;
}
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 $callforward = $self->item_by_id($c, $id, "callforwards");
last unless $self->resource_exists($c, callforward => $callforward);
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);
$callforward = $self->update_item($c, $callforward, $old_resource, $resource, $form);
last unless $callforward;
$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, $callforward, "callforwards");
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;
}
sub DELETE :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
{
my $ruleset = $self->item_by_id($c, $id, "callforwards");
last unless $self->resource_exists($c, ruleset => $ruleset);
try {
$ruleset->voip_rewrite_rules->delete;
$ruleset->delete;
} catch($e) {
$c->log->error("Failed to delete rewriteruleset 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) = @_;

@ -26,7 +26,11 @@ sub hal_from_item {
my $form;
my $rwr_form = $self->get_form($c, "rules");
my %resource = (subscriber_id => $item->subscriber_id);
my $prov_subs = $item->provisioning_voip_subscriber;
die "no provisioning_voip_subscriber" unless $prov_subs;
my %resource = (subscriber_id => $prov_subs->id);
my $hal = Data::HAL->new(
links => [
@ -38,16 +42,16 @@ sub hal_from_item {
),
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->subscriber_id)),
Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%s", $type, $item->subscriber_id)),
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->subscriber->voip_subscriber->id)),
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)),
],
relation => 'ngcp:'.$self->resource_name,
);
for my $cf_type (qw/cfu cfb cft cfna/) {
my $mapping = $c->model('DB')->resultset('voip_cf_mappings')->search({
subscriber_id => $item->subscriber_id,
subscriber_id => $prov_subs->id,
type => $cf_type,
})->first;
if ($mapping) {
@ -73,18 +77,19 @@ sub item_rs {
my ($self, $c, $type) = @_;
my $item_rs;
if($type eq "callforwards") {
if($c->user->roles eq "admin") {
$item_rs = $c->model('DB')->resultset('voip_cf_mappings')
->search_rs(undef,{
columns => ['subscriber_id'],
group_by => ['subscriber_id']});
} else {
return;
}
} else {
die "You should not reach this";
$item_rs = $c->model('DB')->resultset('voip_subscribers')
->search(
{ status => { '!=' => 'terminated' } },
{ prefetch => 'provisioning_voip_subscriber',}
);
if($c->user->roles eq "reseller") {
$item_rs = $item_rs->search({
'contact.reseller_id' => $c->user->reseller_id,
}, {
join => { 'contract' => 'contact' },
});
}
return $item_rs;
}
@ -92,22 +97,96 @@ sub item_by_id {
my ($self, $c, $id, $type) = @_;
my $item_rs = $self->item_rs($c, $type);
return $item_rs->search_rs({subscriber_id => $id})->first;
return $self->item_rs($c, $type)->search_rs({'me.id' => $id})->first;
}
sub update_item {
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
delete $resource->{id};
if ($resource->{rewriterules}) {
$item->voip_rewrite_rules->delete;
my $i = 30;
for my $rule (@{ $resource->{rewriterules} }) {
$item->voip_rewrite_rules->create({
%{ $rule },
priority => $i++,
my $billing_subscriber_id = $item->id; # note that this belongs to provisioning_voip_subscribers
my $prov_subscriber_id = $item->provisioning_voip_subscriber->id;
for my $type (qw/cfu cfb cft cfna/) {
my $mapping = $c->model('DB')->resultset('voip_cf_mappings')->search_rs({
subscriber_id => $prov_subscriber_id,
type => $type,
});
my $mapping_count = $mapping->count;
my ($dset, $tset);
if ($mapping_count == 0) {
next unless (defined $resource->{$type});
$mapping = $mapping = $c->model('DB')->resultset('voip_cf_mappings')->create({
subscriber_id => $prov_subscriber_id,
type => $type,
});
$mapping->discard_changes; # get our row
} elsif ($mapping_count > 1) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Not a simple cf. Multiple $type-s configured.");
return;
} else { # count is 1
$mapping = $mapping->first;
$dset = $mapping->destination_set;
$tset = $mapping->time_set;
}
try {
my $prov_subs = $item->provisioning_voip_subscriber;
my $primary_nr_rs = $item->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 = $prov_subs->domain->domain // '';
if ($dset) {
if ((defined $resource->{$type}{destinations}) && @{ $resource->{$type}{destinations}}) {
$dset->voip_cf_destinations->delete; #empty dset
} else {
$dset->delete; # delete dset
}
} else {
if ((defined $resource->{$type}{destinations}) && @{ $resource->{$type}{destinations}}) {
$dset = $mapping->create_related('destination_set', {'name' => "quickset_$type", subscriber_id => $prov_subscriber_id,} );
$mapping->update({destination_set_id => $dset->id});
}
}
if ($tset) {
if ((defined $resource->{$type}{times}) && @{ $resource->{$type}{times}}) {
$tset->voip_cf_periods->delete; #empty tset
} else {
$tset->delete; # delete tset
}
} else {
if ((defined $resource->{$type}{times}) && @{ $resource->{$type}{times}}) {
$tset = $mapping->create_related('time_set', {'name' => "quickset_$type", subscriber_id => $prov_subscriber_id,} );
$mapping->update({time_set_id => $tset->id});
}
}
for my $d (@{ $resource->{$type}{destinations} }) {
$c->log->debug("fooobar $d");
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->voip_cf_destinations->update_or_create({
%$d
});
}
for my $t (@{ $resource->{$type}{times} }) {
delete $t->{time_set_id};
$tset->voip_cf_periods->update_or_create({
%$t
});
}
} catch($e) {
$c->log->error("Error Updating '$type': $e");
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "CallForward '$type' could not be updated.");
return;
}
}
@ -115,9 +194,8 @@ sub update_item {
c => $c,
form => $form,
resource => $resource,
run => 0,
);
#TODO: priority not accessible here
$item->update($resource);
return $item;
}

@ -695,6 +695,17 @@ sub destination_to_field {
return ($d, $duri);
}
sub callforward_create_or_update_quickset_destinations {
my %params = @_;
my $mapping = $params{mapping};
my $type = $params{type};
my $destinations = $params{destinations};
my $schema = $params{schema};
return;
}
1;
=head1 NAME

Loading…
Cancel
Save