diff --git a/lib/NGCP/Panel/Controller/API/Contracts.pm b/lib/NGCP/Panel/Controller/API/Contracts.pm index 8c0c62ed93..bbe60889b0 100644 --- a/lib/NGCP/Panel/Controller/API/Contracts.pm +++ b/lib/NGCP/Panel/Controller/API/Contracts.pm @@ -94,12 +94,19 @@ sub GET :Allow { my $now = NGCP::Panel::Utils::DateTime::current_local; my $contracts_rs = $self->item_rs($c,0,$now); (my $total_count, $contracts_rs, my $contracts_rows) = $self->paginate_order_collection($c, $contracts_rs); - my $contracts = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( - c => $c, - rs => $contracts_rs, - contract_id_field => 'id', - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); + + my $contracts; + if ($c->config->{api}{underrun_lock_on_request}) { + $contracts = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( + c => $c, + rs => $contracts_rs, + contract_id_field => 'id', + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); + } else { + $contracts = [$contracts_rs->all]; + } + my (@embedded, @links); my $form = $self->get_form($c); $self->expand_prepare_collection($c); @@ -112,7 +119,11 @@ sub GET :Allow { ); } $self->expand_collection_fields($c, \@embedded); - $self->delay_commit($c,$guard); #potential db write ops in hal_from + + if ($c->config->{api}{underrun_lock_on_request}) { + $self->delay_commit($c,$guard); #potential db write ops in hal_from + } + push @links, Data::HAL::Link->new( relation => 'curies', diff --git a/lib/NGCP/Panel/Controller/API/Customers.pm b/lib/NGCP/Panel/Controller/API/Customers.pm index 2de5d17875..2f5de0a174 100644 --- a/lib/NGCP/Panel/Controller/API/Customers.pm +++ b/lib/NGCP/Panel/Controller/API/Customers.pm @@ -167,12 +167,19 @@ sub GET :Allow { my $now = NGCP::Panel::Utils::DateTime::current_local; my $customers_rs = $self->item_rs($c,$now); (my $total_count, $customers_rs, my $customers_rows) = $self->paginate_order_collection($c, $customers_rs); - my $customers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( - c => $c, - rs => $customers_rs, - contract_id_field => 'id', - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); + + my $customers; + if ($c->config->{api}{underrun_lock_on_request}) { + $customers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( + c => $c, + rs => $customers_rs, + contract_id_field => 'id', + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); + } else { + $customers = [$customers_rs->all]; + } + my (@embedded, @links); my $form = $self->get_form($c); $self->expand_prepare_collection($c); @@ -184,7 +191,12 @@ sub GET :Allow { ); } $self->expand_collection_fields($c, \@embedded); - $self->delay_commit($c,$guard); #potential db write ops in hal_from + + if ($c->config->{api}{underrun_lock_on_request}) { + $self->delay_commit($c,$guard); #potential db write ops in hal_from + + } + push @links, Data::HAL::Link->new( relation => 'curies', diff --git a/lib/NGCP/Panel/Controller/API/SubscriberPreferences.pm b/lib/NGCP/Panel/Controller/API/SubscriberPreferences.pm index c5de86e164..5d77e0c1bf 100644 --- a/lib/NGCP/Panel/Controller/API/SubscriberPreferences.pm +++ b/lib/NGCP/Panel/Controller/API/SubscriberPreferences.pm @@ -95,22 +95,32 @@ sub GET :Allow { { my $subscribers_rs = $self->item_rs($c, "subscribers"); (my $total_count, $subscribers_rs, my $subscribers_rows) = $self->paginate_order_collection($c, $subscribers_rs); - my $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( - c => $c, - rs => $subscribers_rs, - contract_id_field => 'contract_id', - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); + + my $subscribers; + + if ($c->config->{api}{underrun_lock_on_request}) { + $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( + c => $c, + rs => $subscribers_rs, + contract_id_field => 'contract_id', + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); + } else { + $subscribers = [$subscribers_rs->all]; + } + my $now = NGCP::Panel::Utils::DateTime::current_local; my (@embedded, @links, %contract_map); $self->expand_prepare_collection($c); for my $subscriber (@$subscribers) { next unless($subscriber->provisioning_voip_subscriber); - my $contract = $subscriber->contract; - my $balance; $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $contract, - now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level - $contract_map{$contract->id} = 1; + if ($c->config->{api}{underrun_lock_on_request}) { + my $contract = $subscriber->contract; + my $balance; $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $contract, + now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level + $contract_map{$contract->id} = 1; + } push @embedded, $self->hal_from_item($c, $subscriber, "subscribers"); push @links, Data::HAL::Link->new( relation => 'ngcp:'.$self->resource_name, @@ -118,7 +128,11 @@ sub GET :Allow { ); } $self->expand_collection_fields($c, \@embedded); - $self->delay_commit($c,$guard); + + if ($c->config->{api}{underrun_lock_on_request}) { + $self->delay_commit($c,$guard); + } + push @links, Data::HAL::Link->new( relation => 'curies', diff --git a/lib/NGCP/Panel/Controller/API/SubscriberPreferencesItem.pm b/lib/NGCP/Panel/Controller/API/SubscriberPreferencesItem.pm index d47e3c8f64..394a1b4126 100644 --- a/lib/NGCP/Panel/Controller/API/SubscriberPreferencesItem.pm +++ b/lib/NGCP/Panel/Controller/API/SubscriberPreferencesItem.pm @@ -59,12 +59,18 @@ sub GET :Allow { my $subscriber = $self->item_by_id($c, $id, "subscribers"); last unless $self->resource_exists($c, subscriberpreference => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + + } my $hal = $self->hal_from_item($c, $subscriber, "subscribers"); - $guard->commit; #potential db write ops in hal_from + + if ($c->config->{api}{underrun_lock_on_request}) { + $guard->commit; #potential db write ops in hal_from + } my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( (map { # XXX Data::HAL must be able to generate links with multiple relations @@ -109,10 +115,13 @@ sub PATCH :Allow { my $subscriber = $self->item_by_id($c, $id, "subscribers"); last unless $self->resource_exists($c, subscriberpreferences => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + } my $old_resource = $self->get_resource($c, $subscriber, "subscribers"); my $resource = $self->apply_patch($c, $old_resource, $json); @@ -167,10 +176,14 @@ sub PUT :Allow { my $subscriber = $self->item_by_id($c, $id, "subscribers"); last unless $self->resource_exists($c, systemcontact => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + } + my $resource = $self->get_valid_put_data( c => $c, id => $id, diff --git a/lib/NGCP/Panel/Controller/API/Subscribers.pm b/lib/NGCP/Panel/Controller/API/Subscribers.pm index d06071ff80..1f203b1920 100644 --- a/lib/NGCP/Panel/Controller/API/Subscribers.pm +++ b/lib/NGCP/Panel/Controller/API/Subscribers.pm @@ -322,22 +322,31 @@ sub GET :Allow { { my $subscribers_rs = $self->item_rs($c); (my $total_count, $subscribers_rs, my $subscribers_rows) = $self->paginate_order_collection($c, $subscribers_rs); - my $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( - c => $c, - rs => $subscribers_rs, - contract_id_field => 'contract_id', - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); + + my $subscribers; + if ($c->config->{api}{underrun_lock_on_request}) { + $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( + c => $c, + rs => $subscribers_rs, + contract_id_field => 'contract_id', + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); + } else { + $subscribers = [$subscribers_rs->all]; + } + my $now = NGCP::Panel::Utils::DateTime::current_local; my (@embedded, @links, %contract_map); my ($form) = $self->get_form($c); $self->expand_prepare_collection($c); for my $subscriber (@$subscribers) { - my $contract = $subscriber->contract; - NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $contract, - now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level - $contract_map{$contract->id} = 1; + if ($c->config->{api}{underrun_lock_on_request}) { + my $contract = $subscriber->contract; + NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $contract, + now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level + $contract_map{$contract->id} = 1; + } my $resource = $self->resource_from_item($c, $subscriber, $form); push @embedded, $self->hal_from_item($c, $subscriber, $resource, $form); push @links, Data::HAL::Link->new( @@ -346,7 +355,11 @@ sub GET :Allow { ); } $self->expand_collection_fields($c, \@embedded); - $self->delay_commit($c,$guard); + + if ($c->config->{api}{underrun_lock_on_request}) { + $self->delay_commit($c,$guard); + } + push @links, Data::HAL::Link->new( relation => 'curies', diff --git a/lib/NGCP/Panel/Controller/API/SubscribersItem.pm b/lib/NGCP/Panel/Controller/API/SubscribersItem.pm index 84bcef7926..b535e5fb27 100644 --- a/lib/NGCP/Panel/Controller/API/SubscribersItem.pm +++ b/lib/NGCP/Panel/Controller/API/SubscribersItem.pm @@ -48,15 +48,20 @@ sub GET :Allow { my $subscriber = $self->item_by_id($c, $id); last unless $self->resource_exists($c, subscriber => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + } my ($form) = $self->get_form($c); my $resource = $self->resource_from_item($c, $subscriber, $form); my $hal = $self->hal_from_item($c, $subscriber, $resource, $form); - $guard->commit; #potential db write ops in hal_from + + if ($c->config->{api}{underrun_lock_on_request}) { + $guard->commit; #potential db write ops in hal_from + } my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( (map { # XXX Data::HAL must be able to generate links with multiple relations @@ -97,10 +102,14 @@ sub PUT :Allow { my $subscriber = $self->item_by_id($c, $id); last unless $self->resource_exists($c, subscriber => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + } + $c->stash->{subscriber} = $subscriber; # password validation my $resource = $self->get_valid_put_data( c => $c, @@ -122,6 +131,7 @@ sub PUT :Allow { last unless $self->add_update_journal_item_hal($c,$hal); $guard->commit; + $self->return_representation($c, 'hal' => $hal, 'preference' => $preference ); } } catch($e) { @@ -153,10 +163,14 @@ sub PATCH :Allow { my $subscriber = $self->item_by_id($c, $id); last unless $self->resource_exists($c, subscriber => $subscriber); - my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, - contract => $subscriber->contract, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ); #apply underrun lock level + + if ($c->config->{api}{underrun_lock_on_request}) { + my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c, + contract => $subscriber->contract, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ); #apply underrun lock level + } + $c->stash->{subscriber} = $subscriber; # password validation my $json = $self->get_valid_patch_data( c => $c, @@ -186,6 +200,7 @@ sub PATCH :Allow { last unless $self->add_update_journal_item_hal($c,$hal); $guard->commit; + $self->return_representation($c, 'hal' => $hal, 'preference' => $preference ); } } catch($e) { diff --git a/lib/NGCP/Panel/Role/API/Subscribers.pm b/lib/NGCP/Panel/Role/API/Subscribers.pm index 05c81dafc7..0561fbfac3 100644 --- a/lib/NGCP/Panel/Role/API/Subscribers.pm +++ b/lib/NGCP/Panel/Role/API/Subscribers.pm @@ -395,12 +395,16 @@ sub prepare_resource { getcustomer_code => sub { my ($cid) = @_; my $contract = $self->get_customer($c, $cid); - NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( - c => $c, - schema => $c->model('DB'), - contract_id => $contract->id, - skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), - ) if $contract; + + if ($c->config->{api}{underrun_lock_on_request}) { + NGCP::Panel::Utils::Contract::acquire_contract_rowlocks( + c => $c, + schema => $c->model('DB'), + contract_id => $contract->id, + skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1), + ) if $contract; + } + return $contract; }, );