Fixed api issue
This commit is contained in:
@@ -80,16 +80,20 @@ class BylawApiController extends Controller
|
|||||||
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
||||||
$paginator = $query->paginate($perPage);
|
$paginator = $query->paginate($perPage);
|
||||||
|
|
||||||
// Pre-fetch ALL nodes directly via DB facade for speed and reliability
|
// BULLETPROOF NODE FETCHING
|
||||||
$documentIds = collect($paginator->items())->pluck('id');
|
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
||||||
|
$allNodes = collect();
|
||||||
|
if (!empty($documentIds)) {
|
||||||
$allNodes = DB::table('bylaw_nodes')
|
$allNodes = DB::table('bylaw_nodes')
|
||||||
->whereIn('document_id', $documentIds)
|
->whereIn('document_id', $documentIds)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
->get()
|
->get();
|
||||||
->groupBy('document_id');
|
}
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
// Safely filter nodes belonging ONLY to this specific document
|
||||||
|
$itemNodes = $allNodes->where('document_id', $item->id);
|
||||||
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -116,8 +120,12 @@ class BylawApiController extends Controller
|
|||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch nodes using DB facade for the format function, but Eloquent for the nodes array
|
// Fetch nodes using raw DB
|
||||||
$rawNodes = DB::table('bylaw_nodes')->where('document_id', $bylaw->id)->orderBy('sort_order', 'asc')->get();
|
$rawNodes = DB::table('bylaw_nodes')
|
||||||
|
->where('document_id', $bylaw->id)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
$formatted = $this->formatBylaw($bylaw, $rawNodes);
|
$formatted = $this->formatBylaw($bylaw, $rawNodes);
|
||||||
|
|
||||||
if (!$formatted['is_locked']) {
|
if (!$formatted['is_locked']) {
|
||||||
@@ -203,15 +211,19 @@ class BylawApiController extends Controller
|
|||||||
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
||||||
$paginator = $query->paginate($perPage);
|
$paginator = $query->paginate($perPage);
|
||||||
|
|
||||||
$documentIds = collect($paginator->items())->pluck('id');
|
// BULLETPROOF NODE FETCHING
|
||||||
|
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
||||||
|
$allNodes = collect();
|
||||||
|
if (!empty($documentIds)) {
|
||||||
$allNodes = DB::table('bylaw_nodes')
|
$allNodes = DB::table('bylaw_nodes')
|
||||||
->whereIn('document_id', $documentIds)
|
->whereIn('document_id', $documentIds)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
->get()
|
->get();
|
||||||
->groupBy('document_id');
|
}
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
$itemNodes = $allNodes->where('document_id', $item->id);
|
||||||
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -250,15 +262,19 @@ class BylawApiController extends Controller
|
|||||||
$count = $query->count();
|
$count = $query->count();
|
||||||
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
|
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
|
||||||
|
|
||||||
$documentIds = $items->pluck('id');
|
// BULLETPROOF NODE FETCHING
|
||||||
|
$documentIds = $items->pluck('id')->toArray();
|
||||||
|
$allNodes = collect();
|
||||||
|
if (!empty($documentIds)) {
|
||||||
$allNodes = DB::table('bylaw_nodes')
|
$allNodes = DB::table('bylaw_nodes')
|
||||||
->whereIn('document_id', $documentIds)
|
->whereIn('document_id', $documentIds)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
->get()
|
->get();
|
||||||
->groupBy('document_id');
|
}
|
||||||
|
|
||||||
$formattedItems = $items->map(function ($item) use ($allNodes) {
|
$formattedItems = $items->map(function ($item) use ($allNodes) {
|
||||||
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
$itemNodes = $allNodes->where('document_id', $item->id);
|
||||||
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
@@ -319,15 +335,18 @@ class BylawApiController extends Controller
|
|||||||
|
|
||||||
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
|
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
|
||||||
|
|
||||||
$documentIds = collect($itemsToFormat)->pluck('id');
|
$documentIds = collect($itemsToFormat)->pluck('id')->toArray();
|
||||||
|
$allNodes = collect();
|
||||||
|
if (!empty($documentIds)) {
|
||||||
$allNodes = DB::table('bylaw_nodes')
|
$allNodes = DB::table('bylaw_nodes')
|
||||||
->whereIn('document_id', $documentIds)
|
->whereIn('document_id', $documentIds)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
->get()
|
->get();
|
||||||
->groupBy('document_id');
|
}
|
||||||
|
|
||||||
$results = array_values(array_map(function($item) use ($allNodes) {
|
$results = array_values(array_map(function($item) use ($allNodes) {
|
||||||
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
$itemNodes = $allNodes->where('document_id', $item->id);
|
||||||
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
}, $itemsToFormat));
|
}, $itemsToFormat));
|
||||||
|
|
||||||
return $this->success($results, 'Success', 'Pager Window Details');
|
return $this->success($results, 'Success', 'Pager Window Details');
|
||||||
@@ -341,30 +360,34 @@ class BylawApiController extends Controller
|
|||||||
/**
|
/**
|
||||||
* BULLETPROOF DESCRIPTION GENERATOR
|
* BULLETPROOF DESCRIPTION GENERATOR
|
||||||
*/
|
*/
|
||||||
private function formatBylaw($item, $preloadedNodes = null)
|
private function formatBylaw($item, $itemNodes = null)
|
||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUser($item);
|
$isLocked = $this->isLockedForCurrentUser($item);
|
||||||
|
$description = $item->description;
|
||||||
|
|
||||||
// 1. We ALWAYS try to build the description from the `node_content` FIRST.
|
// Check if description is practically empty (less than 5 chars = garbage HTML)
|
||||||
$nodes = $preloadedNodes !== null
|
$cleanDesc = trim(str_replace([' ', '‌', "\r", "\n", "\t", ' '], '', strip_tags($description ?? '')));
|
||||||
? $preloadedNodes
|
|
||||||
: DB::table('bylaw_nodes')->where('document_id', $item->id)->orderBy('sort_order', 'asc')->get();
|
if (strlen($cleanDesc) < 5) {
|
||||||
|
|
||||||
|
// Absolute Safety Net: If somehow $itemNodes is still null, FORCE a direct query for this specific ID.
|
||||||
|
if ($itemNodes === null) {
|
||||||
|
$itemNodes = DB::table('bylaw_nodes')
|
||||||
|
->where('document_id', $item->id)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
$nodeContents = [];
|
$nodeContents = [];
|
||||||
foreach ($nodes as $node) {
|
foreach ($itemNodes as $node) {
|
||||||
// We specifically ask for the node_content column here
|
// EXPLICITLY GET node_content
|
||||||
$content = trim(strip_tags($node->node_content ?? ''));
|
$content = trim(strip_tags($node->node_content ?? ''));
|
||||||
if (!empty($content)) {
|
if (!empty($content)) {
|
||||||
$nodeContents[] = $content;
|
$nodeContents[] = $content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stitch them all together
|
|
||||||
$description = implode("\n\n", $nodeContents);
|
$description = implode("\n\n", $nodeContents);
|
||||||
|
|
||||||
// 2. If node_content is COMPLETELY EMPTY across all nodes, ONLY THEN do we fallback to the old description
|
|
||||||
if (empty(trim(strip_tags($description)))) {
|
|
||||||
$description = $item->description;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
Reference in New Issue
Block a user