Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
fix: fix notifications looping when processing the batch
Browse files Browse the repository at this point in the history
Close #390
Close #391
  • Loading branch information
djaiss committed Jan 11, 2023
1 parent d7a5904 commit 5f8d7a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
5 changes: 5 additions & 0 deletions app/Console/Commands/TestReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/Notifications/ReminderTriggered.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

/**
Expand Down

0 comments on commit 5f8d7a3

Please sign in to comment.