From ca946b2aae9cdf6afb177d48f077079cb44aa1bc Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Mon, 15 Dec 2025 09:43:07 +0330 Subject: [PATCH] feat: add intervals --- app/Console/Commands/SendDueReminders.php | 66 ++++++++++++++++------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/app/Console/Commands/SendDueReminders.php b/app/Console/Commands/SendDueReminders.php index b77c2f7..38bf0a1 100644 --- a/app/Console/Commands/SendDueReminders.php +++ b/app/Console/Commands/SendDueReminders.php @@ -30,24 +30,52 @@ public function handle(): int $this->info("Current time: {$now}"); $this->info("Current time string: {$currentTime}"); - $reminders = Reminder::with(['user', 'packageName']) - ->where(function ($query) use ($now, $currentTime) { - // For PostgreSQL: Cast datetime to time for comparison - $query->whereRaw('time::time <= ?', [$currentTime]) - ->where(function ($sub) use ($now) { - // Non-repeatable: send once - $sub->where('repeatable', false) - ->whereNull('last_repeat') - // OR repeatable: not sent today - ->orWhere('repeatable', true) - ->where(function ($inner) use ($now) { - $inner->whereNull('last_repeat') - ->orWhereDate('last_repeat', '<', $now->toDateString()); - }); - }); - }) - ->get(); + ->where(function ($query) use ($now, $currentTime) { + + // time reached today + $query->whereRaw('time::time <= ?', [$currentTime]) + + ->where(function ($sub) use ($now) { + + // non-repeatable (send once) + $sub->where('repeatable', false) + ->whereNull('last_repeat') + + // repeatable with interval + ->orWhere(function ($repeat) use ($now) { + $repeat->where('repeatable', true) + ->where(function ($interval) use ($now) { + $interval + ->whereNull('last_repeat') + ->orWhereRaw( + "last_repeat + (COALESCE(repeat_days, 1) || ' days')::interval <= ?", + [$now] + ); + }); + }); + }); + }) + ->get(); + + + // $reminders = Reminder::with(['user', 'packageName']) + // ->where(function ($query) use ($now, $currentTime) { + // // For PostgreSQL: Cast datetime to time for comparison + // $query->whereRaw('time::time <= ?', [$currentTime]) + // ->where(function ($sub) use ($now) { + // // Non-repeatable: send once + // $sub->where('repeatable', false) + // ->whereNull('last_repeat') + // // OR repeatable: not sent today + // ->orWhere('repeatable', true) + // ->where(function ($inner) use ($now) { + // $inner->whereNull('last_repeat') + // ->orWhereDate('last_repeat', '<', $now->toDateString()); + // }); + // }); + // }) + // ->get(); // Debug: Show what reminders were found $this->info("Found {$reminders->count()} reminders"); @@ -91,9 +119,7 @@ public function handle(): int ); } - $scheduledTime = Carbon::parse($reminder->time); - $scheduledTime->setDate($now->year, $now->month, $now->day); - $reminder->last_repeat = $scheduledTime; + $reminder->last_repeat = $now; $reminder->save(); $this->info("Queued push notification for reminder {$reminder->id}."); }