Skip to content

Commit

Permalink
fix: skip contactreminders for deleted contacts (#7223)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Apr 8, 2024
1 parent c8cc301 commit 7f932bd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,28 @@ public function handle()

$contactReminder = ContactReminder::find($scheduledReminder->contact_reminder_id);
$contact = $contactReminder->contact;
$contactName = NameHelper::formatContactName($userNotificationChannel->user, $contact);

if ($userNotificationChannel->type === UserNotificationChannel::TYPE_EMAIL) {
Notification::route('mail', $userNotificationChannel->content)
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName));
}
if ($contact !== null) {
$contactName = NameHelper::formatContactName($userNotificationChannel->user, $contact);

if ($userNotificationChannel->type === UserNotificationChannel::TYPE_EMAIL) {
Notification::route('mail', $userNotificationChannel->content)
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName));
} elseif ($userNotificationChannel->type === UserNotificationChannel::TYPE_TELEGRAM) {
Notification::route('telegram', $userNotificationChannel->content)
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName));
}

if ($userNotificationChannel->type === UserNotificationChannel::TYPE_TELEGRAM) {
Notification::route('telegram', $userNotificationChannel->content)
->notify(new ReminderTriggered($userNotificationChannel, $contactReminder->label, $contactName));
$this->updateNumberOfTimesTriggered($scheduledReminder->contact_reminder_id);

(new RescheduleContactReminderForChannel())->execute([
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,
'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id,
'contact_reminder_scheduled_id' => $scheduledReminder->id,
]);
}

$this->updateScheduledContactReminderTriggeredAt($scheduledReminder);
$this->updateNumberOfTimesTriggered($scheduledReminder->contact_reminder_id);

(new RescheduleContactReminderForChannel())->execute([
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,
'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id,
'contact_reminder_scheduled_id' => $scheduledReminder->id,
]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use App\Models\ContactReminder;
use App\Models\UserNotificationChannel;
use App\Notifications\ReminderTriggered;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
Expand Down Expand Up @@ -81,4 +81,37 @@ public function it_cant_process_the_scheduled_contact_reminders(): void

Notification::assertNothingSent();
}

/**
* @test
*/
public function it_does_not_process_reminders_for_deleted_contacts(): void
{
Notification::fake();

Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));

$contactReminder = ContactReminder::factory()->create([
'type' => ContactReminder::TYPE_RECURRING_DAY,
'label' => 'test',
]);
$channel = UserNotificationChannel::factory()->create([
'type' => UserNotificationChannel::TYPE_EMAIL,
'content' => 'admin@admin.com',
]);
DB::table('contact_reminder_scheduled')->insertGetId([
'user_notification_channel_id' => $channel->id,
'contact_reminder_id' => $contactReminder->id,
'scheduled_at' => Carbon::now(),
'triggered_at' => null,
]);

$contactReminder->contact->delete();

$job = new ProcessScheduledContactReminders();
$job->dispatch();
$job->handle();

Notification::assertNothingSent();
}
}

0 comments on commit 7f932bd

Please sign in to comment.