feat: add sync command for log_type from telemedicine
This commit is contained in:
126
app/Console/Commands/SyncLogTypeFromTelemedicine.php
Normal file
126
app/Console/Commands/SyncLogTypeFromTelemedicine.php
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
class SyncLogTypeFromTelemedicine extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'sync:log-type {--dry-run : Run without updating database}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Sync log_type from telemedicine prime_logs to request_logs table';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$isDryRun = $this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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' => [
|
'pgsql' => [
|
||||||
'driver' => 'pgsql',
|
'driver' => 'pgsql',
|
||||||
'url' => env('DATABASE_URL'),
|
'url' => env('DATABASE_URL'),
|
||||||
|
|||||||
Reference in New Issue
Block a user