From a68be76015d098377aa84f9ee521c3c829833142 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Fri, 10 Apr 2020 13:23:06 -0500 Subject: [PATCH] Make AlertSchedule::isActive work --- app/Models/AlertSchedule.php | 21 +++++++------------ .../2020_04_08_172357_alert_schedule_utc.php | 6 ++++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/app/Models/AlertSchedule.php b/app/Models/AlertSchedule.php index 36eb060c4e..d7f8c7b2bd 100644 --- a/app/Models/AlertSchedule.php +++ b/app/Models/AlertSchedule.php @@ -40,7 +40,8 @@ class AlertSchedule extends Model public function scopeIsActive($query) { return $query->where(function ($query) { - $now = CarbonImmutable::now(); + $now = CarbonImmutable::now('UTC'); + $query->where(function ($query) use ($now) { // Non recurring simply between start and end $query->where('recurring', 0) @@ -49,22 +50,14 @@ class AlertSchedule extends Model })->orWhere(function ($query) use ($now) { $query->where('recurring', 1) // Check the time is after the start date and before the end date, or end date is not set - ->where(function ($query) use ($now) { - $query->where('start_recurring_dt', '<=', $now->format('%Y-%m-%d')) - ->where(function ($query) use ($now) { - $query->where('end_recurring_dt', '>=', $now->format('%Y-%m-%d')) - ->orWhereNull('end_recurring_dt') - ->orWhere('end_recurring_dt', '0000-00-00') - ->orWhere('end_recurring_dt', ''); - }); - }) - // Check the time is between the start and end hour/minutes/seconds - ->where('start_recurring_hr', '<=', $now->format('%H:%i:%s')) - ->where('end_recurring_hr', '>=', $now->format('%H:%i:%s')) + ->where('start', '<=', $now) + ->where('end', '>=', $now) + ->whereTime('start', '<=', $now->toTimeString()) + ->whereTime('end', '>=', $now->toTimeString()) // Check we are on the correct day of the week ->where(function ($query) use ($now) { /** @var Builder $query */ - $query->where('recurring_day', 'like', '%' . $now->format('%w') . '%') + $query->where('recurring_day', 'like', $now->format('%N%')) ->orWhereNull('recurring_day') ->orWhere('recurring_day', ''); }); diff --git a/database/migrations/2020_04_08_172357_alert_schedule_utc.php b/database/migrations/2020_04_08_172357_alert_schedule_utc.php index aabe298057..c10a98a255 100644 --- a/database/migrations/2020_04_08_172357_alert_schedule_utc.php +++ b/database/migrations/2020_04_08_172357_alert_schedule_utc.php @@ -12,9 +12,10 @@ class AlertScheduleUtc extends Migration */ public function up() { - DB::table('alert_schedule')->where('recurring', 1)->update([ + DB::table('alert_schedule')->update([ 'start' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(start_recurring_dt, ' ', start_recurring_hr), '%Y-%m-%d %H:%i:%s'), start), @@global.time_zone, '+00:00')"), - 'end' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(end_recurring_dt, ' ', end_recurring_hr), '%Y-%m-%d %H:%i:%s'), end), @@global.time_zone, '+00:00')"), + 'end' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(IFNULL(end_recurring_dt, '9000-9-9'), ' ', end_recurring_hr), '%Y-%m-%d %H:%i:%s'), end), @@global.time_zone, '+00:00')"), + 'recurring_day' => DB::raw('REPLACE(recurring_day, 0, 7)'), // convert to RFC N date format ]); Schema::table('alert_schedule', function (Blueprint $table) { @@ -43,6 +44,7 @@ class AlertScheduleUtc extends Migration 'start_recurring_hr' => DB::raw("TIME(CONVERT_TZ(start, '+00:00', @@global.time_zone))"), 'end_recurring_dt' => DB::raw("DATE(CONVERT_TZ(end, '+00:00', @@global.time_zone))"), 'end_recurring_hr' => DB::raw("TIME(CONVERT_TZ(end, '+00:00', @@global.time_zone))"), + 'recurring_day' => DB::raw('REPLACE(recurring_day, 7, 0)'), ]); } }