MIF_E31231302_WEBSITE/app/Console/Kernel.php

159 lines
5.3 KiB
PHP

<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
// $schedule->call(function () {
// app(\App\Http\Controllers\DashboardController::class)
// ->generateNotifikasiHarian();
// })->everyMinute();
/*
|--------------------------------------------------------------------------
| GENERATE NOTIFIKASI HARIAN
|--------------------------------------------------------------------------
|
| HANYA SEKALI SEHARI
| AGAR TIDAK DUPLIKAT & SERVER TIDAK BERAT
|
*/
$schedule->call(function () {
Log::info("GENERATE NOTIFIKASI HARIAN BERJALAN");
app(\App\Http\Controllers\DashboardController::class)
->generateNotifikasiHarian();
})->dailyAt('00:01');
// })->everyMinute();
$schedule->call(function () {
Log::info("=== PROSES KIRIM FCM ===");
$now = now();
DB::table('notifikasi')
->where('is_sent', 0)
->where('waktu_kirim', '<=', $now)
->orderBy('id')
->chunk(100, function ($notifs) {
foreach ($notifs as $notif) {
/*
|--------------------------------------------------------------------------
| CEK KONFIRMASI
|--------------------------------------------------------------------------
*/
if (
$notif->jenis_notif == 'telat_1_jam' ||
$notif->jenis_notif == 'telat_2_jam'
) {
$idMedis = DB::table('pasien_medis')
->where('id_user', $notif->id_user)
->value('id_medis');
$sudahKonfirmasi = DB::table('monitoring_obat')
->where('id_medis', $idMedis)
->whereDate('tanggal', now()->toDateString())
->exists();
if ($sudahKonfirmasi) {
DB::table('notifikasi')
->where('id', $notif->id)
->update([
'is_sent' => 2,
'updated_at' => now()
]);
continue;
}
}
/*
|--------------------------------------------------------------------------
| AMBIL TOKEN
|--------------------------------------------------------------------------
*/
$tokens = DB::table('fcm_tokens')
->where('id_user', $notif->id_user)
->pluck('token')
->toArray();
if (count($tokens) == 0) {
Log::warning(
"TOKEN KOSONG USER {$notif->id_user}"
);
continue;
}
/*
|--------------------------------------------------------------------------
| KIRIM FCM
|--------------------------------------------------------------------------
*/
\App\Http\Controllers\DashboardController::kirimFCM(
$tokens,
$notif
);
/*
|--------------------------------------------------------------------------
| UPDATE STATUS
|--------------------------------------------------------------------------
*/
DB::table('notifikasi')
->where('id', $notif->id)
->update([
'is_sent' => 1,
'updated_at' => now()
]);
Log::info(
"NOTIF TERKIRIM USER {$notif->id_user}"
);
}
});
})->everyMinute();
/*
|--------------------------------------------------------------------------
| HAPUS NOTIFIKASI LAMA
|--------------------------------------------------------------------------
*/
$schedule->call(function () {
DB::table('notifikasi')
->where('created_at', '<', now()->subDays(30))
->delete();
Log::info("NOTIFIKASI LAMA DIHAPUS");
})->dailyAt('01:00');
}
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}