NAKS Sync tab. */ use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Artisan; use App\Services\NaksSyncService; // Check admin permission $u = Auth::user(); if (!isset($u->level) || $u->level !== 'Admin') { echo json_encode([ 'status' => 'error', 'message' => 'Permission denied. Only admins can run sync.' ]); exit; } // Get module from request $module = $request->input('module', 'all'); // Validate module $validModules = ['all', 'technology', 'welder', 'consumables', 'expert', 'equipment']; if (!in_array($module, $validModules)) { echo json_encode([ 'status' => 'error', 'message' => "Invalid module: {$module}. Valid options: " . implode(', ', $validModules) ]); exit; } // Log the sync attempt Log::info("NAKS Sync initiated from admin panel", [ 'user' => $u->email, 'module' => $module, ]); $startTime = microtime(true); try { // Create sync service instance $syncService = app(NaksSyncService::class); // Run sync $stats = $syncService->sync( $module, // module filter null, // specific project false, // dry run false // force ); $duration = round(microtime(true) - $startTime, 2); // Log completion Log::info("NAKS Sync completed from admin panel", [ 'user' => $u->email, 'module' => $module, 'duration' => $duration, 'stats' => $stats, ]); echo json_encode([ 'status' => 'success', 'message' => 'Sync completed successfully', 'stats' => $stats, 'duration' => "{$duration}s", ]); } catch (\Exception $e) { $duration = round(microtime(true) - $startTime, 2); Log::error("NAKS Sync failed from admin panel", [ 'user' => $u->email, 'module' => $module, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); echo json_encode([ 'status' => 'error', 'message' => 'Sync failed: ' . $e->getMessage(), 'duration' => "{$duration}s", ]); } ?>