MT#3949 API: move common contract stuff to role.

agranig/rest
Andreas Granig 13 years ago
parent b64266b969
commit 14d9374e8c

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

@ -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:

Loading…
Cancel
Save