@ -61,124 +61,56 @@ __PACKAGE__->set_config({
allowed_roles = > [ qw/admin reseller ccareadmin ccare subscriberadmin subscriber/ ] ,
} ) ;
sub GET :Allow {
my ( $ self , $ c ) = @ _ ;
my $ page = $ c - > request - > params - > { page } // 1 ;
my $ rows = $ c - > request - > params - > { rows } // 10 ;
{
my $ timesets = $ self - > item_rs ( $ c ) ;
( my $ total_count , $ timesets , my $ timesets_rows ) = $ self - > paginate_order_collection ( $ c , $ timesets ) ;
my ( @ embedded , @ links ) ;
for my $ tset ( @$ timesets_rows ) {
push @ embedded , $ self - > hal_from_item ( $ c , $ tset , "cftimesets" ) ;
push @ links , Data::HAL::Link - > new (
relation = > 'ngcp:' . $ self - > resource_name ,
href = > sprintf ( '%s%d' , $ self - > dispatch_path , $ tset - > id ) ,
) ;
}
push @ links ,
Data::HAL::Link - > new (
relation = > 'curies' ,
href = > 'http://purl.org/sipwise/ngcp-api/#rel-{rel}' ,
name = > 'ngcp' ,
templated = > true ,
) ,
Data::HAL::Link - > new ( relation = > 'profile' , href = > 'http://purl.org/sipwise/ngcp-api/' ) ,
$ self - > collection_nav_links ( $ c , $ page , $ rows , $ total_count , $ c - > request - > path , $ c - > request - > query_params ) ;
my $ hal = Data::HAL - > new (
embedded = > [ @ embedded ] ,
links = > [ @ links ] ,
) ;
$ hal - > resource ( {
total_count = > $ total_count ,
sub create_item {
my ( $ self , $ c , $ resource , $ form , $ process_extras ) = @ _ ;
my $ schema = $ c - > model ( 'DB' ) ;
my $ tset ;
if ( $ c - > user - > roles eq "subscriberadmin" || $ c - > user - > roles eq "subscriber" ) {
$ resource - > { subscriber_id } = $ c - > user - > voip_subscriber - > id ;
} elsif ( ! defined $ resource - > { subscriber_id } ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Missing mandatory field 'subscriber_id'" ) ;
return ;
}
my $ b_subscriber = $ schema - > resultset ( 'voip_subscribers' ) - > find ( {
id = > $ resource - > { subscriber_id } ,
} ) ;
my $ response = HTTP::Response - > new ( HTTP_OK , undef ,
HTTP::Headers - > new ( $ hal - > http_headers ( skip_links = > 1 ) ) , $ hal - > as_json ) ;
$ c - > response - > headers ( $ response - > headers ) ;
$ c - > response - > body ( $ response - > content ) ;
unless ( $ b_subscriber ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid 'subscriber_id'." ) ;
return ;
}
return ;
}
sub POST :Allow {
my ( $ self , $ c ) = @ _ ;
my $ guard = $ c - > model ( 'DB' ) - > txn_scope_guard ;
{
my $ schema = $ c - > model ( 'DB' ) ;
my $ resource = $ self - > get_valid_post_data (
c = > $ c ,
media_type = > 'application/json' ,
) ;
last unless $ resource ;
my $ form = $ self - > get_form ( $ c ) ;
last unless $ self - > validate_form (
c = > $ c ,
resource = > $ resource ,
form = > $ form ,
) ;
my $ tset ;
if ( $ c - > user - > roles eq "subscriberadmin" || $ c - > user - > roles eq "subscriber" ) {
$ resource - > { subscriber_id } = $ c - > user - > voip_subscriber - > id ;
} elsif ( ! defined $ resource - > { subscriber_id } ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Missing mandatory field 'subscriber_id'" ) ;
last ;
}
my $ b_subscriber = $ schema - > resultset ( 'voip_subscribers' ) - > find ( {
id = > $ resource - > { subscriber_id } ,
my $ subscriber = $ b_subscriber - > provisioning_voip_subscriber ;
unless ( $ subscriber ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid subscriber." ) ;
return ;
}
if ( ! exists $ resource - > { times } ) {
$ resource - > { times } = [] ;
}
if ( ref $ resource - > { times } ne "ARRAY" ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid field 'times'. Must be an array." ) ;
return ;
}
my $ times = $ resource - > { times } ;
# enable tz and use_owner_tz params for POST:
#$times = $self->apply_owner_timezone($c,$b_subscriber,$resource->{times},'deflate');
try {
$ tset = $ schema - > resultset ( 'voip_cf_time_sets' ) - > create ( {
name = > $ resource - > { name } ,
subscriber_id = > $ subscriber - > id ,
} ) ;
unless ( $ b_subscriber ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid 'subscriber_id'." ) ;
last ;
}
my $ subscriber = $ b_subscriber - > provisioning_voip_subscriber ;
unless ( $ subscriber ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid subscriber." ) ;
last ;
}
if ( ! exists $ resource - > { times } ) {
$ resource - > { times } = [] ;
}
if ( ref $ resource - > { times } ne "ARRAY" ) {
$ self - > error ( $ c , HTTP_UNPROCESSABLE_ENTITY , "Invalid field 'times'. Must be an array." ) ;
last ;
}
my $ times = $ resource - > { times } ;
# enable tz and use_owner_tz params for POST:
#$times = $self->apply_owner_timezone($c,$b_subscriber,$resource->{times},'deflate');
try {
$ tset = $ schema - > resultset ( 'voip_cf_time_sets' ) - > create ( {
name = > $ resource - > { name } ,
subscriber_id = > $ subscriber - > id ,
} ) ;
for my $ t ( @$ times ) {
delete $ t - > { time_set_id } ;
$ tset - > create_related ( "voip_cf_periods" , $ t ) ;
}
} catch ( $ e ) {
$ self - > error ( $ c , HTTP_INTERNAL_SERVER_ERROR , "Failed to create cftimeset." , $ e ) ;
last ;
for my $ t ( @$ times ) {
delete $ t - > { time_set_id } ;
$ tset - > create_related ( "voip_cf_periods" , $ t ) ;
}
last unless $ self - > add_create_journal_item_hal ( $ c , sub {
my $ self = shift ;
my ( $ c ) = @ _ ;
my $ _dset = $ self - > item_by_id ( $ c , $ tset - > id ) ;
return $ self - > hal_from_item ( $ c , $ _dset , "cftimesets" ) ; } ) ;
$ guard - > commit ;
$ c - > response - > status ( HTTP_CREATED ) ;
$ c - > response - > header ( Location = > sprintf ( '/%s%d' , $ c - > request - > path , $ tset - > id ) ) ;
$ c - > response - > body ( q( ) ) ;
} catch ( $ e ) {
$ self - > error ( $ c , HTTP_INTERNAL_SERVER_ERROR , "Failed to create cftimeset." , $ e ) ;
return ;
}
return ;
return $ tset ;
}
1 ;