Fixed api issue

This commit is contained in:
m.razaghi
2026-07-11 12:25:46 +03:30
parent 3b9fcd76df
commit ffea0e3c11
2 changed files with 43 additions and 24 deletions

View File

@@ -51,7 +51,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100', 'per_page' => 'nullable|integer|min:1|max:100',
]); ]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true); ->where('is_published', true);
if ($request->filled('category_id')) { if ($request->filled('category_id')) {
@@ -96,7 +96,7 @@ class BylawApiController extends Controller
public function single($id) public function single($id)
{ {
$bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->find($id); ->find($id);
@@ -160,7 +160,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100', 'per_page' => 'nullable|integer|min:1|max:100',
]); ]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true); ->where('is_published', true);
if ($request->filled('search')) { if ($request->filled('search')) {
@@ -171,8 +171,10 @@ class BylawApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', '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) { ->orWhereIn('id', function($subQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); $subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
}); });
}); });
} }
@@ -209,7 +211,7 @@ class BylawApiController extends Controller
{ {
$searchTerm = $request->input('search'); $searchTerm = $request->input('search');
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true); ->where('is_published', true);
if (!empty($searchTerm)) { if (!empty($searchTerm)) {
@@ -218,8 +220,10 @@ class BylawApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', '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) { ->orWhereIn('id', function($subQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); $subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
}); });
}); });
} }
@@ -237,7 +241,7 @@ class BylawApiController extends Controller
public function pager($id) public function pager($id)
{ {
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->find($id); ->find($id);
@@ -248,7 +252,7 @@ class BylawApiController extends Controller
], 404); ], 404);
} }
$previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->where(function ($query) use ($current) { ->where(function ($query) use ($current) {
if ($current->approval_date) { if ($current->approval_date) {
@@ -265,7 +269,7 @@ class BylawApiController extends Controller
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->first(); ->first();
$nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->where(function ($query) use ($current) { ->where(function ($query) use ($current) {
if ($current->approval_date) { if ($current->approval_date) {
@@ -303,10 +307,15 @@ class BylawApiController extends Controller
{ {
$isLocked = $this->isLockedForCurrentUser($item); $isLocked = $this->isLockedForCurrentUser($item);
// Map description: If description is null, combine all node_content into one full text!
$description = $item->description; $description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n"); // Direct Query Fallback: If description is empty, fetch node_content manually
if (empty($description)) {
$description = BylawNode::where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->pluck('node_content')
->filter()
->implode("\n\n");
} }
return [ return [

View File

@@ -7,6 +7,7 @@ use App\Models\Art;
use App\Models\Book; use App\Models\Book;
use App\Models\Branch; use App\Models\Branch;
use App\Models\BylawDocument; use App\Models\BylawDocument;
use App\Models\BylawNode;
use App\Models\Chapter; use App\Models\Chapter;
use App\Models\Division; use App\Models\Division;
use App\Models\Gate; use App\Models\Gate;
@@ -46,16 +47,18 @@ class GlobalSearchApiController extends Controller
if (!empty($searchTerm)) { if (!empty($searchTerm)) {
$limitPerTable = 100; // Performance safeguard $limitPerTable = 100; // Performance safeguard
// 1. Search Bylaw Documents (Including deeper node_content search) // 1. Search Bylaw Documents (Including node_content subquery)
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->where(function ($q) use ($searchTerm) { ->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%") $q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', '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) { ->orWhereIn('id', function($subQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); $subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
}); });
})->take($limitPerTable)->get()->map(function($item) { })->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatBylaw($item); $formatted = $this->formatBylaw($item);
@@ -191,15 +194,17 @@ class GlobalSearchApiController extends Controller
if (!empty($searchTerm)) { if (!empty($searchTerm)) {
// 1. Bylaw Documents // 1. Bylaw Documents
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) $bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true) ->where('is_published', true)
->where(function ($q) use ($searchTerm) { ->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%") $q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', '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) { ->orWhereIn('id', function($subQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); $subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
}); });
}); });
$totalCount += $bylawQuery->count(); $totalCount += $bylawQuery->count();
@@ -299,10 +304,15 @@ class GlobalSearchApiController extends Controller
{ {
$isLocked = $this->isLockedForCurrentUserGeneric($item); $isLocked = $this->isLockedForCurrentUserGeneric($item);
// Fallback description to node_content if empty
$description = $item->description; $description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n"); // Direct Query Fallback: If description is empty, fetch node_content manually
if (empty($description)) {
$description = BylawNode::where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->pluck('node_content')
->filter()
->implode("\n\n");
} }
return [ return [