MT#63050 /api/customers email_template auto assign

* POST /api/customers now assigns reseller's default email_templates
  that are not specified in the DTO. If reseller's default template
  does not exist (reseller that was created via UI v1 without values),
  it automatically created as a copy of the raw default template
  for the reseller, and then assigned to the customer.

Change-Id: I0f74154724e1d053002ec4fcc59a5f99cedd322c
(cherry picked from commit d1686f5243)
(cherry picked from commit 48f67e81c0)
mr13.5.1
Kirill Solomko 2 weeks ago
parent 2677b629af
commit 19d36631df

@ -266,6 +266,50 @@ sub POST :Allow {
$resource->{modify_timestamp} = $now;
my $customer;
my $tmplfields = $self->get_template_fields_spec();
foreach my $field (keys %$tmplfields){
my ($t_table, $t_rel, $t_def_name) = @{$tmplfields->{$field}};
if (my $t_id = $resource->{$field}) {
my $template = $c->model('DB')->resultset($t_table)->search({
'me.id' => $t_id,
'me.reseller_id' => $custcontact->reseller_id,
})->first;
unless ($template) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "'$field' with value '" . $t_id
. "' does not belong to Reseller '" . $custcontact->reseller_id
. "' that is assigned to Customer's Contact '$resource->{contact_id}'");
return;
}
} else {
next unless $t_def_name;
my $template = $c->model('DB')->resultset($t_table)->search({
'me.name' => $t_def_name,
'me.reseller_id' => $custcontact->reseller_id,
})->first;
unless ($template) {
my $raw_def_template = $c->model('DB')->resultset($t_table)->search({
'me.name' => $t_def_name,
'me.reseller_id' => undef
})->first;
unless ($raw_def_template) {
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Cannot find basic default template '$t_def_name'");
return;
}
my $copy_template = { $raw_def_template->get_inflated_columns };
delete $copy_template->{id};
$copy_template->{reseller_id} = $custcontact->reseller_id;
$template = $c->model('DB')->resultset('email_templates')->create($copy_template);
}
$resource->{$field} = $template->id;
}
}
try {
$customer = $schema->resultset('contracts')->create($resource);
$c->log->debug("customer id " . $customer->id . " created");
@ -280,21 +324,6 @@ sub POST :Allow {
last;
}
#todo: strange: why do we check this after customer creation?
my $tmplfields = $self->get_template_fields_spec();
foreach my $field (keys %$tmplfields){
next unless $customer->$field();
my $field_table_rel = $tmplfields->{$field}->[1];
unless($customer->$field_table_rel()->reseller_id &&
$customer->$field_table_rel()->reseller_id == $customer->contact->reseller_id) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "'$field' with value '" . $customer->$field()
. "' does not belong to Reseller '" . $customer->contact->reseller_id
. "' that is assigned to Customer's Contact '$resource->{contact_id}'");
return;
}
}
try {
NGCP::Panel::Utils::BillingMappings::append_billing_mappings(c => $c,
contract => $customer,

@ -285,9 +285,9 @@ sub update_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/],
'subscriber_email_template_id' => [qw/email_templates subscriber_email_template subscriber_default_email/],
'passreset_email_template_id' => [qw/email_templates passreset_email_template passreset_default_email/],
'invoice_email_template_id' => [qw/email_templates invoice_email_template invoice_default_email/],
};
}

Loading…
Cancel
Save