Clean up emails

This commit is contained in:
David Bomba 2021-01-20 14:49:22 +11:00
parent 92e5465679
commit a021bd83d9
9 changed files with 51 additions and 70 deletions

View File

@ -155,10 +155,5 @@ class SendTestEmails extends Command
->setSubject($message['subject']) ->setSubject($message['subject'])
->setBody($message['body']); ->setBody($message['body']);
// Mail::to(config('ninja.testvars.test_email'), 'Mr Test')
// ->cc($cc_emails)
// ->bcc($bcc_emails)
//->replyTo(also_available_if_needed)
//->send(new TemplateEmail($email_builder, $user, $client));
} }
} }

View File

@ -117,52 +117,55 @@ class EmailController extends BaseController
$subject = $request->input('subject'); $subject = $request->input('subject');
$body = $request->input('body'); $body = $request->input('body');
$entity_string = strtolower(class_basename($entity_obj)); $entity_string = strtolower(class_basename($entity_obj));
$template = $request->input('template'); $template = str_replace("email_template_", "", $request->input('template'));
$template = str_replace("email_template_", "", $template);
$entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj, $template) {
if ($invitation->contact->send_email && $invitation->contact->email) {
$data = [ $data = [
'subject' => $subject, 'subject' => $subject,
'body' => $body 'body' => $body
]; ];
$entity_obj->invitations->each(function ($invitation) use ($data, $entity_string, $entity_obj, $template) {
if ($invitation->contact->send_email && $invitation->contact->email) {
$entity_obj->service()->markSent()->save(); $entity_obj->service()->markSent()->save();
EmailEntity::dispatch($invitation, $invitation->company, $template, $data)->delay(now()->addSeconds(5)); EmailEntity::dispatch($invitation, $invitation->company, $template, $data)->delay(now()->addSeconds(5));
} }
}); });
$entity_obj->last_sent_date = now(); $entity_obj->last_sent_date = now();
$entity_obj->save(); $entity_obj->save();
/*Only notify the admin ONCE, not once per contact/invite*/ /*Only notify the admin ONCE, not once per contact/invite*/
if ($entity_obj instanceof Invoice) { if ($entity_obj instanceof Invoice) {
$this->entity_type = Invoice::class; $this->entity_type = Invoice::class;
$this->entity_transformer = InvoiceTransformer::class; $this->entity_transformer = InvoiceTransformer::class;
if ($entity_obj->invitations->count() >= 1) { if ($entity_obj->invitations->count() >= 1)
$entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template); $entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template);
}
} }
if ($entity_obj instanceof Quote) { if ($entity_obj instanceof Quote) {
$this->entity_type = Quote::class; $this->entity_type = Quote::class;
$this->entity_transformer = QuoteTransformer::class; $this->entity_transformer = QuoteTransformer::class;
if ($entity_obj->invitations->count() >= 1) { if ($entity_obj->invitations->count() >= 1)
event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'quote')); event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'quote'));
}
} }
if ($entity_obj instanceof Credit) { if ($entity_obj instanceof Credit) {
$this->entity_type = Credit::class; $this->entity_type = Credit::class;
$this->entity_transformer = CreditTransformer::class; $this->entity_transformer = CreditTransformer::class;
if ($entity_obj->invitations->count() >= 1) { if ($entity_obj->invitations->count() >= 1)
event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'credit')); event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'credit'));
}
} }
if ($entity_obj instanceof RecurringInvoice) { if ($entity_obj instanceof RecurringInvoice) {

View File

@ -40,29 +40,32 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invitation; public $invitation; //The entity invitation
public $company; public $company; //The company
public $settings; public $settings; //The settings object
public $entity_string; public $entity_string; //The entity string ie. invoice, quote, credit
public $reminder_template; public $reminder_template; //The base template we are using
public $entity; public $entity; //The entity object
public $html_engine; public $html_engine; //The HTMLEngine object
public $email_entity_builder; public $email_entity_builder; //The email builder which merges the template and text
public $template_data; public $template_data; //The data to be merged into the template
/** /**
* EmailEntity constructor. * EmailEntity constructor.
*
*
* @param Invitation $invitation * @param Invitation $invitation
* @param Company $company * @param Company $company
* @param ?string $reminder_template * @param ?string $reminder_template
* @param array $template_data
*/ */
public function __construct($invitation, Company $company, ?string $reminder_template = null, $template_data = null) public function __construct($invitation, Company $company, ?string $reminder_template = null, $template_data = null)
{ {
@ -93,12 +96,14 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
if ($this->company->is_disabled) { /* Don't fire emails if the company is disabled */
if ($this->company->is_disabled)
return true; return true;
}
/* Set DB */
MultiDB::setDB($this->company->db); MultiDB::setDB($this->company->db);
/* Set the correct mail driver */
$this->setMailDriver(); $this->setMailDriver();
try { try {
@ -106,7 +111,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
->send( ->send(
new TemplateEmail( new TemplateEmail(
$this->email_entity_builder, $this->email_entity_builder,
$this->invitation->contact->user,
$this->invitation->contact->client $this->invitation->contact->client
) )
); );
@ -132,6 +136,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
} }
} }
/* Switch statement to handling failure notifications */
private function entityEmailFailed($message) private function entityEmailFailed($message)
{ {
switch ($this->entity_string) { switch ($this->entity_string) {
@ -145,30 +150,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
} }
} }
// private function entityEmailSucceeded() /* Builds the email builder object */
// {
// switch ($this->reminder_template) {
// case 'invoice':
// event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars()));
// break;
// case 'reminder1':
// event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT));
// break;
// case 'reminder2':
// event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER2_SENT));
// break;
// case 'reminder3':
// event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER3_SENT));
// break;
// case 'reminder_endless':
// event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER_ENDLESS_SENT));
// break;
// default:
// # code...
// break;
// }
// }
private function resolveEmailBuilder() private function resolveEmailBuilder()
{ {
$class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine"; $class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine";

View File

@ -77,7 +77,6 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
nlog("failed entity sent mailer");
/*If we are migrating data we don't want to fire these notification*/ /*If we are migrating data we don't want to fire these notification*/
if ($this->company->is_disabled) if ($this->company->is_disabled)

View File

@ -72,12 +72,10 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
nlog("entity sent mailer");
/*If we are migrating data we don't want to fire these notification*/ /*If we are migrating data we don't want to fire these notification*/
if ($this->company->is_disabled) { if ($this->company->is_disabled)
return true; return true;
}
//Set DB //Set DB
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);

View File

@ -80,7 +80,7 @@ class EmailPayment extends BaseMailerJob implements ShouldQueue
try { try {
$mail = Mail::to($this->contact->email, $this->contact->present()->name()); $mail = Mail::to($this->contact->email, $this->contact->present()->name());
$mail->send(new TemplateEmail($email_builder, $this->contact->user, $this->contact->client)); $mail->send(new TemplateEmail($email_builder, $this->contact->client));
} catch (\Exception $e) { } catch (\Exception $e) {
nlog("mailing failed with message " . $e->getMessage()); nlog("mailing failed with message " . $e->getMessage());
event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars())); event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars()));

View File

@ -41,22 +41,32 @@ class InvoiceEmailedNotification implements ShouldQueue
$invoice->last_sent_date = now(); $invoice->last_sent_date = now();
$invoice->save(); $invoice->save();
/* We loop through each user and determine whether they need to be notified */
foreach ($event->invitation->company->company_users as $company_user) { foreach ($event->invitation->company->company_users as $company_user) {
/* The User */
$user = $company_user->user; $user = $company_user->user;
/* This is only here to handle the alternate message channels - ie Slack */
$notification = new EntitySentNotification($event->invitation, 'invoice'); $notification = new EntitySentNotification($event->invitation, 'invoice');
/* Returns an array of notification methods */
$methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']); $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']);
/* If one of the methods is email then we fire the EntitySentMailer */
if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) {
unset($methods[$key]); unset($methods[$key]);
EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template); EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template);
/* This prevents more than one notification being sent */
$first_notification_sent = false; $first_notification_sent = false;
} }
/* Override the methods in the Notification Class */
$notification->method = $methods; $notification->method = $methods;
/* Notify on the alternate channels */
$user->notify($notification); $user->notify($notification);
} }
} }

View File

@ -23,18 +23,12 @@ class TemplateEmail extends Mailable
private $build_email; private $build_email;
private $user; //the user the email will be sent from
private $client; private $client;
private $footer; public function __construct($build_email, Client $client)
public function __construct($build_email, User $user, Client $client)
{ {
$this->build_email = $build_email; $this->build_email = $build_email;
$this->user = $user; //this is inappropriate here, need to refactor 'user' in this context the 'user' could also be the 'system'
$this->client = $client; $this->client = $client;
} }

View File

@ -5,9 +5,9 @@
@endcomponent @endcomponent
@endslot @endslot
@slot('greeting') <p>
@lang($message) {!! $message !!}
@endslot </p>
@component('email.components.button', ['url' => $url]) @component('email.components.button', ['url' => $url])
@lang($button) @lang($button)