From dd6d45fe78f4f0553e84246ab7e10be4010aa4fe Mon Sep 17 00:00:00 2001
From: Mazarin <djaiss@users.noreply.github.com>
Date: Wed, 11 Jan 2023 12:50:53 -0500
Subject: [PATCH] fix: fix notifications looping when processing the batch
 (monicahq/chandler#392)

Close #390
Close #391
---
 app/Console/Commands/TestReminders.php        |  8 +++-
 .../Jobs/ProcessScheduledContactReminders.php | 37 +++++++++----------
 app/Notifications/ReminderTriggered.php       | 10 ++++-
 3 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/app/Console/Commands/TestReminders.php b/app/Console/Commands/TestReminders.php
index cb044578dc6..6fc7019fa42 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);
 
@@ -57,6 +56,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 7c796ceffa7..2c19c2ff888 100644
--- a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php
+++ b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php
@@ -6,7 +6,6 @@
 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;
@@ -39,24 +38,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 +68,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 b9580e6ef29..b6e6a428132 100644
--- a/app/Notifications/ReminderTriggered.php
+++ b/app/Notifications/ReminderTriggered.php
@@ -34,7 +34,15 @@ 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'];
+        }
+
+        return [];
     }
 
     /**