diff --git a/lib/NGCP/Panel/Controller/API/NumbersItem.pm b/lib/NGCP/Panel/Controller/API/NumbersItem.pm index 2588fa49d9..1ced3a3da6 100644 --- a/lib/NGCP/Panel/Controller/API/NumbersItem.pm +++ b/lib/NGCP/Panel/Controller/API/NumbersItem.pm @@ -29,6 +29,18 @@ sub get_journal_methods{ return [qw/handle_item_base_journal handle_journals_get handle_journalsitem_get handle_journals_options handle_journalsitem_options handle_journals_head handle_journalsitem_head handle_journalsitem_put handle_journalsitem_patch/]; } +sub pre_process_form_resource { + my($self,$c, $item, $old_resource, $resource, $form, $process_extras) = @_; + + if (!exists $resource->{is_devid} && defined $item->voip_dbalias) { + $resource->{is_devid} = $item->voip_dbalias->is_devid; + } + if (!exists $resource->{devid_alias} && defined $item->voip_dbalias) { + $resource->{devid_alias} = $item->voip_dbalias->devid_alias; + } + return 1; +} + sub update_item_model { my ($self, $c, $item, $old_resource, $resource, $form) = @_; my $schema = $c->model('DB'); @@ -97,7 +109,17 @@ sub update_item_model { (); } else { # otherwise keep number - { e164 => { cc => $_->cc, ac => $_->ac, sn => $_->sn } }; + if (!defined $_->voip_dbalias) { + $c->log->debug("+++ no oldsub dbalias for sn " . $_->sn); + } else { + $c->log->debug("+++ oldsub dbalias for sn " . $_->sn . " is " . ($_->voip_dbalias->devid_alias//"undef")); + } + { e164 => { + cc => $_->cc, ac => $_->ac, sn => $_->sn, + is_devid => (defined $_->voip_dbalias ? $_->voip_dbalias->is_devid : 0), + devid_alias => (defined $_->voip_dbalias ? $_->voip_dbalias->devid_alias : undef), + } + }; } } } @{ $oldalias } ]; @@ -121,11 +143,28 @@ sub update_item_model { (); } else { # otherwise keep number - { e164 => { cc => $_->cc, ac => $_->ac, sn => $_->sn } }; + if (!defined $_->voip_dbalias) { + $c->log->debug("+++ no newsub dbalias for sn " . $_->sn); + } else { + $c->log->debug("+++ newsub dbalias for sn " . $_->sn . " is " . ($_->voip_dbalias->devid_alias//"undef")); + } + { e164 => { + cc => $_->cc, ac => $_->ac, sn => $_->sn, + is_devid => (defined $_->voip_dbalias ? $_->voip_dbalias->is_devid : 0), + devid_alias => (defined $_->voip_dbalias ? $_->voip_dbalias->devid_alias : undef), + } + }; } } } @{ $newalias } ]; - push @{ $newalias }, { e164 => { cc => $item->cc, ac => $item->ac, sn => $item->sn } }; + + use Data::Dumper; $c->log->debug(Dumper $resource); + push @{ $newalias }, { e164 => { + cc => $item->cc, ac => $item->ac, sn => $item->sn, + is_devid => $resource->{is_devid} // 0, + devid_alias => $resource->{devid_alias}, + } + }; NGCP::Panel::Utils::Subscriber::update_subscriber_numbers( c => $c, @@ -157,14 +196,20 @@ sub update_item_model { return; } - # reload item, in case the id changed (which shouldn't happen) $item = $self->_item_rs($c)->find({ - cc => $item->cc, ac => $item->ac, sn => $item->sn + cc => $item->cc, ac => $item->ac, sn => $item->sn, }); return $item; } +sub post_process_hal_resource { + my($self, $c, $item, $resource, $form) = @_; + $resource->{is_devid} = bool $item->voip_dbalias->is_devid; + $resource->{devid_alias} = $item->voip_dbalias->devid_alias; + return $resource; +} + 1; diff --git a/lib/NGCP/Panel/Field/AliasNumber.pm b/lib/NGCP/Panel/Field/AliasNumber.pm index 87c3cfe2e4..98c4b2e03e 100644 --- a/lib/NGCP/Panel/Field/AliasNumber.pm +++ b/lib/NGCP/Panel/Field/AliasNumber.pm @@ -11,7 +11,7 @@ has_field 'id' => ( has_field 'e164' => ( type => '+NGCP::Panel::Field::E164', - order => 99, + order => 97, required => 0, label => 'Alias Number', do_label => 1, @@ -19,6 +19,36 @@ has_field 'e164' => ( wrapper_class => [qw/hfh-rep-field/], ); +has_field 'is_devid' => ( + type => 'Boolean', + required => 0, + label => 'Is Device ID', + do_label => 1, + do_wrapper => 1, + wrapper_class => [qw/hfh-rep-field/], + order => 98, + element_attr => { + rel => ['tooltip'], + title => ['When selected, a call to this alias number is only sent to registered devices indicating either the alias number or the optional alternative device id during registration in the Display-Name.'] + }, +); + +has_field 'devid_alias' => ( + type => 'Text', + required => 0, + maxlength => 127, + label => 'Alternative Device ID', + do_label => 1, + do_wrapper => 1, + wrapper_class => [qw/hfh-rep-field/], + order => 99, + element_attr => { + rel => ['tooltip'], + title => ['An optional device id to be configured on a phone, which is associated with this alias number (e.g. "softphone").'] + }, +); + + has_field 'rm' => ( type => 'RmElement', value => 'Remove', @@ -26,6 +56,12 @@ has_field 'rm' => ( element_class => [qw/btn btn-primary pull-right/], ); +has_block 'fields' => ( + tag => 'div', + class => [qw/modal-body/], + render_list => [qw/e164 is_devid devid_alias/ ], +); + no Moose; 1; diff --git a/lib/NGCP/Panel/Field/E164Alias.pm b/lib/NGCP/Panel/Field/E164Alias.pm new file mode 100644 index 0000000000..909bdb7abc --- /dev/null +++ b/lib/NGCP/Panel/Field/E164Alias.pm @@ -0,0 +1,38 @@ +package NGCP::Panel::Field::E164Alias; +use HTML::FormHandler::Moose; +use NGCP::Panel::Field::E164; +extends 'NGCP::Panel::Field::E164'; + +has_field 'is_devid' => ( + type => 'Boolean', + required => 0, + label => 'Is Device ID', + do_label => 1, + do_wrapper => 1, + wrapper_class => [qw/hfh-rep-field/], + order => 98, + element_attr => { + rel => ['tooltip'], + title => ['When selected, a call to this alias number is only sent to registered devices indicating either the alias number or the optional alternative device id during registration in the Display-Name.'] + }, +); + +has_field 'devid_alias' => ( + type => 'Text', + required => 0, + maxlength => 127, + label => 'Alternative Device ID', + do_label => 1, + do_wrapper => 1, + wrapper_class => [qw/hfh-rep-field/], + order => 99, + element_attr => { + rel => ['tooltip'], + title => ['An optional device id to be configured on a phone, which is associated with this alias number (e.g. "softphone").'] + }, +); + +no Moose; +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/Number/SubadminAPI.pm b/lib/NGCP/Panel/Form/Number/SubadminAPI.pm index ebac652985..6ec8ce84b2 100644 --- a/lib/NGCP/Panel/Form/Number/SubadminAPI.pm +++ b/lib/NGCP/Panel/Form/Number/SubadminAPI.pm @@ -44,5 +44,24 @@ has_field 'is_primary' => ( }, ); +has_field 'is_devid' => ( + type => 'Boolean', + element_attr => { + rel => ['tooltip'], + title => ['When selected, a call to this alias number is only sent to registered devices indicating either the alias number or the optional alternative device id during registration in the Display-Name.'] + }, +); + +has_field 'devid_alias' => ( + type => 'Text', + required => 0, + maxlength => 127, + element_attr => { + rel => ['tooltip'], + title => ['An optional device id to be configured on a phone, which is associated with this alias number (e.g. "softphone").'] + }, +); + + 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/Subscriber/SubscriberAPI.pm b/lib/NGCP/Panel/Form/Subscriber/SubscriberAPI.pm index b4a1956eec..b9c0da82e9 100644 --- a/lib/NGCP/Panel/Form/Subscriber/SubscriberAPI.pm +++ b/lib/NGCP/Panel/Form/Subscriber/SubscriberAPI.pm @@ -38,7 +38,7 @@ has_field 'alias_numbers' => ( ); has_field 'alias_numbers.contains' => ( - type => '+NGCP::Panel::Field::E164', + type => '+NGCP::Panel::Field::E164Alias', ); has_field 'lock' => ( diff --git a/lib/NGCP/Panel/Form/Subscriber/SubscriberSubAdminAPI.pm b/lib/NGCP/Panel/Form/Subscriber/SubscriberSubAdminAPI.pm index 653e30a164..a0de40f259 100644 --- a/lib/NGCP/Panel/Form/Subscriber/SubscriberSubAdminAPI.pm +++ b/lib/NGCP/Panel/Form/Subscriber/SubscriberSubAdminAPI.pm @@ -47,7 +47,7 @@ has_field 'webpassword' => ( ); has_field 'e164' => ( - type => '+NGCP::Panel::Field::E164', + type => '+NGCP::Panel::Field::E164Alias', order => 99, required => 0, label => 'E164 Number', diff --git a/lib/NGCP/Panel/Role/API/Numbers.pm b/lib/NGCP/Panel/Role/API/Numbers.pm index d7a6106bab..25030755ea 100644 --- a/lib/NGCP/Panel/Role/API/Numbers.pm +++ b/lib/NGCP/Panel/Role/API/Numbers.pm @@ -45,7 +45,7 @@ sub _item_rs { },{ '+select' => [\'if(me.id=subscriber.primary_number_id,1,0)'], '+as' => ['is_primary'], - join => 'subscriber' + join => 'subscriber', }); if($c->user->roles eq "admin") { } elsif($c->user->roles eq "reseller") { diff --git a/lib/NGCP/Panel/Role/API/Subscribers.pm b/lib/NGCP/Panel/Role/API/Subscribers.pm index 9cab179d89..eb1cb9d32d 100644 --- a/lib/NGCP/Panel/Role/API/Subscribers.pm +++ b/lib/NGCP/Panel/Role/API/Subscribers.pm @@ -111,6 +111,10 @@ sub resource_from_item { }; next if($resource{primary_number} && compare($resource{primary_number}, $alias)); + if (defined $n->voip_dbalias) { + $alias->{is_devid} = bool $n->voip_dbalias->is_devid; + $alias->{devid_alias} = $n->voip_dbalias->devid_alias; + } push @{ $resource{alias_numbers} }, $alias; } } @@ -688,6 +692,7 @@ sub update_item { schema => $schema, subscriber => $subscriber, ); + try { NGCP::Panel::Utils::Subscriber::update_subscriber_numbers( c => $c, diff --git a/lib/NGCP/Panel/Utils/Subscriber.pm b/lib/NGCP/Panel/Utils/Subscriber.pm index 2cf1c231a7..2b22897db1 100644 --- a/lib/NGCP/Panel/Utils/Subscriber.pm +++ b/lib/NGCP/Panel/Utils/Subscriber.pm @@ -1111,14 +1111,18 @@ sub update_subscriber_numbers { username => $cli, }); if($dbalias) { - if($dbalias->is_primary) { - $dbalias->update({ is_primary => 0 }); - } + $dbalias->update({ + is_primary => 0, + is_devid => $alias->{e164}->{is_devid} // 0, + devid_alias => $alias->{e164}->{devid_alias}, + }); } else { $dbalias = $prov_subs->voip_dbaliases->create({ username => $cli, domain_id => $prov_subs->domain->id, is_primary => 0, + is_devid => $alias->{e164}->{is_devid} // 0, + devid_alias => $alias->{e164}->{devid_alias}, }); } if(defined $acli_pref) { diff --git a/t/api-rest/api-subscribers.t b/t/api-rest/api-subscribers.t index 2eb86a5827..b38772eb77 100644 --- a/t/api-rest/api-subscribers.t +++ b/t/api-rest/api-subscribers.t @@ -301,6 +301,8 @@ if($remote_config->{config}->{features}->{cloudpbx}){ $pilot_local = $test_machine->get_item_hal('subscribers', '/api/subscribers/'.$pilot_local->{content}->{id}, 1); + $terminated_primary_number->{is_devid} = json_true; + $terminated_primary_number->{devid_alias} = 'something'; $test_machine->check_patch2get([ { op => 'replace', path => '/alias_numbers', value => [@{$pilot_local->{content}->{alias_numbers}}, $terminated_primary_number] } ] , $pilot_local->{location},$put2get_check_params); $aliases = $test_machine->get_collection_hal('numbers', '/api/numbers/?type=alias&subscriber_id='.$pilot_local->{content}->{id}, 1)->{collection}; diff --git a/t/lib/Test/FakeData.pm b/t/lib/Test/FakeData.pm index dc48bca937..6e2eccfe72 100644 --- a/t/lib/Test/FakeData.pm +++ b/t/lib/Test/FakeData.pm @@ -22,7 +22,7 @@ use File::Temp qw(tempfile); Moose::Exporter->setup_import_methods( - as_is => [ 'seq' ], + as_is => [ 'seq', 'json_true', 'json_false' ], ); sub BUILD { @@ -924,6 +924,15 @@ sub seq{ return $number + $seq++; } +my $json = '{"true": true, "false": false}'; +my $perl = decode_json $json; +sub json_true { + return $perl->{true}; +} +sub json_false { + return $perl->{false}; +} + 1; __END__