From 13e96121d7d4e36f7d7c177d620eacb8c123e62a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 16 Feb 2021 21:14:53 +1100 Subject: [PATCH 1/2] Refactoring emails --- app/Jobs/Mail/EntityFailedSendMailer.php | 103 ------------ app/Jobs/Mail/EntityPaidMailer.php | 90 ----------- app/Jobs/Mail/EntitySentMailer.php | 97 ------------ app/Jobs/Mail/EntityViewedMailer.php | 95 ----------- .../Credit/CreditEmailedNotification.php | 2 - .../Invoice/InvoiceEmailedNotification.php | 16 +- .../InvoiceFailedEmailNotification.php | 16 +- .../Misc/InvitationViewedListener.php | 15 +- app/Listeners/Payment/PaymentNotification.php | 21 ++- .../Quote/QuoteEmailedNotification.php | 17 +- .../Admin/EntityViewedNotification.php | 11 -- .../Admin/InvoiceSentNotification.php | 149 ------------------ .../Admin/InvoiceViewedNotification.php | 142 ----------------- .../Admin/NewPartialPaymentNotification.php | 148 ----------------- .../Admin/NewPaymentNotification.php | 41 ----- app/PaymentDrivers/BaseDriver.php | 28 +++- 16 files changed, 92 insertions(+), 899 deletions(-) delete mode 100644 app/Jobs/Mail/EntityFailedSendMailer.php delete mode 100644 app/Jobs/Mail/EntityPaidMailer.php delete mode 100644 app/Jobs/Mail/EntitySentMailer.php delete mode 100644 app/Jobs/Mail/EntityViewedMailer.php delete mode 100644 app/Notifications/Admin/InvoiceSentNotification.php delete mode 100644 app/Notifications/Admin/InvoiceViewedNotification.php delete mode 100644 app/Notifications/Admin/NewPartialPaymentNotification.php diff --git a/app/Jobs/Mail/EntityFailedSendMailer.php b/app/Jobs/Mail/EntityFailedSendMailer.php deleted file mode 100644 index 0aabf91ef683..000000000000 --- a/app/Jobs/Mail/EntityFailedSendMailer.php +++ /dev/null @@ -1,103 +0,0 @@ -company = $company; - - $this->user = $user; - - $this->invitation = $invitation; - - $this->entity = $invitation->{$entity_type}; - - $this->entity_type = $entity_type; - - $this->settings = $invitation->contact->client->getMergedSettings(); - - $this->template = $template; - - $this->message = $message; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) - return true; - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - $mail_obj = (new EntityFailedSendObject($this->invitation, $this->entity_type, $this->template, $this->message))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - try { - Mail::to($this->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - nlog("failing in EntityFailedSendMailer"); - //$this->failed($e); - $this->logMailError($e->getMessage(), $this->entity->client); - } - } -} diff --git a/app/Jobs/Mail/EntityPaidMailer.php b/app/Jobs/Mail/EntityPaidMailer.php deleted file mode 100644 index 7c8094cbda97..000000000000 --- a/app/Jobs/Mail/EntityPaidMailer.php +++ /dev/null @@ -1,90 +0,0 @@ -company = $company; - - $this->user = $user; - - $this->payment = $payment; - - $this->settings = $payment->client->getMergedSettings(); - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { - return true; - } - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - try { - $mail_obj = (new EntityPaidObject($this->payment))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - //send email - Mail::to($this->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - // //$this->failed($e); - $this->logMailError($e->getMessage(), $this->payment->client); - } - } -} diff --git a/app/Jobs/Mail/EntitySentMailer.php b/app/Jobs/Mail/EntitySentMailer.php deleted file mode 100644 index e7660f5e1f84..000000000000 --- a/app/Jobs/Mail/EntitySentMailer.php +++ /dev/null @@ -1,97 +0,0 @@ -company = $company; - - $this->user = $user; - - $this->invitation = $invitation; - - $this->entity = $invitation->{$entity_type}; - - $this->entity_type = $entity_type; - - $this->settings = $invitation->contact->client->getMergedSettings(); - - $this->template = $template; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) - return true; - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - $mail_obj = (new EntitySentObject($this->invitation, $this->entity_type, $this->template))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - try { - Mail::to($this->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - // //$this->failed($e); - $this->logMailError($e->getMessage(), $this->entity->client); - } - } -} diff --git a/app/Jobs/Mail/EntityViewedMailer.php b/app/Jobs/Mail/EntityViewedMailer.php deleted file mode 100644 index fa8fca3efd7b..000000000000 --- a/app/Jobs/Mail/EntityViewedMailer.php +++ /dev/null @@ -1,95 +0,0 @@ -company = $company; - - $this->user = $user; - - $this->invitation = $invitation; - - $this->entity = $invitation->{$entity_type}; - - $this->entity_type = $entity_type; - - $this->settings = $invitation->contact->client->getMergedSettings(); - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { - return true; - } - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - $mail_obj = (new EntityViewedObject($this->invitation, $this->entity_type))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - //send email - try { - Mail::to($this->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - //$this->failed($e); - $this->logMailError($e->getMessage(), $this->entity->client); - } - } -} diff --git a/app/Listeners/Credit/CreditEmailedNotification.php b/app/Listeners/Credit/CreditEmailedNotification.php index 0b085e046ad4..610eda58dd45 100644 --- a/app/Listeners/Credit/CreditEmailedNotification.php +++ b/app/Listeners/Credit/CreditEmailedNotification.php @@ -11,7 +11,6 @@ namespace App\Listeners\Credit; -use App\Jobs\Mail\EntitySentMailer; use App\Jobs\Mail\NinjaMailer; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; @@ -64,7 +63,6 @@ class CreditEmailedNotification implements ShouldQueue NinjaMailerJob::dispatch($nmo); - //EntitySentMailer::dispatch($event->invitation, 'credit', $user, $event->invitation->company, $event->template); $first_notification_sent = false; } diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index bb95fd6a50bf..d14da9e7ed3a 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -11,8 +11,11 @@ namespace App\Listeners\Invoice; -use App\Jobs\Mail\EntitySentMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; +use App\Mail\Admin\EntitySentObject; use App\Notifications\Admin\EntitySentNotification; use App\Utils\Traits\Notifications\UserNotifies; use Illuminate\Contracts\Queue\ShouldQueue; @@ -41,6 +44,12 @@ class InvoiceEmailedNotification implements ShouldQueue $invoice->last_sent_date = now(); $invoice->save(); + $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) { @@ -57,8 +66,11 @@ class InvoiceEmailedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template); + + $nmo->to_user = $user; + NinjaMailerJob::dispatch($nmo); + /* This prevents more than one notification being sent */ $first_notification_sent = false; } diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index 3ce6f7ce73b4..bf5e0aeb833c 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -11,9 +11,11 @@ namespace App\Listeners\Invoice; -use App\Jobs\Mail\EntityFailedSendMailer; -use App\Jobs\Mail\EntitySentMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; +use App\Mail\Admin\EntityFailedSendObject; use App\Notifications\Admin\EntitySentNotification; use App\Utils\Traits\Notifications\UserNotifies; use Illuminate\Contracts\Queue\ShouldQueue; @@ -44,6 +46,11 @@ class InvoiceFailedEmailNotification implements ShouldQueue $invoice->last_sent_date = now(); $invoice->save(); + $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; @@ -54,7 +61,10 @@ class InvoiceFailedEmailNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntityFailedSendMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template, $event->message); + $nmo->to_user = $user; + + NinjaMailerJob::dispatch($nmo); + $first_notification_sent = false; } diff --git a/app/Listeners/Misc/InvitationViewedListener.php b/app/Listeners/Misc/InvitationViewedListener.php index 8d2a188a3664..c8a80c949d0d 100644 --- a/app/Listeners/Misc/InvitationViewedListener.php +++ b/app/Listeners/Misc/InvitationViewedListener.php @@ -11,8 +11,11 @@ namespace App\Listeners\Misc; -use App\Jobs\Mail\EntityViewedMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; +use App\Mail\Admin\EntityViewedObject; use App\Notifications\Admin\EntityViewedNotification; use App\Utils\Traits\Notifications\UserNotifies; use Illuminate\Contracts\Queue\ShouldQueue; @@ -46,6 +49,12 @@ class InvitationViewedListener implements ShouldQueue $notification = new EntityViewedNotification($invitation, $entity_name); + $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"; @@ -54,7 +63,9 @@ class InvitationViewedListener implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - EntityViewedMailer::dispatch($invitation, $entity_name, $company_user->user, $invitation->company); + $nmo->to_user = $company_user->user; + NinjaMailerJob::dispatch($nmo); + } $notification->method = $methods; diff --git a/app/Listeners/Payment/PaymentNotification.php b/app/Listeners/Payment/PaymentNotification.php index 0773c58f0c8a..7b5420c83df7 100644 --- a/app/Listeners/Payment/PaymentNotification.php +++ b/app/Listeners/Payment/PaymentNotification.php @@ -11,8 +11,11 @@ namespace App\Listeners\Payment; -use App\Jobs\Mail\EntityPaidMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; +use App\Mail\Admin\EntityPaidObject; use App\Notifications\Admin\NewPaymentNotification; use App\Utils\Ninja; use App\Utils\Traits\Notifications\UserNotifies; @@ -41,12 +44,16 @@ class PaymentNotification implements ShouldQueue public function handle($event) { MultiDB::setDb($event->company->db); + + if ($event->company->is_disabled) + return true; $payment = $event->payment; - - if ($event->company->is_disabled) { - return true; - } + + $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) { @@ -57,7 +64,9 @@ class PaymentNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - EntityPaidMailer::dispatch($payment, $payment->company, $user); + $nmo->to_user = $user; + + NinjaMailerJob::dispatch($nmo); } $notification = new NewPaymentNotification($payment, $payment->company); diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index 5098707380cd..62b88518d9f5 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -11,8 +11,11 @@ namespace App\Listeners\Quote; -use App\Jobs\Mail\EntitySentMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; +use App\Mail\Admin\EntitySentObject; use App\Notifications\Admin\EntitySentNotification; use App\Utils\Traits\Notifications\UserNotifies; use Illuminate\Contracts\Queue\ShouldQueue; @@ -41,6 +44,12 @@ class QuoteEmailedNotification implements ShouldQueue $quote->last_sent_date = now(); $quote->save(); + $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; @@ -51,7 +60,11 @@ class QuoteEmailedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntitySentMailer::dispatch($event->invitation, 'quote', $user, $event->invitation->company, $event->template); + + $nmo->to_user = $user; + + NinjaMailerJob::dispatch($nmo); + $first_notification_sent = false; } diff --git a/app/Notifications/Admin/EntityViewedNotification.php b/app/Notifications/Admin/EntityViewedNotification.php index 71b08a29d151..5fbdad38de01 100644 --- a/app/Notifications/Admin/EntityViewedNotification.php +++ b/app/Notifications/Admin/EntityViewedNotification.php @@ -76,17 +76,6 @@ class EntityViewedNotification extends Notification implements ShouldQueue */ public function toMail($notifiable) { - //@TODO THESE ARE @DEPRECATED NOW we are now using app/Mail/Admin/* - - $data = $this->buildDataArray(); - $subject = $this->buildSubject(); - - return (new MailMessage) - ->subject($subject) - ->markdown('email.admin.generic', $data) - ->withSwiftMessage(function ($message) { - $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - }); } /** diff --git a/app/Notifications/Admin/InvoiceSentNotification.php b/app/Notifications/Admin/InvoiceSentNotification.php deleted file mode 100644 index ee5e02b27014..000000000000 --- a/app/Notifications/Admin/InvoiceSentNotification.php +++ /dev/null @@ -1,149 +0,0 @@ -invitation = $invitation; - $this->invoice = $invitation->invoice; - $this->contact = $invitation->contact; - $this->company = $company; - $this->settings = $this->invoice->client->getMergedSettings(); - $this->is_system = $is_system; - } - - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - return $this->is_system ? ['slack'] : ['mail']; - } - - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return MailMessage - */ - public function toMail($notifiable) - { - //@TODO THESE ARE @DEPRECATED NOW we are now using app/Mail/Admin/* - - $amount = Number::formatMoney($this->invoice->amount, $this->invoice->client); - $subject = ctrans( - 'texts.notification_invoice_sent_subject', - [ - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - ); - - $data = [ - 'title' => $subject, - 'message' => ctrans( - 'texts.notification_invoice_sent', - [ - 'amount' => $amount, - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - ), - 'url' => config('ninja.app_url').'/invoices/'.$this->invoice->hashed_id, - 'button' => ctrans('texts.view_invoice'), - 'signature' => $this->settings->email_signature, - 'logo' => $this->company->present()->logo(), - 'settings' => $this->settings, - - ]; - - return (new MailMessage) - ->subject($subject) - ->markdown('email.admin.generic', $data) - ->withSwiftMessage(function ($message) { - $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - }); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } - - public function toSlack($notifiable) - { - $logo = $this->company->present()->logo(); - $amount = Number::formatMoney($this->invoice->amount, $this->invoice->client); - - return (new SlackMessage) - ->from(ctrans('texts.notification_bot')) - ->success() - ->image('https://app.invoiceninja.com/favicon-v2.png') - ->content(trans( - 'texts.notification_invoice_sent_subject', - [ - 'amount' => $amount, - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - )) - ->attachment(function ($attachment) use ($amount) { - $attachment->title(ctrans('texts.invoice_number_placeholder', ['invoice' => $this->invoice->number]), $this->invitation->getAdminLink()) - ->fields([ - ctrans('texts.client') => $this->contact->present()->name(), - ctrans('texts.amount') => $amount, - ]); - }); - } -} diff --git a/app/Notifications/Admin/InvoiceViewedNotification.php b/app/Notifications/Admin/InvoiceViewedNotification.php deleted file mode 100644 index 37c77c578f35..000000000000 --- a/app/Notifications/Admin/InvoiceViewedNotification.php +++ /dev/null @@ -1,142 +0,0 @@ -invoice = $invitation->invoice; - $this->contact = $invitation->contact; - $this->company = $company; - $this->settings = $this->invoice->client->getMergedSettings(); - $this->is_system = $is_system; - } - - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - return $this->is_system ? ['slack'] : ['mail']; - } - - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return MailMessage - */ - public function toMail($notifiable) - { - - //@TODO THESE ARE @DEPRECATED NOW we are now using app/Mail/Admin/* - - $amount = Number::formatMoney($this->invoice->amount, $this->invoice->client); - $subject = ctrans( - 'texts.notification_invoice_viewed_subject', - [ - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - ); - - $data = [ - 'title' => $subject, - 'message' => ctrans( - 'texts.notification_invoice_viewed', - [ - 'amount' => $amount, - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - ), - 'url' => config('ninja.app_url').'/invoices/'.$this->invoice->hashed_id, - 'button' => ctrans('texts.view_invoice'), - 'signature' => $this->settings->email_signature, - 'logo' => $this->company->present()->logo(), - 'settings' => $this->settings, - - ]; - - return (new MailMessage) - ->subject($subject) - ->markdown('email.admin.generic', $data) - ->withSwiftMessage(function ($message) { - $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - }); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } - - public function toSlack($notifiable) - { - $logo = $this->company->present()->logo(); - $amount = Number::formatMoney($this->invoice->amount, $this->invoice->client); - - return (new SlackMessage) - ->success() - ->from(ctrans('texts.notification_bot')) - ->image($logo) - ->content(ctrans( - 'texts.notification_invoice_viewed', - [ - 'amount' => $amount, - 'client' => $this->contact->present()->name(), - 'invoice' => $this->invoice->number, - ] - )); - } -} diff --git a/app/Notifications/Admin/NewPartialPaymentNotification.php b/app/Notifications/Admin/NewPartialPaymentNotification.php deleted file mode 100644 index f736f689525f..000000000000 --- a/app/Notifications/Admin/NewPartialPaymentNotification.php +++ /dev/null @@ -1,148 +0,0 @@ -payment = $payment; - $this->company = $company; - $this->settings = $payment->client->getMergedSettings(); - $this->is_system = $is_system; - } - - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - return $this->is_system ? ['slack'] : ['mail']; - } - - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return MailMessage - */ - public function toMail($notifiable) - { - //@TODO THESE ARE @DEPRECATED NOW we are now using app/Mail/Admin/* - - $amount = Number::formatMoney($this->payment->amount, $this->payment->client); - - $invoice_texts = ctrans('texts.invoice_number_short'); - - foreach ($this->payment->invoices as $invoice) { - $invoice_texts .= $invoice->number.','; - } - - $invoice_texts = substr($invoice_texts, 0, -1); - - $data = [ - 'title' => ctrans( - 'texts.notification_partial_payment_paid_subject', - ['client' => $this->payment->client->present()->name()] - ), - 'message' => ctrans( - 'texts.notification_partial_payment_paid', - ['amount' => $amount, - 'client' => $this->payment->client->present()->name(), - 'invoice' => $invoice_texts, - ] - ), - 'url' => config('ninja.app_url').'/payments/'.$this->payment->hashed_id, - 'button' => ctrans('texts.view_payment'), - 'signature' => $this->settings->email_signature, - 'logo' => $this->company->present()->logo(), - 'settings' => $this->settings, - ]; - - return (new MailMessage) - ->subject( - ctrans( - 'texts.notification_partial_payment_paid_subject', - ['client' => $this->payment->client->present()->name()] - ) - )->markdown('email.admin.generic', $data) - ->withSwiftMessage(function ($message) { - $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - }); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } - - public function toSlack($notifiable) - { - $logo = $this->company->present()->logo(); - $amount = Number::formatMoney($this->payment->amount, $this->payment->client); - $invoice_texts = ctrans('texts.invoice_number_short'); - - foreach ($this->payment->invoices as $invoice) { - $invoice_texts .= $invoice->number.','; - } - - $invoice_texts = substr($invoice_texts, 0, -1); - - return (new SlackMessage) - ->success() - //->to("#devv2") - ->from('System') - ->image($logo) - ->content(ctrans( - 'texts.notification_payment_paid', - ['amount' => $amount, - 'client' => $this->payment->client->present()->name(), - 'invoice' => $invoice_texts, ] - )); - } -} diff --git a/app/Notifications/Admin/NewPaymentNotification.php b/app/Notifications/Admin/NewPaymentNotification.php index fcc147f761b6..5becc897ab9e 100644 --- a/app/Notifications/Admin/NewPaymentNotification.php +++ b/app/Notifications/Admin/NewPaymentNotification.php @@ -68,47 +68,6 @@ class NewPaymentNotification extends Notification implements ShouldQueue */ public function toMail($notifiable) { - //@TODO THESE ARE @DEPRECATED NOW we are now using app/Mail/Admin/* - - $amount = Number::formatMoney($this->payment->amount, $this->payment->client); - - $invoice_texts = ctrans('texts.invoice_number_short'); - - foreach ($this->payment->invoices as $invoice) { - $invoice_texts .= $invoice->number.','; - } - - $invoice_texts = substr($invoice_texts, 0, -1); - - $data = [ - 'title' => ctrans( - 'texts.notification_payment_paid_subject', - ['client' => $this->payment->client->present()->name()] - ), - 'message' => ctrans( - 'texts.notification_payment_paid', - ['amount' => $amount, - 'client' => $this->payment->client->present()->name(), - 'invoice' => $invoice_texts, - ] - ), - 'url' => config('ninja.app_url').'/payments/'.$this->payment->hashed_id, - 'button' => ctrans('texts.view_payment'), - 'signature' => $this->settings->email_signature, - 'logo' => $this->company->present()->logo(), - 'settings' => $this->settings, - ]; - - return (new MailMessage) - ->subject( - ctrans( - 'texts.notification_payment_paid_subject', - ['client' => $this->payment->client->present()->name()] - ) - )->markdown('email.admin.generic', $data) - ->withSwiftMessage(function ($message) { - $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - }); } /** diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 3f4169ae6dfc..3cb8e83c7a0e 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -18,8 +18,12 @@ use App\Factory\PaymentFactory; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Jobs\Mail\AutoBillingFailureMailer; use App\Jobs\Mail\ClientPaymentFailureMailer; +use App\Jobs\Mail\NinjaMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; +use App\Mail\Admin\ClientPaymentFailureObject; use App\Models\Client; use App\Models\ClientContact; use App\Models\ClientGatewayToken; @@ -354,12 +358,24 @@ class BaseDriver extends AbstractPaymentDriver $this->payment_hash ); - ClientPaymentFailureMailer::dispatch( - $gateway->client, - $error, - $gateway->client->company, - $this->payment_hash - ); + $nmo = new NinjaMailerObject; + $nmo->mailable = new NinjaMailer( (new ClientPaymentFailureObject($gateway->client, $error, $gateway->client->company, $this->payment_hash))->build() ); + $nmo->company = $gateway->client->company; + $nmo->settings = $gateway->client->company->settings; + + $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get(); + + $invoices->first()->invitations->each(function ($invitation) { + + if ($invitation->contact->send_email && $invitation->contact->email) { + + $nmo->to_user = $invitation->contact; + NinjaMailerJob::dispatch($nmo); + + } + + }); + SystemLogger::dispatch( $gateway->payment_hash, From d4532c4d214a3b7c8910c0474fc4b83d233bcce0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 16 Feb 2021 21:23:32 +1100 Subject: [PATCH 2/2] Refactoring emails --- app/Jobs/Mail/AutoBillingFailureMailer.php | 109 ----------------- app/Jobs/Mail/ClientPaymentFailureMailer.php | 111 ------------------ app/Jobs/Mail/PaymentFailureMailer.php | 1 - .../ClientContactResetPassword.php | 10 -- .../ResetPasswordNotification.php | 2 - app/PaymentDrivers/BaseDriver.php | 2 - tests/MockAccountData.php | 8 ++ 7 files changed, 8 insertions(+), 235 deletions(-) delete mode 100644 app/Jobs/Mail/AutoBillingFailureMailer.php delete mode 100644 app/Jobs/Mail/ClientPaymentFailureMailer.php diff --git a/app/Jobs/Mail/AutoBillingFailureMailer.php b/app/Jobs/Mail/AutoBillingFailureMailer.php deleted file mode 100644 index 3ccd0c420005..000000000000 --- a/app/Jobs/Mail/AutoBillingFailureMailer.php +++ /dev/null @@ -1,109 +0,0 @@ -client = $client; - - $this->error = $error; - - $this->company = $company; - - $this->payment_hash = $payment_hash; - - $this->company = $company; - - $this->settings = $client->getMergedSettings(); - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { - return true; - } - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - //iterate through company_users - $this->company->company_users->each(function ($company_user) { - - //determine if this user has the right permissions - $methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure','all_notifications']); - - //if mail is a method type -fire mail!! - if (($key = array_search('mail', $methods)) !== false) { - unset($methods[$key]); - - $mail_obj = (new AutoBillingFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - //send email - try { - Mail::to($company_user->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - //$this->failed($e); - $this->logMailError($e->getMessage(), $this->client); - } - } - }); - } -} diff --git a/app/Jobs/Mail/ClientPaymentFailureMailer.php b/app/Jobs/Mail/ClientPaymentFailureMailer.php deleted file mode 100644 index 7db3617995cd..000000000000 --- a/app/Jobs/Mail/ClientPaymentFailureMailer.php +++ /dev/null @@ -1,111 +0,0 @@ -company = $company; - - $this->error = $error; - - $this->client = $client; - - $this->payment_hash = $payment_hash; - - $this->company = $company; - - $this->settings = $client->getMergedSettings(); - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { - return true; - } - - //Set DB - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - $this->invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get(); - - $this->invoices->first()->invitations->each(function ($invitation) { - - if ($invitation->contact->send_email && $invitation->contact->email) { - - $mail_obj = (new ClientPaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - - //send email - try { - Mail::to($invitation->contact->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - - $this->logMailError($e->getMessage(), $this->client); - } - - } - - }); - - - } -} diff --git a/app/Jobs/Mail/PaymentFailureMailer.php b/app/Jobs/Mail/PaymentFailureMailer.php index c1a5dbc47977..5bb1ef5dbfde 100644 --- a/app/Jobs/Mail/PaymentFailureMailer.php +++ b/app/Jobs/Mail/PaymentFailureMailer.php @@ -82,7 +82,6 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue //determine if this user has the right permissions $methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure','all_notifications']); - //if mail is a method type -fire mail!! if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); diff --git a/app/Notifications/ClientContactResetPassword.php b/app/Notifications/ClientContactResetPassword.php index ff9cd764efb9..731d7deab6c2 100644 --- a/app/Notifications/ClientContactResetPassword.php +++ b/app/Notifications/ClientContactResetPassword.php @@ -67,16 +67,6 @@ class ClientContactResetPassword extends Notification */ public function toMail($notifiable) { - if (static::$toMailCallback) { - return call_user_func(static::$toMailCallback, $notifiable, $this->token); - } - - return (new MailMessage) - ->subject('Reset Password Notification') - ->line('You are receiving this email because we received a password reset request for your account.') - ->action('Reset Password', url(config('app.url').route('client.password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false))) - ->line('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.users.expire')]) - ->line('If you did not request a password reset, no further action is required.'); } /** diff --git a/app/Notifications/ResetPasswordNotification.php b/app/Notifications/ResetPasswordNotification.php index 387e79441539..34b7f4ad69c5 100644 --- a/app/Notifications/ResetPasswordNotification.php +++ b/app/Notifications/ResetPasswordNotification.php @@ -42,8 +42,6 @@ class ResetPasswordNotification extends Notification */ public function toMail($notifiable) { - return (new MailMessage) - ->view('email.auth.password-reset', ['link' => route('password.reset', $this->token)]); } /** diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 3cb8e83c7a0e..5b84299bfd28 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -16,8 +16,6 @@ use App\Events\Payment\PaymentWasCreated; use App\Exceptions\PaymentFailed; use App\Factory\PaymentFactory; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; -use App\Jobs\Mail\AutoBillingFailureMailer; -use App\Jobs\Mail\ClientPaymentFailureMailer; use App\Jobs\Mail\NinjaMailer; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index d27d691a0582..1d8952dac4ad 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -466,7 +466,10 @@ trait MockAccountData $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount); // UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company); + $user_id = $this->invoice->user_id; + $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now(); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2; @@ -477,6 +480,7 @@ trait MockAccountData $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now()->addMinutes(2); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2; @@ -487,6 +491,7 @@ trait MockAccountData $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now()->addMinutes(10); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2; @@ -497,6 +502,7 @@ trait MockAccountData $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now()->addMinutes(15); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2; @@ -507,6 +513,7 @@ trait MockAccountData $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now()->addMinutes(20); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2; @@ -517,6 +524,7 @@ trait MockAccountData $recurring_invoice->save(); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $user_id; $recurring_invoice->next_send_date = Carbon::now()->addDays(10); $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; $recurring_invoice->remaining_cycles = 2;