Skip to content

Commit dd6d45f

Browse files
authored
fix: fix notifications looping when processing the batch (monicahq/chandler#392)
Close #390 Close #391
1 parent 03f332c commit dd6d45f

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

app/Console/Commands/TestReminders.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,21 @@ public function handle(): void
4646

4747
foreach ($scheduledContactReminders as $scheduledReminder) {
4848
$channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id);
49+
$contactReminder = ContactReminder::findOrFail($scheduledReminder->contact_reminder_id);
4950

5051
if ($channel->type == UserNotificationChannel::TYPE_EMAIL && $channel->active) {
51-
$contactReminder = ContactReminder::findOrFail($scheduledReminder->contact_reminder_id);
52-
5352
$contact = $contactReminder->contact;
5453
$contactName = NameHelper::formatContactName($channel->user, $contact);
5554

5655
Notification::route('mail', $channel->content)
5756
->notify(new ReminderTriggered($channel, $contactReminder->label, $contactName));
5857
}
5958

59+
if ($channel->type === UserNotificationChannel::TYPE_TELEGRAM) {
60+
Notification::route('telegram', $channel->content)
61+
->notify(new ReminderTriggered($channel, $contactReminder->label, ''));
62+
}
63+
6064
try {
6165
(new RescheduleContactReminderForChannel([
6266
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,

app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\Helpers\NameHelper;
77
use App\Models\ContactReminder;
88
use App\Models\UserNotificationChannel;
9-
use App\Models\UserNotificationSent;
109
use App\Notifications\ReminderTriggered;
1110
use Carbon\Carbon;
1211
use Illuminate\Bus\Queueable;
@@ -39,24 +38,24 @@ public function handle()
3938
->get();
4039

4140
foreach ($scheduledContactReminders as $scheduledReminder) {
42-
$channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id);
41+
$userNotificationChannel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id);
4342

44-
if ($channel->type === UserNotificationChannel::TYPE_EMAIL) {
45-
$contactReminder = ContactReminder::find($scheduledReminder->contact_reminder_id);
43+
$contactReminder = ContactReminder::find($scheduledReminder->contact_reminder_id);
44+
45+
if ($userNotificationChannel->type === UserNotificationChannel::TYPE_EMAIL) {
4646
$contact = $contactReminder->contact;
47-
$contactName = NameHelper::formatContactName($channel->user, $contact);
47+
$contactName = NameHelper::formatContactName($userNotificationChannel->user, $contact);
4848

49-
Notification::route('mail', $channel->content)
50-
->notify(new ReminderTriggered($channel, $contactReminder->label, $contactName));
49+
Notification::route('mail', $userNotificationChannel->content)
50+
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName));
51+
}
5152

52-
UserNotificationSent::create([
53-
'user_notification_channel_id' => $channel->id,
54-
'sent_at' => Carbon::now(),
55-
'subject_line' => $contactReminder->label,
56-
]);
53+
if ($userNotificationChannel->type === UserNotificationChannel::TYPE_TELEGRAM) {
54+
Notification::route('telegram', $userNotificationChannel->content)
55+
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, ''));
5756
}
5857

59-
$this->updateScheduledContactReminderTriggeredAt($scheduledReminder->id);
58+
$this->updateScheduledContactReminderTriggeredAt($scheduledReminder);
6059
$this->updateNumberOfTimesTriggered($scheduledReminder->contact_reminder_id);
6160

6261
$this->appendToChain(
@@ -69,13 +68,13 @@ public function handle()
6968
}
7069
}
7170

72-
private function updateScheduledContactReminderTriggeredAt(int $id): void
71+
private function updateScheduledContactReminderTriggeredAt($scheduledReminder): void
7372
{
74-
DB::table('contact_reminder_scheduled')
75-
->where('id', $id)
76-
->update([
77-
'triggered_at' => Carbon::now(),
78-
]);
73+
(new RescheduleContactReminderForChannel([
74+
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,
75+
'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id,
76+
'contact_reminder_scheduled_id' => $scheduledReminder->id,
77+
]))->handle();
7978
}
8079

8180
private function updateNumberOfTimesTriggered(int $id): void

app/Notifications/ReminderTriggered.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ public function __construct(
3434
*/
3535
public function via($notifiable)
3636
{
37-
return ['mail', 'telegram'];
37+
if ($this->channel->type === UserNotificationChannel::TYPE_EMAIL) {
38+
return ['mail'];
39+
}
40+
41+
if ($this->channel->type === UserNotificationChannel::TYPE_TELEGRAM) {
42+
return ['telegram'];
43+
}
44+
45+
return [];
3846
}
3947

4048
/**

0 commit comments

Comments
 (0)