Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 18:25:31 +03:30
parent 7f744beec4
commit 2474f268d1

View File

@@ -80,29 +80,8 @@ class BylawApiController extends Controller
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$paginator = $query->paginate($perPage);
// Fetch Node Content and Map perfectly by Document ID
$documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesByDoc = [];
if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes')
->whereIn('document_id', $documentIds)
->orderBy('sort_order', 'asc')
->select('document_id', 'node_content')
->get();
foreach ($nodes as $node) {
$docId = (int) $node->document_id;
$content = trim((string)$node->node_content);
if (!empty($content)) {
$nodesByDoc[$docId][] = $content;
}
}
}
$items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) {
$itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
$items = collect($paginator->items())->map(function ($item) {
return $this->formatBylaw($item);
});
$responseData = [
@@ -214,28 +193,8 @@ class BylawApiController extends Controller
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$paginator = $query->paginate($perPage);
$documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesByDoc = [];
if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes')
->whereIn('document_id', $documentIds)
->orderBy('sort_order', 'asc')
->select('document_id', 'node_content')
->get();
foreach ($nodes as $node) {
$docId = (int) $node->document_id;
$content = trim((string)$node->node_content);
if (!empty($content)) {
$nodesByDoc[$docId][] = $content;
}
}
}
$items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) {
$itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
$items = collect($paginator->items())->map(function ($item) {
return $this->formatBylaw($item);
});
$responseData = [
@@ -274,28 +233,8 @@ class BylawApiController extends Controller
$count = $query->count();
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
$documentIds = $items->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesByDoc = [];
if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes')
->whereIn('document_id', $documentIds)
->orderBy('sort_order', 'asc')
->select('document_id', 'node_content')
->get();
foreach ($nodes as $node) {
$docId = (int) $node->document_id;
$content = trim((string)$node->node_content);
if (!empty($content)) {
$nodesByDoc[$docId][] = $content;
}
}
}
$formattedItems = $items->map(function ($item) use ($nodesByDoc) {
$itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
$formattedItems = $items->map(function ($item) {
return $this->formatBylaw($item);
});
return $this->success([
@@ -356,71 +295,46 @@ class BylawApiController extends Controller
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
$documentIds = collect($itemsToFormat)->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesByDoc = [];
if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes')
->whereIn('document_id', $documentIds)
->orderBy('sort_order', 'asc')
->select('document_id', 'node_content')
->get();
foreach ($nodes as $node) {
$docId = (int) $node->document_id;
$content = trim((string)$node->node_content);
if (!empty($content)) {
$nodesByDoc[$docId][] = $content;
}
}
}
$results = array_values(array_map(function($item) use ($nodesByDoc) {
$itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
$results = array_values(array_map(function($item) {
return $this->formatBylaw($item);
}, $itemsToFormat));
return $this->success($results, 'Success', 'Pager Window Details');
}
private function isLockedForCurrentUser($item): bool
{
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
/**
* FULLY REBUILT: FORCES NODE_CONTENT INTO DESCRIPTION
* COMPLETELY REWRITTEN - BULLETPROOF DIRECT QUERY
*/
private function formatBylaw($item, $preloadedNodes = null)
private function formatBylaw($item)
{
$isLocked = $this->isLockedForCurrentUser($item);
// 1. Direct, undeniable raw query to the database for this specific document's nodes
$nodes = DB::table('bylaw_nodes')
->where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->get();
$nodeContentFullText = "";
// 1. Prioritize preloaded nodes (used in index, search, fast_search, pager)
if ($preloadedNodes !== null && is_array($preloadedNodes)) {
$nodeContentFullText = implode("\n\n", $preloadedNodes);
}
// 2. Fallback to DB query if not preloaded (used in single method)
else {
$nodes = DB::table('bylaw_nodes')
->where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->pluck('node_content')
->toArray();
$validContents = [];
foreach ($nodes as $content) {
if (!empty(trim((string)$content))) {
$validContents[] = trim((string)$content);
}
$extractedTexts = [];
foreach ($nodes as $node) {
$text = trim((string) $node->node_content);
if (!empty($text)) {
$extractedTexts[] = $text;
}
$nodeContentFullText = implode("\n\n", $validContents);
}
// 3. Absolute last resort (only if the DB literally has no nodes for this item)
if (empty(trim(strip_tags($nodeContentFullText)))) {
$nodeContentFullText = $item->description;
// 2. Combine all node_content into the final string
$nodeContentAsDescription = implode("\n\n", $extractedTexts);
// 3. Fallback ONLY if the database row is literally missing nodes
if (empty($nodeContentAsDescription)) {
$nodeContentAsDescription = $item->description;
}
// 4. Safe Lock Check (prevents "call to member function on null" if user isn't logged in)
$isLocked = false;
if (auth()->check() && auth()->user() && method_exists(auth()->user(), 'isSubscriber')) {
$isLocked = !auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
} else {
$isLocked = (bool) ($item->is_locked ?? false);
}
return [
@@ -441,7 +355,7 @@ class BylawApiController extends Controller
'bylaw_type' => $item->bylaw_type,
'global_status' => $item->global_status,
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
'description' => $isLocked ? null : $nodeContentFullText, // <--- NODE_CONTENT IS NOW 100% FORCED HERE
'description' => $isLocked ? null : $nodeContentAsDescription, // DIRECT NODE_CONTENT HERE
'is_locked' => $isLocked,
];
}