Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 18:21:02 +03:30
parent 7a987a33c9
commit 7f744beec4

View File

@@ -25,7 +25,7 @@ class BylawApiController extends Controller
]; ];
}); });
return $this->success($categories, '', 'Bylaw Categories List'); return $this->success($categories, 'Success', 'Bylaw Categories List');
} }
public function authorities() public function authorities()
@@ -38,7 +38,7 @@ class BylawApiController extends Controller
]; ];
}); });
return $this->success($authorities, '', 'Bylaw Authorities List'); return $this->success($authorities, 'Success', 'Bylaw Authorities List');
} }
public function index(Request $request) public function index(Request $request)
@@ -80,9 +80,9 @@ 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);
// واکشی ۱۰۰٪ قطعی گره‌ها با آرایه‌های بومی (Native PHP Arrays) // Fetch Node Content and Map perfectly by Document ID
$documentIds = collect($paginator->items())->pluck('id')->toArray(); $documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesDictionary = []; $nodesByDoc = [];
if (!empty($documentIds)) { if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes') $nodes = DB::table('bylaw_nodes')
@@ -92,18 +92,17 @@ class BylawApiController extends Controller
->get(); ->get();
foreach ($nodes as $node) { foreach ($nodes as $node) {
$docId = $node->document_id; $docId = (int) $node->document_id;
if (!isset($nodesDictionary[$docId])) { $content = trim((string)$node->node_content);
$nodesDictionary[$docId] = []; if (!empty($content)) {
} $nodesByDoc[$docId][] = $content;
if (!empty(trim((string)$node->node_content))) {
$nodesDictionary[$docId][] = trim((string)$node->node_content);
} }
} }
} }
$items = collect($paginator->items())->map(function ($item) use ($nodesDictionary) { $items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) {
return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); $itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
}); });
$responseData = [ $responseData = [
@@ -114,7 +113,7 @@ class BylawApiController extends Controller
'total' => $paginator->total(), 'total' => $paginator->total(),
]; ];
return $this->success($responseData, '', 'Bylaws Fetched'); return $this->success($responseData, 'Success', 'Bylaws Fetched');
} }
public function single($id) public function single($id)
@@ -170,7 +169,7 @@ class BylawApiController extends Controller
$formatted['links'] = $links->toArray(); $formatted['links'] = $links->toArray();
} }
return $this->success($formatted, '', 'Bylaw Document Details Fetched'); return $this->success($formatted, 'Success', 'Bylaw Document Details Fetched');
} }
public function search(Request $request) public function search(Request $request)
@@ -215,8 +214,8 @@ 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')->toArray(); $documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesDictionary = []; $nodesByDoc = [];
if (!empty($documentIds)) { if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes') $nodes = DB::table('bylaw_nodes')
@@ -226,18 +225,17 @@ class BylawApiController extends Controller
->get(); ->get();
foreach ($nodes as $node) { foreach ($nodes as $node) {
$docId = $node->document_id; $docId = (int) $node->document_id;
if (!isset($nodesDictionary[$docId])) { $content = trim((string)$node->node_content);
$nodesDictionary[$docId] = []; if (!empty($content)) {
} $nodesByDoc[$docId][] = $content;
if (!empty(trim((string)$node->node_content))) {
$nodesDictionary[$docId][] = trim((string)$node->node_content);
} }
} }
} }
$items = collect($paginator->items())->map(function ($item) use ($nodesDictionary) { $items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) {
return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); $itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
}); });
$responseData = [ $responseData = [
@@ -248,7 +246,7 @@ class BylawApiController extends Controller
'total' => $paginator->total(), 'total' => $paginator->total(),
]; ];
return $this->success($responseData, '', 'Search Results Fetched'); return $this->success($responseData, 'Success', 'Search Results Fetched');
} }
public function fast_search(Request $request) public function fast_search(Request $request)
@@ -276,8 +274,8 @@ 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')->toArray(); $documentIds = $items->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesDictionary = []; $nodesByDoc = [];
if (!empty($documentIds)) { if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes') $nodes = DB::table('bylaw_nodes')
@@ -287,24 +285,23 @@ class BylawApiController extends Controller
->get(); ->get();
foreach ($nodes as $node) { foreach ($nodes as $node) {
$docId = $node->document_id; $docId = (int) $node->document_id;
if (!isset($nodesDictionary[$docId])) { $content = trim((string)$node->node_content);
$nodesDictionary[$docId] = []; if (!empty($content)) {
} $nodesByDoc[$docId][] = $content;
if (!empty(trim((string)$node->node_content))) {
$nodesDictionary[$docId][] = trim((string)$node->node_content);
} }
} }
} }
$formattedItems = $items->map(function ($item) use ($nodesDictionary) { $formattedItems = $items->map(function ($item) use ($nodesByDoc) {
return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); $itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
}); });
return $this->success([ return $this->success([
'items' => $formattedItems, 'items' => $formattedItems,
'count' => $count 'count' => $count
], '', 'Fast Search Results'); ], 'Success', 'Fast Search Results');
} }
public function pager($id) public function pager($id)
@@ -359,8 +356,8 @@ class BylawApiController extends Controller
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]); $itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
$documentIds = collect($itemsToFormat)->pluck('id')->toArray(); $documentIds = collect($itemsToFormat)->pluck('id')->map(fn($id) => (int)$id)->toArray();
$nodesDictionary = []; $nodesByDoc = [];
if (!empty($documentIds)) { if (!empty($documentIds)) {
$nodes = DB::table('bylaw_nodes') $nodes = DB::table('bylaw_nodes')
@@ -370,21 +367,20 @@ class BylawApiController extends Controller
->get(); ->get();
foreach ($nodes as $node) { foreach ($nodes as $node) {
$docId = $node->document_id; $docId = (int) $node->document_id;
if (!isset($nodesDictionary[$docId])) { $content = trim((string)$node->node_content);
$nodesDictionary[$docId] = []; if (!empty($content)) {
} $nodesByDoc[$docId][] = $content;
if (!empty(trim((string)$node->node_content))) {
$nodesDictionary[$docId][] = trim((string)$node->node_content);
} }
} }
} }
$results = array_values(array_map(function($item) use ($nodesDictionary) { $results = array_values(array_map(function($item) use ($nodesByDoc) {
return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); $itemId = (int) $item->id;
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
}, $itemsToFormat)); }, $itemsToFormat));
return $this->success($results, '', 'Pager Window Details'); return $this->success($results, 'Success', 'Pager Window Details');
} }
private function isLockedForCurrentUser($item): bool private function isLockedForCurrentUser($item): bool
@@ -393,38 +389,38 @@ class BylawApiController extends Controller
} }
/** /**
* سیستم کاملاً ضد گلوله برای استخراج Description * FULLY REBUILT: FORCES NODE_CONTENT INTO DESCRIPTION
*/ */
private function formatBylaw($item, $preloadedNodes = null) private function formatBylaw($item, $preloadedNodes = null)
{ {
$isLocked = $this->isLockedForCurrentUser($item); $isLocked = $this->isLockedForCurrentUser($item);
// ۱. اولویت اول: متنی که ادمین در پنل وارد کرده است $nodeContentFullText = "";
$description = $item->description;
// ۲. اولویت دوم: اگر توضیحات ادمین خالی بود، متن قانون را از گره‌ها می‌گیریم // 1. Prioritize preloaded nodes (used in index, search, fast_search, pager)
if (empty(trim((string)$description))) { 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();
// اگر آرایه گره‌ها از قبل (در توابع index یا search) آماده شده بود: $validContents = [];
if ($preloadedNodes !== null && is_array($preloadedNodes)) { foreach ($nodes as $content) {
$description = implode("\n\n", $preloadedNodes); if (!empty(trim((string)$content))) {
$validContents[] = trim((string)$content);
// در غیر این صورت (مثلا در تابع single) مستقیماً از دیتابیس می‌گیریم
} 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);
}
} }
$description = implode("\n\n", $validContents);
} }
$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;
} }
return [ return [
@@ -445,7 +441,7 @@ class BylawApiController extends Controller
'bylaw_type' => $item->bylaw_type, 'bylaw_type' => $item->bylaw_type,
'global_status' => $item->global_status, 'global_status' => $item->global_status,
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null), 'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
'description' => $isLocked ? null : $description, 'description' => $isLocked ? null : $nodeContentFullText, // <--- NODE_CONTENT IS NOW 100% FORCED HERE
'is_locked' => $isLocked, 'is_locked' => $isLocked,
]; ];
} }