updated apis

This commit is contained in:
m.razaghi
2026-07-10 23:22:56 +03:30
parent d19d4b8667
commit 3b9fcd76df
2 changed files with 102 additions and 21 deletions

View File

@@ -46,14 +46,17 @@ class GlobalSearchApiController extends Controller
if (!empty($searchTerm)) {
$limitPerTable = 100; // Performance safeguard
// 1. Search Bylaw Documents
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
// 1. Search Bylaw Documents (Including deeper node_content search)
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%");
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
});
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatBylaw($item);
$formatted['type'] = 'bylaw';
@@ -130,11 +133,9 @@ class GlobalSearchApiController extends Controller
if ($sortBy === 'number') {
$sortCallback = function ($item) {
// Extract whatever number property this specific item uses
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? $item['number'] ?? '');
};
// Use Natural Sorting so "2" comes before "10"
if ($sortOrder === 'asc') {
$results = $results->sortBy($sortCallback, SORT_NATURAL)->values();
} else {
@@ -142,7 +143,6 @@ class GlobalSearchApiController extends Controller
}
} elseif ($sortBy === 'date') {
$sortCallback = function ($item) {
// Extract whatever date property this specific item uses (Note: Structural models will default to '' and group together)
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '');
};
@@ -186,19 +186,21 @@ class GlobalSearchApiController extends Controller
$results = collect();
$totalCount = 0;
// Take a max of 3 from each table so the quick-search array doesn't become too heavy
$limitPerTable = 3;
if (!empty($searchTerm)) {
// 1. Bylaw Documents
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%");
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
$totalCount += $bylawQuery->count();
$bylaws = $bylawQuery->take($limitPerTable)->get()->map(function($item) {
@@ -297,6 +299,12 @@ class GlobalSearchApiController extends Controller
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
// Fallback description to node_content if empty
$description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
}
return [
'id' => $item->id,
'title' => $item->title,
@@ -304,7 +312,7 @@ class GlobalSearchApiController extends Controller
'approval_authority_name' => optional($item->approvalAuthority)->name,
'approval_date' => $item->approval_date,
'promulgation_number' => $item->promulgation_number,
'description' => $isLocked ? null : $item->description,
'description' => $isLocked ? null : $description,
'is_locked' => $isLocked,
];
}
@@ -357,7 +365,7 @@ class GlobalSearchApiController extends Controller
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number, // Added for sorting capability
'number' => $item->number,
'text' => $isLocked ? null : $context,
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)),
@@ -378,7 +386,7 @@ class GlobalSearchApiController extends Controller
$payload = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number, // Available for sorting capability
'number' => $item->number,
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)),
'law' => $law?->title ?? '',