HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago-portal-updates/app/Console/Commands/SendOrderReminderNotification.php
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use App\Models\Order;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Notifications\OrderReminderNotification;

class SendOrderReminderNotification extends Command
{
    protected $signature = 'send:order-reminder';
    protected $description = 'Send order reminder notification to users.';

    public function handle()
    {
        $currentDate = Carbon::now()->format('Y-m-d'); // Get today's date (YYYY-MM-DD)
        $currentTime = Carbon::now()->addMinutes(5)->format('H:i'); // Get time + 5 minutes (24-hour format)

        $this->info("Checking orders for: {$currentDate} at {$currentTime}");

        // Fetch orders with today's date and exact estimated arrival time
        $orders = DB::select("SELECT * FROM orders WHERE DATE(FROM_UNIXTIME(order_placed_at)) = ? AND estimated_arival_time = ?", [$currentDate, $currentTime]);

        $orders = collect($orders); // Convert to collection

        if ($orders->isEmpty()) {
            $this->info("No orders found for notification.");
            return;
        }

        $this->info("Orders found for notification: " . $orders->count());

        foreach ($orders as $order) {
            if ($order->user_id) {
                $user = User::find($order->user_id);
                if (!$user) {
                    $this->info("User not found for Order ID: {$order->id}");
                    continue;
                }
                if (!$user->fcm_token) {
                    $this->info("Firebase token not found for User ID: {$user->id}");
                    continue;
                }
                $this->info($user->fcm_token);

                // Send notification
                $notification = new OrderReminderNotification($order);
                $notification->toDatabase($user);
                $sent = $notification->sendFirebaseNotification($user);
                $this->info($sent);
                $this->info("Notification sent to: {$user->name} ({$user->email}) for Order ID: {$order->id}");
            }
        }
    }
}