Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 18:28:57 +03:30
parent 2474f268d1
commit 032ae6ebf8

View File

@@ -302,39 +302,39 @@ class BylawApiController extends Controller
return $this->success($results, 'Success', 'Pager Window Details');
}
private function isLockedForCurrentUser($item): bool
{
if (auth()->check() && auth()->user() && method_exists(auth()->user(), 'isSubscriber')) {
return !auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
return (bool) ($item->is_locked ?? false);
}
/**
* COMPLETELY REWRITTEN - BULLETPROOF DIRECT QUERY
* اجرای مستقیم SQL برای دور زدن باگ‌های فیلتر و مدل در لاراول
*/
private function formatBylaw($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();
$isLocked = $this->isLockedForCurrentUser($item);
// واکشی خام با دستور استاندارد SQL (کاملاً تضمینی)
$rawNodes = DB::select("SELECT node_content FROM bylaw_nodes WHERE document_id = ? ORDER BY sort_order ASC", [$item->id]);
$extractedTexts = [];
foreach ($nodes as $node) {
$text = trim((string) $node->node_content);
if (!empty($text)) {
foreach ($rawNodes as $node) {
// حذف فضاهای خالی اضافی بدون حذف محتوای اصلی
$text = trim($node->node_content);
if ($text !== '' && $text !== null) {
$extractedTexts[] = $text;
}
}
// 2. Combine all node_content into the final string
$nodeContentAsDescription = implode("\n\n", $extractedTexts);
// چسباندن تمام تکه متن‌ها به یکدیگر
$finalNodeContent = 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);
// بک‌آپ نهایی (اگر به طرز عجیبی واقعاً دیتابیس برای این آیدی هیچ پاسخی نداد)
if (empty($finalNodeContent)) {
$finalNodeContent = $item->description;
}
return [
@@ -355,7 +355,10 @@ 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 : $nodeContentAsDescription, // DIRECT NODE_CONTENT HERE
// نتیجه استخراج مستقیم SQL در این متغیر قرار گرفته است:
'description' => $isLocked ? null : $finalNodeContent,
'is_locked' => $isLocked,
];
}