97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Jobs\ReindexDocumentJob;
|
|
use App\Jobs\ReindexKnowledgeItemJob;
|
|
use App\Models\Category;
|
|
use App\Models\DocumentVersion;
|
|
use App\Models\KnowledgeItem;
|
|
use App\Services\KnowledgeBase\AuditService;
|
|
use Illuminate\Console\Command;
|
|
|
|
/**
|
|
* php artisan kb:reindex-category {--category_id=} {--slug=} {--dry-run}
|
|
*
|
|
* Contoh:
|
|
* php artisan kb:reindex-category --category_id=1
|
|
* php artisan kb:reindex-category --slug=pelesenan
|
|
* php artisan kb:reindex-category --slug=pelesenan --dry-run
|
|
*/
|
|
class ReindexCategoryCommand extends Command
|
|
{
|
|
protected $signature = 'kb:reindex-category
|
|
{--category_id= : ID kategori}
|
|
{--slug= : Slug kategori}
|
|
{--dry-run : Papar sahaja tanpa dispatch}';
|
|
|
|
protected $description = 'Reindex semua dokumen dan knowledge items dalam satu kategori';
|
|
|
|
public function handle(AuditService $auditService): int
|
|
{
|
|
$category = null;
|
|
|
|
if ($id = $this->option('category_id')) {
|
|
$category = Category::find((int) $id);
|
|
} elseif ($slug = $this->option('slug')) {
|
|
$category = Category::where('slug', $slug)->first();
|
|
}
|
|
|
|
if (!$category) {
|
|
$this->error('Kategori tidak dijumpai.');
|
|
$this->listCategories();
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$dryRun = $this->option('dry-run');
|
|
|
|
$this->info("Kategori: {$category->name}");
|
|
$this->line(str_repeat('─', 50));
|
|
|
|
// Dokumen
|
|
$versions = DocumentVersion::whereHas('document', fn($q) => $q->where('category_id', $category->id))
|
|
->where('is_current', true)
|
|
->where('processing_status', DocumentVersion::STATUS_INDEXED)
|
|
->with('document')
|
|
->get();
|
|
|
|
$this->info("Dokumen (versi semasa): {$versions->count()}");
|
|
foreach ($versions as $v) {
|
|
$this->line(" → {$v->document->title} v{$v->version_number}");
|
|
if (!$dryRun) {
|
|
ReindexDocumentJob::dispatch($v->id);
|
|
}
|
|
}
|
|
|
|
// Knowledge Items
|
|
$items = KnowledgeItem::where('category_id', $category->id)
|
|
->where('is_active', true)
|
|
->get();
|
|
|
|
$this->info("\nKnowledge Items: {$items->count()}");
|
|
foreach ($items as $item) {
|
|
$this->line(" → [{$item->item_type}] {$item->title}");
|
|
if (!$dryRun) {
|
|
ReindexKnowledgeItemJob::dispatch($item->id);
|
|
}
|
|
}
|
|
|
|
if ($dryRun) {
|
|
$this->warn("\n[DRY RUN] Tiada job dihantar. Buang --dry-run untuk reindex sebenar.");
|
|
} else {
|
|
$auditService->systemReindexStarted("category:{$category->slug}");
|
|
$this->info("\n✓ Semua job telah dijadualkan.");
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function listCategories(): void
|
|
{
|
|
$this->info("\nKategori tersedia:");
|
|
Category::orderBy('name')->get()->each(function ($c) {
|
|
$this->line(" ID: {$c->id} Slug: {$c->slug} Nama: {$c->name}");
|
|
});
|
|
}
|
|
}
|