Skip to content

Commit

Permalink
fix: fix notifications looping when processing the batch (monicahq/ch…
Browse files Browse the repository at this point in the history
…andler#392)

Close #390
Close #391
  • Loading branch information
djaiss committed Jan 11, 2023
1 parent 03f332c commit dd6d45f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
8 changes: 6 additions & 2 deletions app/Console/Commands/TestReminders.php
Expand Up @@ -46,17 +46,21 @@ 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);

Notification::route('mail', $channel->content)
->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
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion app/Notifications/ReminderTriggered.php
Expand Up @@ -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 [];
}

/**
Expand Down

0 comments on commit dd6d45f

Please sign in to comment.