From 5f8d7a325f7ad30eadb6cf7dcfd3a2cf9fc9753a Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Wed, 11 Jan 2023 12:41:13 -0500 Subject: [PATCH 1/3] fix: fix notifications looping when processing the batch Close #390 Close #391 --- app/Console/Commands/TestReminders.php | 5 +++ .../Jobs/ProcessScheduledContactReminders.php | 37 ++++++++++--------- app/Notifications/ReminderTriggered.php | 8 +++- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/Console/Commands/TestReminders.php b/app/Console/Commands/TestReminders.php index cb044578d..e20e517c7 100644 --- a/app/Console/Commands/TestReminders.php +++ b/app/Console/Commands/TestReminders.php @@ -57,6 +57,11 @@ public function handle(): void ->notify(new ReminderTriggered($channel, $contactReminder->label, $contactName)); } + if ($channel->type === UserNotificationChannel::TYPE_TELEGRAM) { + Notification::route('telegram', $channel->content) + ->notify(new ReminderTriggered($channel, $contactReminder->label, '')); + } + try { (new RescheduleContactReminderForChannel([ 'contact_reminder_id' => $scheduledReminder->contact_reminder_id, diff --git a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php index 7c796ceff..8580a0bba 100644 --- a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php +++ b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php @@ -11,6 +11,7 @@ use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\DB; @@ -39,24 +40,24 @@ public function handle() ->get(); foreach ($scheduledContactReminders as $scheduledReminder) { - $channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id); + $userNotificationChannel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id); - if ($channel->type === UserNotificationChannel::TYPE_EMAIL) { - $contactReminder = ContactReminder::find($scheduledReminder->contact_reminder_id); + $contactReminder = ContactReminder::find($scheduledReminder->contact_reminder_id); + + if ($userNotificationChannel->type === UserNotificationChannel::TYPE_EMAIL) { $contact = $contactReminder->contact; - $contactName = NameHelper::formatContactName($channel->user, $contact); + $contactName = NameHelper::formatContactName($userNotificationChannel->user, $contact); - Notification::route('mail', $channel->content) - ->notify(new ReminderTriggered($channel, $contactReminder->label, $contactName)); + Notification::route('mail', $userNotificationChannel->content) + ->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName)); + } - UserNotificationSent::create([ - 'user_notification_channel_id' => $channel->id, - 'sent_at' => Carbon::now(), - 'subject_line' => $contactReminder->label, - ]); + if ($userNotificationChannel->type === UserNotificationChannel::TYPE_TELEGRAM) { + Notification::route('telegram', $userNotificationChannel->content) + ->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, '')); } - $this->updateScheduledContactReminderTriggeredAt($scheduledReminder->id); + $this->updateScheduledContactReminderTriggeredAt($scheduledReminder); $this->updateNumberOfTimesTriggered($scheduledReminder->contact_reminder_id); $this->appendToChain( @@ -69,13 +70,13 @@ public function handle() } } - private function updateScheduledContactReminderTriggeredAt(int $id): void + private function updateScheduledContactReminderTriggeredAt($scheduledReminder): void { - DB::table('contact_reminder_scheduled') - ->where('id', $id) - ->update([ - 'triggered_at' => Carbon::now(), - ]); + (new RescheduleContactReminderForChannel([ + 'contact_reminder_id' => $scheduledReminder->contact_reminder_id, + 'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id, + 'contact_reminder_scheduled_id' => $scheduledReminder->id, + ]))->handle(); } private function updateNumberOfTimesTriggered(int $id): void diff --git a/app/Notifications/ReminderTriggered.php b/app/Notifications/ReminderTriggered.php index b9580e6ef..aa2877e02 100644 --- a/app/Notifications/ReminderTriggered.php +++ b/app/Notifications/ReminderTriggered.php @@ -34,7 +34,13 @@ public function __construct( */ public function via($notifiable) { - return ['mail', 'telegram']; + if ($this->channel->type === UserNotificationChannel::TYPE_EMAIL) { + return ['mail']; + } + + if ($this->channel->type === UserNotificationChannel::TYPE_TELEGRAM) { + return ['telegram']; + } } /** From 6476950fbeba1f9da4ea765914a20bb64b78909a Mon Sep 17 00:00:00 2001 From: djaiss Date: Wed, 11 Jan 2023 17:42:19 +0000 Subject: [PATCH 2/3] chore: php linting with pint --- .../ManageReminders/Jobs/ProcessScheduledContactReminders.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php index 8580a0bba..2c19c2ff8 100644 --- a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php +++ b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php @@ -6,12 +6,10 @@ use App\Helpers\NameHelper; use App\Models\ContactReminder; use App\Models\UserNotificationChannel; -use App\Models\UserNotificationSent; use App\Notifications\ReminderTriggered; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\DB; From 7b791e6a25ed36e303f879d4c3d8c7e3b0459165 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Wed, 11 Jan 2023 12:44:48 -0500 Subject: [PATCH 3/3] fix --- app/Console/Commands/TestReminders.php | 3 +-- app/Notifications/ReminderTriggered.php | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/TestReminders.php b/app/Console/Commands/TestReminders.php index e20e517c7..6fc7019fa 100644 --- a/app/Console/Commands/TestReminders.php +++ b/app/Console/Commands/TestReminders.php @@ -46,10 +46,9 @@ public function handle(): void foreach ($scheduledContactReminders as $scheduledReminder) { $channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id); + $contactReminder = ContactReminder::findOrFail($scheduledReminder->contact_reminder_id); if ($channel->type == UserNotificationChannel::TYPE_EMAIL && $channel->active) { - $contactReminder = ContactReminder::findOrFail($scheduledReminder->contact_reminder_id); - $contact = $contactReminder->contact; $contactName = NameHelper::formatContactName($channel->user, $contact); diff --git a/app/Notifications/ReminderTriggered.php b/app/Notifications/ReminderTriggered.php index aa2877e02..b6e6a4281 100644 --- a/app/Notifications/ReminderTriggered.php +++ b/app/Notifications/ReminderTriggered.php @@ -41,6 +41,8 @@ public function via($notifiable) if ($this->channel->type === UserNotificationChannel::TYPE_TELEGRAM) { return ['telegram']; } + + return []; } /**