From a4e6c7767bc340f59f4ca2616b414831efba0116 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Thu, 22 Dec 2016 00:33:44 +0200 Subject: [PATCH] TT#8272 Collapse copy-pasted code a little Change-Id: If6844d2ae72d728411421faf01f905bd49c21033 --- lib/NGCP/Panel/Controller/API/Customers.pm | 28 +++------- lib/NGCP/Panel/Role/API/Customers.pm | 65 ++++++++-------------- 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/Customers.pm b/lib/NGCP/Panel/Controller/API/Customers.pm index eef3c20215..07579cfabb 100644 --- a/lib/NGCP/Panel/Controller/API/Customers.pm +++ b/lib/NGCP/Panel/Controller/API/Customers.pm @@ -281,25 +281,15 @@ sub POST :Allow { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "The contact_id is not a valid ngcp:customercontacts item, but an ngcp:systemcontacts item"); last; } - if($customer->invoice_template_id && - $customer->invoice_template->reseller_id != $customer->contact->reseller_id) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - if($customer->subscriber_email_template_id && - $customer->subscriber_email_template->reseller_id != $customer->contact->reseller_id) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'subscriber_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - if($customer->passreset_email_template_id && - $customer->passreset_email_template->reseller_id != $customer->contact->reseller_id) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'passreset_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - if($customer->invoice_email_template_id && - $customer->invoice_email_template->reseller_id != $customer->contact->reseller_id) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; + #todo: strange: why do we check this after customer creation? + my $tmplfields = $self->get_template_fields_spec(); + foreach my $field (keys %$tmplfields){ + my $field_table_rel = $tmplfields->{$field}->[1]; + if($customer->$field() && + $customer->$field_table_rel()->reseller_id != $customer->contact->reseller_id) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid '$field', doesn't exist for the reseller assigned to customer contact"); + return; + } } try { diff --git a/lib/NGCP/Panel/Role/API/Customers.pm b/lib/NGCP/Panel/Role/API/Customers.pm index 55ec20dd8d..ddc42a0b4b 100644 --- a/lib/NGCP/Panel/Role/API/Customers.pm +++ b/lib/NGCP/Panel/Role/API/Customers.pm @@ -204,49 +204,19 @@ sub update_customer { $custcontact = $customer->contact; } - my $oldinvoicetmpl = $old_resource->{invoice_template_id} // 0; - if($resource->{invoice_template_id} && - $oldinvoicetmpl != $resource->{invoice_template_id}) { - my $tmpl = $c->model('DB')->resultset('invoice_templates') - ->search({ reseller_id => $custcontact->reseller_id }) - ->find($resource->{invoice_template_id}); - unless($tmpl) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - } - my $oldsubtmpl = $old_resource->{subscriber_email_template_id} // 0; - if($resource->{subscriber_email_template_id} && - $oldsubtmpl != $resource->{subscriber_email_template_id}) { - my $tmpl = $c->model('DB')->resultset('email_templates') - ->search({ reseller_id => $custcontact->reseller_id }) - ->find($resource->{subscriber_email_template_id}); - unless($tmpl) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'subscriber_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - } - my $oldpasstmpl = $old_resource->{passreset_email_template_id} // 0; - if($resource->{passreset_email_template_id} && - $oldpasstmpl != $resource->{passreset_email_template_id}) { - my $tmpl = $c->model('DB')->resultset('email_templates') - ->search({ reseller_id => $custcontact->reseller_id }) - ->find($resource->{passreset_email_template_id}); - unless($tmpl) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'passreset_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } - } - my $oldinvtmpl = $old_resource->{invoice_email_template_id} // 0; - if($resource->{invoice_email_template_id} && - $oldinvtmpl != $resource->{invoice_email_template_id}) { - my $tmpl = $c->model('DB')->resultset('email_templates') - ->search({ reseller_id => $custcontact->reseller_id }) - ->find($resource->{invoice_email_template_id}); - unless($tmpl) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_email_template_id', doesn't exist for reseller assigned to customer contact"); - return; - } + my $tmplfields = $self->get_template_fields_spec(); + foreach my $field (keys %$tmplfields){ + my $oldtmpl = $old_resource->{$field} // 0; + if($resource->{$field} && + $oldtmpl != $resource->{$field}) { + my $tmpl = $c->model('DB')->resultset($tmplfields->{$field}->[0]) + ->search({ reseller_id => $custcontact->reseller_id }) + ->find($resource->{$field}); + unless($tmpl) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid '$field', doesn't exist for reseller assigned to customer contact"); + return; + } + } } my $old_ext_id = $customer->external_id // ''; @@ -317,5 +287,14 @@ sub update_customer { return $customer; } +sub get_template_fields_spec{ + return { + 'invoice_template_id' => [qw/invoice_templates invoice_template/], + 'subscriber_email_template_id' => [qw/email_templates subscriber_email_template/], + 'passreset_email_template_id' => [qw/email_templates passreset_email_template/], + 'invoice_email_template_id' => [qw/email_templates invoice_email_template/], + }; +} + 1; # vim: set tabstop=4 expandtab: