TT#8272 Collapse copy-pasted code a little

Change-Id: If6844d2ae72d728411421faf01f905bd49c21033
changes/56/10356/3
Irina Peshinskaya 9 years ago
parent 3dee9d786b
commit a4e6c7767b

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

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

Loading…
Cancel
Save