diff --git a/app/Console/Commands/Onetime/UpdateResourceDescriptions.php b/app/Console/Commands/Onetime/UpdateResourceDescriptions.php new file mode 100644 index 000000000..09c69a5ac --- /dev/null +++ b/app/Console/Commands/Onetime/UpdateResourceDescriptions.php @@ -0,0 +1,129 @@ +option('dry-run'); + + if (!Storage::disk('excel')->exists($this->filePath)) { + $this->error("Excel file not found: {$this->filePath}"); + return self::FAILURE; + } + + $fullPath = Storage::disk('excel')->path($this->filePath); + $this->info("Reading: {$this->filePath}"); + + $rows = Excel::toArray([], $fullPath); + + if (empty($rows) || empty($rows[0])) { + $this->error('No data found in the Excel file.'); + return self::FAILURE; + } + + // Skip Row 1 is header + $data = collect($rows[0])->skip(1); + $results = []; + $updated = 0; + + $bar = $this->output->createProgressBar($data->count()); + $bar->start(); + + foreach ($data as $row) { + $name = trim((string)($row['Name of the resource'] ?? $row[0] ?? '')); + $desc = trim((string)($row['Description of the resource'] ?? $row[1] ?? '')); + + // skip blank lines + if ($name === '' || $desc === '') { + $bar->advance(); + continue; + } + + $resource = ResourceItem::whereRaw('TRIM(name) = ?', [$name])->first(); + + if (!$resource) { + $results[] = [ + 'name' => $name, + 'status' => 'NOT FOUND', + 'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''), + ]; + $bar->advance(); + continue; + } + + if ($dryRun) { + $results[] = [ + 'name' => $name, + 'status' => 'WOULD UPDATE', + 'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''), + ]; + } else { + $resource->description = $desc; + $resource->save(); + + $results[] = [ + 'name' => $name, + 'status' => 'UPDATED', + 'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''), + ]; + } + + $updated++; + $bar->advance(); + } + + $bar->finish(); + $this->newLine(2); + + // summary + $this->info(sprintf( + 'Processed: %d | Updated: %d | Not Found: %d', + $data->count(), + $updated, + count(array_filter($results, fn($r) => $r['status'] === 'NOT FOUND')) + )); + + // print table of results + $this->newLine(); + $this->table(['Resource Name', 'Status', 'Description (excerpt)'], $results); + + // log summary + Log::info('Resource descriptions update completed', [ + 'dry_run' => $dryRun, + 'total_rows' => $data->count(), + 'updated' => $updated, + 'not_found' => array_filter($results, fn($r) => $r['status'] === 'NOT FOUND'), + ]); + + if ($dryRun) { + $this->warn('Dry run only — no data was modified.'); + } + + return self::SUCCESS; + } +} diff --git a/resources/excel/Uploaded resources.xlsx b/resources/excel/Uploaded resources.xlsx new file mode 100644 index 000000000..8b1a5bd43 Binary files /dev/null and b/resources/excel/Uploaded resources.xlsx differ