From ff11db177fc8838f8dc04fe7701cf4f2d2d25879 Mon Sep 17 00:00:00 2001 From: Iqbal Date: Tue, 5 May 2026 14:07:33 +0700 Subject: [PATCH] feat: add sync command for log_type from telemedicine --- .../Commands/SyncLogTypeFromTelemedicine.php | 126 ++++++++++++++++++ config/database.php | 13 ++ 2 files changed, 139 insertions(+) create mode 100644 app/Console/Commands/SyncLogTypeFromTelemedicine.php diff --git a/app/Console/Commands/SyncLogTypeFromTelemedicine.php b/app/Console/Commands/SyncLogTypeFromTelemedicine.php new file mode 100644 index 00000000..f6d08fa8 --- /dev/null +++ b/app/Console/Commands/SyncLogTypeFromTelemedicine.php @@ -0,0 +1,126 @@ +option('dry-run'); + + if ($isDryRun) { + $this->info('🔍 DRY RUN MODE - No database changes will be made'); + } + + $this->info('Starting sync log_type from telemedicine...'); + + // Get telemedicine DB config + $telemedicineDb = config('database.connections.telemedicine'); + + if (!$telemedicineDb) { + $this->error('Telemedicine database connection not configured!'); + return Command::FAILURE; + } + + // Query prime_logs from telemedicine with log_code + $primeLogs = DB::connection('telemedicine') + ->table('prime_logs') + ->whereNotNull('log_code') + ->where('log_code', '!=', '') + ->where('status', 'success') + ->select('log_code', 'type') + ->get(); + + if ($primeLogs->isEmpty()) { + $this->warn('No prime_logs found with log_code'); + return Command::SUCCESS; + } + + $this->info("Found {$primeLogs->count()} prime_logs records"); + + $updated = 0; + $skipped = 0; + $notFound = 0; + + $bar = $this->output->createProgressBar($primeLogs->count()); + $bar->start(); + + foreach ($primeLogs as $primeLog) { + // Find matching request_log by code + $requestLog = DB::table('request_logs') + ->where('code', $primeLog->log_code) + ->whereNull('deleted_at') + ->first(); + + if (!$requestLog) { + $notFound++; + $bar->advance(); + continue; + } + + // Skip if log_type already set correctly + if ($requestLog->log_type === $primeLog->type) { + $skipped++; + $bar->advance(); + continue; + } + + if (!$isDryRun) { + // Update log_type + DB::table('request_logs') + ->where('id', $requestLog->id) + ->update([ + 'log_type' => $primeLog->type, + 'updated_at' => now() + ]); + } + + $updated++; + $bar->advance(); + } + + $bar->finish(); + $this->newLine(2); + + // Summary + $this->info('✅ Sync completed!'); + $this->table( + ['Status', 'Count'], + [ + ['Updated', $updated], + ['Skipped (already correct)', $skipped], + ['Not found in request_logs', $notFound], + ['Total processed', $primeLogs->count()], + ] + ); + + if ($isDryRun) { + $this->warn('⚠️ This was a DRY RUN. Run without --dry-run to apply changes.'); + } + + return Command::SUCCESS; + } +} diff --git a/config/database.php b/config/database.php index 2b1a1a86..ad716693 100644 --- a/config/database.php +++ b/config/database.php @@ -83,6 +83,19 @@ return [ ]) : [], ], + 'telemedicine' => [ + 'driver' => 'mysql', + 'host' => env('TELEMEDICINE_DB_HOST', '127.0.0.1'), + 'port' => env('TELEMEDICINE_DB_PORT', '3306'), + 'database' => env('TELEMEDICINE_DB_DATABASE', 'forge'), + 'username' => env('TELEMEDICINE_DB_USERNAME', 'forge'), + 'password' => env('TELEMEDICINE_DB_PASSWORD', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'strict' => false, + ], + 'pgsql' => [ 'driver' => 'pgsql', 'url' => env('DATABASE_URL'),