|
|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|