From 14d9374e8ce5deaade2633f0e1d708cf59a25bf3 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Thu, 5 Dec 2013 18:10:21 +0100 Subject: [PATCH] MT#3949 API: move common contract stuff to role. --- .../Panel/Controller/API/ContractsItem.pm | 78 +------------------ lib/NGCP/Panel/Role/API/Contracts.pm | 49 ++++++++++++ 2 files changed, 51 insertions(+), 76 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/ContractsItem.pm b/lib/NGCP/Panel/Controller/API/ContractsItem.pm index 6f52451ff5..d4ceb9e6da 100644 --- a/lib/NGCP/Panel/Controller/API/ContractsItem.pm +++ b/lib/NGCP/Panel/Controller/API/ContractsItem.pm @@ -103,50 +103,12 @@ sub PATCH :Allow { my $contract = $self->contract_by_id($c, $id); last unless $self->resource_exists($c, contract => $contract); - my $billing_mapping = $contract->billing_mappings->find($contract->get_column('bmid')); my $old_resource = { $contract->get_inflated_columns }; - $old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id; my $resource = $self->apply_patch($c, $old_resource, $json); last unless $resource; my $form = NGCP::Panel::Form::Contract::PeeringReseller->new; - last unless $self->validate_form( - c => $c, - form => $form, - resource => $resource - ); - - my $now = NGCP::Panel::Utils::DateTime::current_local; - $resource->{modify_timestamp} = $now; - if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) { - my $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id}); - unless($billing_profile) { - $self->error($c, HTTP_NOT_FOUND, "Invalid 'billing_profile_id'"); - last; - } - $contract->billing_mappings->create({ - start_date => NGCP::Panel::Utils::DateTime::current_local, - billing_profile_id => $resource->{billing_profile_id}, - product_id => $billing_mapping->product_id, - }); - } - delete $resource->{billing_profile_id}; - - $contract->update($resource); - - if($old_resource->{status} ne $resource->{status}) { - if($id == 1) { - $self->error($c, HTTP_FORBIDDEN, "Cannot set contract status to '".$resource->{status}."' for contract id '1'"); - last; - } - NGCP::Panel::Utils::Contract::recursively_lock_contract( - c => $c, - contract => $contract, - ); - } - - # TODO: what about changed product, do we allow it? - + last unless $self->update_contract($c, $contract, $old_resource, $resource, $form); $guard->commit; if ('minimal' eq $preference) { @@ -180,46 +142,10 @@ sub PUT :Allow { id => $id, media_type => 'application/json', ); - my $billing_mapping = $contract->billing_mappings->find($contract->get_column('bmid')); my $old_resource = { $contract->get_inflated_columns }; - $old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id; my $form = NGCP::Panel::Form::Contract::PeeringReseller->new; - last unless $self->validate_form( - c => $c, - form => $form, - resource => $resource - ); - - my $now = NGCP::Panel::Utils::DateTime::current_local; - $resource->{modify_timestamp} = $now; - if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) { - my $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id}); - unless($billing_profile) { - $self->error($c, HTTP_NOT_FOUND, "Invalid 'billing_profile_id'"); - last; - } - $contract->billing_mappings->create({ - start_date => NGCP::Panel::Utils::DateTime::current_local, - billing_profile_id => $resource->{billing_profile_id}, - product_id => $billing_mapping->product_id, - }); - } - delete $resource->{billing_profile_id}; - $contract->update($resource); - - if($old_resource->{status} ne $resource->{status}) { - if($id == 1) { - $self->error($c, HTTP_FORBIDDEN, "Cannot set contract status to '".$resource->{status}."' for contract id '1'"); - last; - } - NGCP::Panel::Utils::Contract::recursively_lock_contract( - c => $c, - contract => $contract, - ); - } - - # TODO: what about changed product, do we allow it? + last unless $self->update_contract($c, $contract, $old_resource, $resource, $form); $guard->commit; diff --git a/lib/NGCP/Panel/Role/API/Contracts.pm b/lib/NGCP/Panel/Role/API/Contracts.pm index c3afcc18e2..0f5dc3d9c3 100644 --- a/lib/NGCP/Panel/Role/API/Contracts.pm +++ b/lib/NGCP/Panel/Role/API/Contracts.pm @@ -94,5 +94,54 @@ sub contract_by_id { return $contracts->find({'me.id' => $id}); } +sub update_contract { + my ($self, $c, $contract, $old_resource, $resource, $form) = @_; + + my $billing_mapping = $contract->billing_mappings->find($contract->get_column('bmid')); + $old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id; + $resource->{billing_profile_id} //= $old_resource->{billing_profile_id}; + + $form //= NGCP::Panel::Form::Contract::PeeringReseller->new; + return unless $self->validate_form( + c => $c, + form => $form, + resource => $resource, + ); + + my $now = NGCP::Panel::Utils::DateTime::current_local; + $resource->{modify_timestamp} = $now; + + if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) { + my $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id}); + unless($billing_profile) { + $self->error($c, HTTP_NOT_FOUND, "Invalid 'billing_profile_id'"); + return; + } + $contract->billing_mappings->create({ + start_date => NGCP::Panel::Utils::DateTime::current_local, + billing_profile_id => $resource->{billing_profile_id}, + product_id => $billing_mapping->product_id, + }); + } + delete $resource->{billing_profile_id}; + + $contract->update($resource); + + if($old_resource->{status} ne $resource->{status}) { + if($contract->id == 1) { + $self->error($c, HTTP_FORBIDDEN, "Cannot set contract status to '".$resource->{status}."' for contract id '1'"); + return; + } + NGCP::Panel::Utils::Contract::recursively_lock_contract( + c => $c, + contract => $contract, + ); + } + + # TODO: what about changed product, do we allow it? + + return $contract; +} + 1; # vim: set tabstop=4 expandtab: