diff --git a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php index 1884848f3a5..6db9e863291 100644 --- a/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php +++ b/app/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactReminders.php @@ -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, - ]); } } diff --git a/tests/Unit/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactRemindersTest.php b/tests/Unit/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactRemindersTest.php index f3b4c37ac0e..a66095d71b1 100644 --- a/tests/Unit/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactRemindersTest.php +++ b/tests/Unit/Domains/Contact/ManageReminders/Jobs/ProcessScheduledContactRemindersTest.php @@ -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; @@ -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(); + } }