Fix reminders
This commit is contained in:
@@ -54,6 +54,7 @@ class SendNotifications extends Command
|
||||
$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) {
|
||||
@@ -61,6 +62,7 @@ class SendNotifications extends Command
|
||||
|
||||
if ($reminderDate === $userCurrentDate) {
|
||||
$sendEmailToUser = true;
|
||||
$userTimezone = $user->timezone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +71,7 @@ class SendNotifications extends Command
|
||||
dispatch(new SendReminderEmail($reminder, $user));
|
||||
}
|
||||
|
||||
dispatch(new SetNextReminderDate($reminder));
|
||||
dispatch(new SetNextReminderDate($reminder, $userTimezone));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +42,7 @@ class SetNextReminderDate implements ShouldQueue
|
||||
|
||||
default:
|
||||
$this->reminder->last_triggered = $this->reminder->next_expected_date;
|
||||
$this->reminder->calculateNextExpectedDate();
|
||||
$this->reminder->calculateNextExpectedDate($this->timezone);
|
||||
$this->reminder->save();
|
||||
break;
|
||||
}
|
||||
|
||||
+3
-2
@@ -147,9 +147,9 @@ class Reminder extends Model
|
||||
*
|
||||
* @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);
|
||||
@@ -160,6 +160,7 @@ class Reminder extends Model
|
||||
}
|
||||
|
||||
$this->next_expected_date = $date;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ class ReminderTest extends TestCase
|
||||
|
||||
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;
|
||||
@@ -66,21 +67,21 @@ class ReminderTest extends TestCase
|
||||
$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));
|
||||
@@ -88,7 +89,7 @@ class ReminderTest extends TestCase
|
||||
$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));
|
||||
@@ -96,7 +97,7 @@ class ReminderTest extends TestCase
|
||||
$reminder->frequency_type = 'week';
|
||||
$this->assertEquals(
|
||||
'2017-02-02',
|
||||
$reminder->calculateNextExpectedDate()->next_expected_date->toDateString()
|
||||
$reminder->calculateNextExpectedDate($timezone)->next_expected_date->toDateString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user