Files
2026-06-10 08:30:03 +00:00

134 lines
4.4 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Reminder;
use App\Jobs\SendPushJob;
use App\Jobs\SendPushToPackageJob;
use Carbon\Carbon;
class SendDueReminders extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'reminders:send-due';
/**
* The console command description.
*/
protected $description = 'Send push notifications for due reminders';
/**
* Execute the console command.
*/
public function handle(): int
{
$now = now();
$currentTime = $now->toTimeString(); // HH:MM:SS
$this->info("Current time: {$now}");
$this->info("Current time string: {$currentTime}");
$reminders = Reminder::with(['user', 'packageName'])
->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");
foreach ($reminders as $reminder) {
$this->info("Reminder ID {$reminder->id}: time={$reminder->time}, last_repeat={$reminder->last_repeat}");
}
if ($reminders->isEmpty()) {
$this->info('No reminders due at this time.');
return Command::SUCCESS;
}
foreach ($reminders as $reminder) {
$packageName = $reminder->packageName;
// Check if reminder has a specific user
if ($reminder->user_id) {
$user = $reminder->user;
$relation = $user->packageNames()->where('package_names.id', $packageName->id)->first();
$fcmToken = $relation?->pivot?->fcm_token;
if ($fcmToken) {
SendPushJob::dispatch(
user: $user,
title: $reminder->title,
packageName: $packageName,
description: $reminder->description ?? '',
data: $reminder->data ?? []
);
}
}
// Otherwise, send to all users that have this package
else {
// Just dispatch the job once for all users with this package
SendPushToPackageJob::dispatch(
$packageName,
$reminder->title,
$reminder->description ?? '',
$reminder->data ?? []
);
}
$scheduled = Carbon::parse($reminder->time)
->setDate($now->year, $now->month, $now->day);
$reminder->last_repeat = $scheduled;
$reminder->save();
$this->info("Queued push notification for reminder {$reminder->id}.");
}
return Command::SUCCESS;
}
}