From 4ca346fc4dfaba52abfebae34f0194e20b5e6504 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 23 Sep 2022 06:05:22 +0200 Subject: [PATCH] Expand notifications --- app/Handlers/Events/AdminEventHandler.php | 2 - app/Handlers/Events/BillEventHandler.php | 33 +++++------ app/Jobs/WarnAboutBills.php | 11 ++-- app/Notifications/User/BillReminder.php | 70 ++++++++++++++++++++++- app/Notifications/User/NewAccessToken.php | 57 +++++++++++++++++- app/Providers/EventServiceProvider.php | 4 ++ 6 files changed, 146 insertions(+), 31 deletions(-) diff --git a/app/Handlers/Events/AdminEventHandler.php b/app/Handlers/Events/AdminEventHandler.php index cc2c235909..f158b96f4d 100644 --- a/app/Handlers/Events/AdminEventHandler.php +++ b/app/Handlers/Events/AdminEventHandler.php @@ -44,8 +44,6 @@ class AdminEventHandler * @param AdminRequestedTestMessage $event * * @return bool - * @throws FireflyException - * @deprecated */ public function sendTestMessage(AdminRequestedTestMessage $event): bool { diff --git a/app/Handlers/Events/BillEventHandler.php b/app/Handlers/Events/BillEventHandler.php index 946fce2a9f..8c275137c7 100644 --- a/app/Handlers/Events/BillEventHandler.php +++ b/app/Handlers/Events/BillEventHandler.php @@ -25,8 +25,10 @@ declare(strict_types=1); namespace FireflyIII\Handlers\Events; use FireflyIII\Events\WarnUserAboutBill; -use FireflyIII\Mail\BillWarningMail; -use Log; +use FireflyIII\Notifications\User\BillReminder; +use FireflyIII\Support\Facades\Preferences; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Notification; use Mail; /** @@ -42,24 +44,17 @@ class BillEventHandler */ public function warnAboutBill(WarnUserAboutBill $event): void { - $bill = $event->bill; - $field = $event->field; - $diff = $event->diff; - $user = $bill->user; - $address = $user->email; - $ipAddress = request()?->ip(); + Log::debug(sprintf('Now in %s', __METHOD__)); - // see if user has alternative email address: - $pref = app('preferences')->getForUser($user, 'remote_guard_alt_email'); - if (null !== $pref) { - $address = $pref->data; + $bill = $event->bill; + $preference = Preferences::getForUser($bill->user, 'notification_bill_reminder', true)->data; + + if (true === $preference) { + Log::debug('Bill reminder is true!'); + Notification::send($bill->user, new BillReminder($bill, $event->field, $event->diff)); + } + if (false === $preference) { + Log::debug('User has disabled bill reminders.'); } - - // send message: - Mail::to($address)->send(new BillWarningMail($bill, $field, $diff, $ipAddress)); - - - Log::debug('warnAboutBill'); } - } diff --git a/app/Jobs/WarnAboutBills.php b/app/Jobs/WarnAboutBills.php index 36ce43ab3b..7f6bb7c0b4 100644 --- a/app/Jobs/WarnAboutBills.php +++ b/app/Jobs/WarnAboutBills.php @@ -44,7 +44,6 @@ class WarnAboutBills implements ShouldQueue private Carbon $date; private bool $force; - /** * Create a new job instance. * @@ -54,16 +53,16 @@ class WarnAboutBills implements ShouldQueue */ public function __construct(?Carbon $date) { + $newDate = new Carbon; + $newDate->startOfDay(); + $this->date = $newDate; + if (null !== $date) { $newDate = clone $date; $newDate->startOfDay(); $this->date = $newDate; } - if (null === $date) { - $newDate = new Carbon; - $newDate->startOfDay(); - $this->date = $newDate; - } + $this->force = false; Log::debug(sprintf('Created new WarnAboutBills("%s")', $this->date->format('Y-m-d'))); diff --git a/app/Notifications/User/BillReminder.php b/app/Notifications/User/BillReminder.php index a81bd3641d..0e72655c5f 100644 --- a/app/Notifications/User/BillReminder.php +++ b/app/Notifications/User/BillReminder.php @@ -22,7 +22,73 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; -class BillReminder -{ +use FireflyIII\Models\Bill; +use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Notification; +/** + * Class BillReminder + */ +class BillReminder extends Notification +{ + use Queueable; + + private Bill $bill; + private string $field; + private int $diff; + + /** + * Create a new notification instance. + * + * @return void + */ + public function __construct(Bill $bill, string $field, int $diff) + { + $this->bill = $bill; + $this->field = $field; + $this->diff = $diff; + } + + /** + * 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 \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $subject = (string) trans(sprintf('email.bill_warning_subject_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); + if (0 === $this->diff) { + $subject = (string) trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); + } + + return (new MailMessage) + ->markdown('emails.bill-warning', ['field' => $this->field, 'diff' => $this->diff, 'bill' => $this->bill]) + ->subject($subject); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } } diff --git a/app/Notifications/User/NewAccessToken.php b/app/Notifications/User/NewAccessToken.php index 485f80f701..fe0969443b 100644 --- a/app/Notifications/User/NewAccessToken.php +++ b/app/Notifications/User/NewAccessToken.php @@ -22,7 +22,60 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; -class NewAccessToken -{ +use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Notification; +class NewAccessToken extends Notification +{ + use Queueable; + + private string $address; + + /** + * Create a new notification instance. + * + * @return void + */ + public function __construct(string $address) + { + $this->address = $address; + } + + /** + * 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 \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage) + ->markdown('emails.admin-test', ['email' => $this->address]) + ->subject((string) trans('email.admin_test_subject')); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ea99f2602d..7930b77611 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -42,11 +42,13 @@ use FireflyIII\Mail\OAuthTokenCreatedMail; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBankRepetition; +use FireflyIII\Notifications\Admin\TestNotification; use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use Illuminate\Auth\Events\Login; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; +use Illuminate\Support\Facades\Notification; use Laravel\Passport\Client; use Laravel\Passport\Events\AccessTokenCreated; use Log; @@ -231,6 +233,8 @@ class EventServiceProvider extends ServiceProvider return; } + // HERE WE ARE + Notification::send($event->user, new TestNotification($event->user->email)); $email = $user->email;