Skip to content

Commit

Permalink
fix: fix month reminder view (#5914)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jan 19, 2022
1 parent 7bbf0c3 commit 503fb36
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
5 changes: 4 additions & 1 deletion app/Helpers/AccountHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ public static function getUpcomingRemindersForMonth(Account $account, int $month
'nature' => 'reminder',
])
->orderBy('planned_date', 'asc')
->get();
->get()
->filter(function ($reminderOutbox) {
return $reminderOutbox->reminder->contact !== null;
});
}

/**
Expand Down
47 changes: 34 additions & 13 deletions app/Jobs/Reminder/NotifyUserAboutReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,47 @@ public function handle()
$message = $this->getMessage();

if (! is_null($message)) {
// send the notification to this user
$this->sendNotification($message);
$this->scheduleNextReminder();
}

// delete the reminder outbox
$this->reminderOutbox->delete();
}

/**
* Send the notification to this user.
*
* @param MailNotification $message
* @return void
*/
private function sendNotification(MailNotification $message): void
{
if ($this->reminderOutbox->reminder->contact !== null) {
$account = $this->reminderOutbox->user->account;
$hasLimitations = AccountHelper::hasLimitations($account);
if (! $hasLimitations) {
Notification::send($this->reminderOutbox->user, $message);
}

// schedule the next reminder for this user
/** @var \App\Models\Contact\Reminder */
$reminder = $this->reminderOutbox->reminder;
if ($reminder->frequency_type == 'one_time') {
$reminder->inactive = true;
$reminder->save();
} else {
$reminder->schedule($this->reminderOutbox->user);
}
}
}

// delete the reminder outbox
$this->reminderOutbox->delete();
/**
* Schedule the next reminder for this user.
*
* @return void
*/
private function scheduleNextReminder(): void
{
/** @var \App\Models\Contact\Reminder */
$reminder = $this->reminderOutbox->reminder;

if ($reminder->frequency_type == 'one_time') {
$reminder->inactive = true;
$reminder->save();
} else {
$reminder->schedule($this->reminderOutbox->user);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion resources/views/dashboard/_monthReminder.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
<p>{{ trans('dashboard.reminders_none') }}</p>
@endif
</ul>
@endforeach
@endforeach
38 changes: 38 additions & 0 deletions tests/Unit/Jobs/Reminder/NotifyUserAboutReminderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,44 @@ public function it_doesnt_notify_a_user_if_he_is_on_the_free_plan()
);
}

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

Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));

$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => '1',
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
'nature' => 'notification',
]);

$contact->delete();

NotifyUserAboutReminder::dispatch($reminderOutbox);

Notification::assertNotSentTo(
$user,
UserReminded::class
);
}

/** @test */
public function it_marks_the_one_time_reminder_has_inactive_once_it_is_sent()
{
Expand Down

0 comments on commit 503fb36

Please sign in to comment.