Skip to content

Commit

Permalink
Fix reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Jul 22, 2017
1 parent ff497f1 commit 2c2e72d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/Console/Commands/SendNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ public function handle()
$account = $reminder->contact->account;
$reminderDate = $reminder->next_expected_date->hour(0)->minute(0)->second(0)->toDateString();
$sendEmailToUser = false;
$userTimezone = null;

// check if one of the user of the account has the reminder on this day
foreach ($account->users as $user) {
$userCurrentDate = Carbon::now($user->timezone)->hour(0)->minute(0)->second(0)->toDateString();

if ($reminderDate === $userCurrentDate) {
$sendEmailToUser = true;
$userTimezone = $user->timezone;
}
}

Expand All @@ -69,7 +71,7 @@ public function handle()
dispatch(new SendReminderEmail($reminder, $user));
}

dispatch(new SetNextReminderDate($reminder));
dispatch(new SetNextReminderDate($reminder, $userTimezone));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/Jobs/SetNextReminderDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class SetNextReminderDate implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $reminder;
protected $timezone;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Reminder $reminder)
public function __construct(Reminder $reminder, $timezone)
{
$this->reminder = $reminder;
$this->timezone = $timezone;
}

/**
Expand All @@ -40,7 +42,7 @@ public function handle()

default:
$this->reminder->last_triggered = $this->reminder->next_expected_date;
$this->reminder->calculateNextExpectedDate();
$this->reminder->calculateNextExpectedDate($this->timezone);
$this->reminder->save();
break;
}
Expand Down
5 changes: 3 additions & 2 deletions app/Reminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public function getNextExpectedDate()
*
* @return static
*/
public function calculateNextExpectedDate()
public function calculateNextExpectedDate($timezone)
{
$date = $this->next_expected_date;
$date = $this->next_expected_date->setTimezone($timezone);

while ($date->isPast()) {
$date = DateHelper::addTimeAccordingToFrequencyType($date, $this->frequency_type, $this->frequency_number);
Expand All @@ -160,6 +160,7 @@ public function calculateNextExpectedDate()
}

$this->next_expected_date = $date;

return $this;
}
}
11 changes: 6 additions & 5 deletions tests/Unit/ReminderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function testGetNextExpectedDateReturnsString()

public function test_calculate_next_expected_date()
{
$timezone = 'US/Eastern';
$reminder = new Reminder;
$reminder->next_expected_date = '1980-01-01 10:10:10';
$reminder->frequency_number = 1;
Expand All @@ -66,37 +67,37 @@ public function test_calculate_next_expected_date()
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-01-03',
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
);

$reminder->frequency_type = 'month';
$reminder->next_expected_date = '1980-01-01 10:10:10';
$this->assertEquals(
'2017-02-01',
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
);

$reminder->frequency_type = 'year';
$reminder->next_expected_date = '1980-01-01 10:10:10';
$this->assertEquals(
'2018-01-01',
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
);

Carbon::setTestNow(Carbon::create(2017, 1, 1));
$reminder->next_expected_date = '2016-12-25 10:10:10';
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-01-08',
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
);

Carbon::setTestNow(Carbon::create(2017, 1, 1));
$reminder->next_expected_date = '2017-02-02 10:10:10';
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-02-02',
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
);
}
}

0 comments on commit 2c2e72d

Please sign in to comment.