Refactor for how we sent admin notifications

This commit is contained in:
David Bomba 2023-02-20 09:21:21 +11:00
parent da9b83d507
commit 0b6c74ce2f
17 changed files with 92 additions and 230 deletions

View File

@ -44,11 +44,6 @@ class CreditCreatedNotification implements ShouldQueue
$credit = $event->credit;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($credit, 'credit'))->build());
$nmo->company = $credit->company;
$nmo->settings = $credit->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
/* The User */
@ -64,9 +59,15 @@ class CreditCreatedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($credit, 'credit'))->build());
$nmo->company = $credit->company;
$nmo->settings = $credit->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
}
/* Override the methods in the Notification Class */

View File

@ -44,11 +44,6 @@ class CreditEmailedNotification implements ShouldQueue
$credit->last_sent_date = now();
$credit->saveQuietly();
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'credit', $event->template))->build());
$nmo->company = $credit->company;
$nmo->settings = $credit->company->settings;
foreach ($event->invitation->company->company_users as $company_user) {
$user = $company_user->user;
@ -60,9 +55,15 @@ class CreditEmailedNotification implements ShouldQueue
// if (($key = array_search('mail', $methods))) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'credit', $event->template))->build());
$nmo->company = $credit->company;
$nmo->settings = $credit->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
}
}
}

View File

@ -44,11 +44,6 @@ class InvoiceCreatedNotification implements ShouldQueue
$invoice = $event->invoice;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($invoice, 'invoice'))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
/* The User */
@ -68,10 +63,16 @@ class InvoiceCreatedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($invoice, 'invoice'))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -46,10 +46,6 @@ class InvoiceEmailedNotification implements ShouldQueue
$invoice->last_sent_date = now();
$invoice->saveQuietly();
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'invoice', $event->template))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->invitation->company->company_users as $company_user) {
@ -66,10 +62,15 @@ class InvoiceEmailedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'invoice', $event->template))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -45,12 +45,6 @@ class InvoiceFailedEmailNotification
$first_notification_sent = true;
$invoice = $event->invitation->invoice;
// $invoice->update(['last_sent_date' => now()]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityFailedSendObject($event->invitation, 'invoice', $event->template, $event->message))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
foreach ($event->invitation->company->company_users as $company_user) {
$user = $company_user->user;
@ -60,10 +54,16 @@ class InvoiceFailedEmailNotification
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityFailedSendObject($event->invitation, 'invoice', $event->template, $event->message))->build());
$nmo->company = $invoice->company;
$nmo->settings = $invoice->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
$first_notification_sent = false;
}
}

View File

@ -58,11 +58,6 @@ class InvitationViewedListener implements ShouldQueue
$entity_name = 'purchase_order';
}
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityViewedObject($invitation, $entity_name))->build());
$nmo->company = $invitation->company;
$nmo->settings = $invitation->company->settings;
foreach ($invitation->company->company_users as $company_user) {
$entity_viewed = "{$entity_name}_viewed";
$entity_viewed_all = "{$entity_name}_viewed_all";
@ -73,8 +68,16 @@ class InvitationViewedListener implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityViewedObject($invitation, $entity_name))->build());
$nmo->company = $invitation->company;
$nmo->settings = $invitation->company->settings;
$nmo->to_user = $company_user->user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
}
}
}

View File

@ -51,11 +51,6 @@ class PaymentNotification implements ShouldQueue
$payment = $event->payment;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityPaidObject($payment))->build());
$nmo->company = $event->company;
$nmo->settings = $event->company->settings;
/*User notifications*/
foreach ($payment->company->company_users as $company_user) {
$user = $company_user->user;
@ -73,9 +68,16 @@ class PaymentNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityPaidObject($payment))->build());
$nmo->company = $event->company;
$nmo->settings = $event->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
}
}

View File

@ -41,10 +41,6 @@ class PurchaseOrderAcceptedListener implements ShouldQueue
$purchase_order = $event->purchase_order;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new PurchaseOrderAcceptedObject($purchase_order, $event->company))->build());
$nmo->company = $event->company;
$nmo->settings = $event->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
@ -62,10 +58,16 @@ class PurchaseOrderAcceptedListener implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new PurchaseOrderAcceptedObject($purchase_order, $event->company))->build());
$nmo->company = $event->company;
$nmo->settings = $event->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -45,10 +45,6 @@ class PurchaseOrderCreatedListener implements ShouldQueue
$purchase_order = $event->purchase_order;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($purchase_order, 'purchase_order'))->build());
$nmo->company = $purchase_order->company;
$nmo->settings = $purchase_order->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
@ -69,10 +65,16 @@ class PurchaseOrderCreatedListener implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($purchase_order, 'purchase_order'))->build());
$nmo->company = $purchase_order->company;
$nmo->settings = $purchase_order->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -46,11 +46,6 @@ class PurchaseOrderEmailedNotification implements ShouldQueue
$purchase_order->last_sent_date = now();
$purchase_order->saveQuietly();
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'purchase_order', 'purchase_order'))->build());
$nmo->company = $purchase_order->company;
$nmo->settings = $purchase_order->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->invitation->company->company_users as $company_user) {
/* The User */
@ -66,10 +61,16 @@ class PurchaseOrderEmailedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'purchase_order', 'purchase_order'))->build());
$nmo->company = $purchase_order->company;
$nmo->settings = $purchase_order->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -43,10 +43,6 @@ class QuoteApprovedNotification implements ShouldQueue
$quote = $event->quote;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new QuoteApprovedObject($quote, $event->company))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
@ -64,10 +60,17 @@ class QuoteApprovedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new QuoteApprovedObject($quote, $event->company))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -44,10 +44,6 @@ class QuoteCreatedNotification implements ShouldQueue
$quote = $event->quote;
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($quote, 'quote'))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
/* We loop through each user and determine whether they need to be notified */
foreach ($event->company->company_users as $company_user) {
@ -68,10 +64,17 @@ class QuoteCreatedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntityCreatedObject($quote, 'quote'))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
/* This prevents more than one notification being sent */
$first_notification_sent = false;
}

View File

@ -45,11 +45,6 @@ class QuoteEmailedNotification implements ShouldQueue
$quote->last_sent_date = now();
$quote->saveQuietly();
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'quote', $event->template))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
foreach ($event->invitation->company->company_users as $company_user) {
$user = $company_user->user;
@ -58,9 +53,15 @@ class QuoteEmailedNotification implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'quote', $event->template))->build());
$nmo->company = $quote->company;
$nmo->settings = $quote->company->settings;
$nmo->to_user = $user;
(new NinjaMailerJob($nmo))->handle();
$nmo = null;
}
}
}

View File

@ -70,7 +70,7 @@ class EntityCreatedObject
);
$mail_obj->markdown = 'email.admin.generic';
$mail_obj->tag = $this->company->company_key;
// $mail_obj->tag = $this->company->company_key;
$mail_obj->data = [
'title' => $mail_obj->subject,
'message' => ctrans(
@ -97,7 +97,7 @@ class EntityCreatedObject
$mail_obj->subject = $this->getSubject();
$mail_obj->data = $this->getData();
$mail_obj->markdown = 'email.admin.generic';
$mail_obj->tag = $this->entity->company->company_key;
// $mail_obj->tag = $this->entity->company->company_key;
}
return $mail_obj;

View File

@ -36,9 +36,6 @@ class EntityNotificationMailer extends Mailable
{
return $this->from(config('mail.from.address'), config('mail.from.name'))
->subject($this->mail_obj->subject)
->markdown($this->mail_obj->markdown, $this->mail_obj->data)
->withSymfonyMessage(function ($message) {
$message->getHeaders()->addTextHeader('Tag', $this->mail_obj->tag);
});
->markdown($this->mail_obj->markdown, $this->mail_obj->data);
}
}

View File

@ -41,9 +41,6 @@ class UserNotificationMailer extends Mailable
'title' => $this->mail_obj->data['title'],
'body' => $this->mail_obj->data['message'],
])
->view($this->mail_obj->markdown, $this->mail_obj->data)
->withSymfonyMessage(function ($message) {
$message->getHeaders()->addTextHeader('Tag', $this->mail_obj->tag);
});
->view($this->mail_obj->markdown, $this->mail_obj->data);
}
}

View File

@ -1,153 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Notifications;
use App\Jobs\Invoice\CreateUbl;
use App\Models\Invoice;
use App\Utils\TempFile;
use App\Utils\Traits\MakesInvoiceHtml;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class BaseNotification extends Notification
{
// use Queueable;
use MakesInvoiceHtml;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function buildMailMessageSettings(MailMessage $mail_message) :MailMessage
{
$mail_message->subject($this->generateEmailEntityHtml($this->entity, $this->subject, $this->contact));
if (strlen($this->settings->reply_to_email) > 1) {
$mail_message->replyTo($this->settings->reply_to_email);
}
if (strlen($this->settings->bcc_email) > 1) {
$mail_message->bcc($this->settings->bcc_email);
}
if ($this->settings->pdf_email_attachment) {
$mail_message->attach(TempFile::path($this->invitation->pdf_file_path()), ['as' => basename($this->invitation->pdf_file_path())]);
}
foreach ($this->entity->documents as $document) {
$mail_message->attach(TempFile::path($document->generateUrl()), ['as' => $document->name]);
}
if ($this->entity instanceof Invoice && $this->settings->ubl_email_attachment) {
$ubl_string = (new Createubl($this->entity))->handle();
$mail_message->attachData($ubl_string, $this->entity->getFileName('xml'));
}
return $mail_message->withSymfonyMessage(function ($message) {
$message->getHeaders()->addTextHeader('Tag', $this->invitation->company->company_key);
});
}
public function buildMailMessageData() :array
{
$body = $this->generateEmailEntityHtml($this->entity, $this->body, $this->contact);
$design_style = $this->settings->email_style;
if ($design_style == 'custom') {
$email_style_custom = $this->settings->email_style_custom;
$body = strtr($email_style_custom, "$body", $body);
}
$data = [
'body' => $body,
'design' => $design_style,
'footer' => '',
'title' => '',
'company' => '',
'view_link' => $this->invitation->getLink(),
'view_text' => ctrans('texts.view_'.$this->entity_string),
'logo' => $this->entity->company->present()->logo(),
'signature' => $this->settings->email_signature,
'settings' => $this->settings,
'whitelabel' => $this->entity->company->account->isPaid() ? true : false,
];
return $data;
}
public function getTemplateView()
{
switch ($this->settings->email_style) {
case 'plain':
return 'email.template.plain';
break;
case 'custom':
return 'email.template.custom';
break;
case 'light':
return 'email.template.light';
break;
case 'dark':
return 'email.template.dark';
break;
default:
return 'email.admin.generic_email';
break;
}
}
}