Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 17:33:31 +03:30
parent e041b85e74
commit 677e101181

View File

@@ -10,7 +10,6 @@ use App\Models\BylawNode;
use App\Models\BylawCodificationLink; use App\Models\BylawCodificationLink;
use App\Traits\BaseApiResponse; use App\Traits\BaseApiResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BylawApiController extends Controller class BylawApiController extends Controller
{ {
@@ -80,8 +79,12 @@ 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);
$items = collect($paginator->items())->map(function ($item) { // Pre-fetch ALL nodes for the current page in ONE query (Prevents Database crashing/emptying)
return $this->formatBylaw($item); $documentIds = collect($paginator->items())->pluck('id');
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
}); });
$responseData = [ $responseData = [
@@ -108,14 +111,16 @@ class BylawApiController extends Controller
], 404); ], 404);
} }
$formatted = $this->formatBylaw($bylaw); // Fetch nodes safely
if (!$formatted['is_locked']) {
$nodes = BylawNode::with('type') $nodes = BylawNode::with('type')
->where('document_id', $bylaw->id) ->where('document_id', $bylaw->id)
->orderBy('sort_order', 'asc') ->orderBy('sort_order', 'asc')
->get() ->get();
->map(function ($node) {
$formatted = $this->formatBylaw($bylaw, $nodes);
if (!$formatted['is_locked']) {
$formatted['nodes'] = $nodes->map(function ($node) {
return [ return [
'id' => $node->id, 'id' => $node->id,
'parent_id' => $node->parent_id, 'parent_id' => $node->parent_id,
@@ -128,9 +133,7 @@ class BylawApiController extends Controller
'valid_to' => $node->valid_to, 'valid_to' => $node->valid_to,
'node_status' => $node->node_status, 'node_status' => $node->node_status,
]; ];
}); })->toArray();
$formatted['nodes'] = $nodes->toArray();
$links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc') $links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc')
->where('source_entity_id', $bylaw->id) ->where('source_entity_id', $bylaw->id)
@@ -194,8 +197,12 @@ 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);
$items = collect($paginator->items())->map(function ($item) { // Pre-fetch ALL nodes for the current page in ONE query
return $this->formatBylaw($item); $documentIds = collect($paginator->items())->pluck('id');
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
}); });
$responseData = [ $responseData = [
@@ -232,12 +239,18 @@ class BylawApiController extends Controller
} }
$count = $query->count(); $count = $query->count();
$items = $query->orderBy('approval_date', 'desc')->take(5)->get()->map(function ($item) { $items = $query->orderBy('approval_date', 'desc')->take(5)->get();
return $this->formatBylaw($item);
// Pre-fetch ALL nodes for the current page in ONE query
$documentIds = $items->pluck('id');
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
$formattedItems = $items->map(function ($item) use ($allNodes) {
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
}); });
return $this->success([ return $this->success([
'items' => $items, 'items' => $formattedItems,
'count' => $count 'count' => $count
], 'Success', 'Fast Search Results'); ], 'Success', 'Fast Search Results');
} }
@@ -292,11 +305,15 @@ class BylawApiController extends Controller
->orderBy('id', 'asc') ->orderBy('id', 'asc')
->first(); ->first();
$results = array_values(array_filter([ $itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
$previousItem ? $this->formatBylaw($previousItem) : null,
$this->formatBylaw($current), // Pre-fetch ALL nodes
$nextItem ? $this->formatBylaw($nextItem) : null $documentIds = collect($itemsToFormat)->pluck('id');
])); $allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
$results = array_values(array_map(function($item) use ($allNodes) {
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
}, $itemsToFormat));
return $this->success($results, 'Success', 'Pager Window Details'); return $this->success($results, 'Success', 'Pager Window Details');
} }
@@ -306,36 +323,32 @@ class BylawApiController extends Controller
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false); return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
} }
private function formatBylaw($item) /**
* 100% Bulletproof formatting with aggressive HTML empty-checking
*/
private function formatBylaw($item, $preloadedNodes = null)
{ {
$isLocked = $this->isLockedForCurrentUser($item); $isLocked = $this->isLockedForCurrentUser($item);
$description = $item->description; $description = $item->description;
// HARDCORE EMPTY CHECK: Destroy all tags, non-breaking spaces, and newlines. // AGGRESSIVE HTML CLEANER: Destroys hidden HTML tags,  , and whitespaces
$cleanDesc = trim(str_replace([' ', '‌', "\r", "\n", "\t", ' '], '', strip_tags($description ?? ''))); $cleanDesc = trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($description ?? ''))));
// If the description is genuinely empty, pull from the nodes table! // If the description is genuinely empty, pull from the nodes!
if (empty($cleanDesc)) { if ($cleanDesc === '') {
$nodes = DB::table('bylaw_nodes')
->where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->get(['title', 'node_content']); // Fetch BOTH title and node_content
$descParts = []; // Uses nodes passed from the parent query, OR natively queries them if missing
foreach ($nodes as $node) { $nodes = $preloadedNodes !== null ? $preloadedNodes : BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get();
$nodeTitle = trim($node->title ?? '');
$nodeContent = trim($node->node_content ?? '');
if (!empty($nodeTitle)) { if ($nodes && $nodes->count() > 0) {
$descParts[] = $nodeTitle; // Extracts node_content, drops any that are pure empty space, and joins them
$description = $nodes->pluck('node_content')->filter(function ($content) {
return trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($content ?? '')))) !== '';
})->implode("\n\n");
} else {
$description = null;
} }
if (!empty($nodeContent)) {
$descParts[] = $nodeContent;
}
}
$description = implode("\n\n", $descParts);
} }
return [ return [