Expand notifications

This commit is contained in:
James Cole 2022-09-23 06:05:22 +02:00
parent d2c52b47f1
commit 4ca346fc4d
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
6 changed files with 146 additions and 31 deletions

View File

@ -44,8 +44,6 @@ class AdminEventHandler
* @param AdminRequestedTestMessage $event
*
* @return bool
* @throws FireflyException
* @deprecated
*/
public function sendTestMessage(AdminRequestedTestMessage $event): bool
{

View File

@ -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');
}
}

View File

@ -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')));

View File

@ -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 [
//
];
}
}

View File

@ -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 [
//
];
}
}

View File

@ -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;