Fixed major bugs

This commit is contained in:
m.razaghi
2026-07-09 17:06:07 +03:30
parent b7f139b935
commit 71dc7da8a4
4 changed files with 50 additions and 61 deletions

View File

@@ -13,9 +13,6 @@ class BylawApiController extends Controller
{ {
use BaseApiResponse; use BaseApiResponse;
/**
* Get all Bylaw Subject Categories
*/
public function categories() public function categories()
{ {
$categories = BylawSubjectCategory::all()->map(function ($category) { $categories = BylawSubjectCategory::all()->map(function ($category) {
@@ -28,9 +25,6 @@ class BylawApiController extends Controller
return $this->success($categories, 'Success', 'Bylaw Categories List'); return $this->success($categories, 'Success', 'Bylaw Categories List');
} }
/**
* Get all Bylaw Legal Authorities
*/
public function authorities() public function authorities()
{ {
$authorities = BylawLegalAuthority::all()->map(function ($authority) { $authorities = BylawLegalAuthority::all()->map(function ($authority) {
@@ -44,9 +38,6 @@ class BylawApiController extends Controller
return $this->success($authorities, 'Success', 'Bylaw Authorities List'); return $this->success($authorities, 'Success', 'Bylaw Authorities List');
} }
/**
* Get Paginated Bylaws (Filtered & Sorted by Approval Date)
*/
public function index(Request $request) public function index(Request $request)
{ {
$request->validate([ $request->validate([
@@ -93,9 +84,6 @@ class BylawApiController extends Controller
return $this->success($responseData, 'Success', 'Bylaws Fetched'); return $this->success($responseData, 'Success', 'Bylaws Fetched');
} }
/**
* Search Bylaws by Keyword
*/
public function search(Request $request) public function search(Request $request)
{ {
$request->validate([ $request->validate([
@@ -118,8 +106,6 @@ 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);
// Default search sort to newest first
$paginator = $query->orderBy('approval_date', 'desc')->paginate($perPage); $paginator = $query->orderBy('approval_date', 'desc')->paginate($perPage);
$items = collect($paginator->items())->map(function ($item) { $items = collect($paginator->items())->map(function ($item) {
@@ -137,9 +123,6 @@ class BylawApiController extends Controller
return $this->success($responseData, 'Success', 'Search Results Fetched'); return $this->success($responseData, 'Success', 'Search Results Fetched');
} }
/**
* Contextual Pager API
*/
public function pager($id) public function pager($id)
{ {
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) $current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
@@ -199,11 +182,15 @@ class BylawApiController extends Controller
return $this->success($results, 'Success', 'Pager Window Details'); return $this->success($results, 'Success', 'Pager Window Details');
} }
/** private function isLockedForCurrentUser($item): bool
* Helper to uniformly format the Bylaw Document payload {
*/ return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
private function formatBylaw($item) private function formatBylaw($item)
{ {
$isLocked = $this->isLockedForCurrentUser($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,
@@ -219,8 +206,9 @@ class BylawApiController extends Controller
'execution_date' => $item->execution_date, 'execution_date' => $item->execution_date,
'bylaw_type' => $item->bylaw_type, 'bylaw_type' => $item->bylaw_type,
'global_status' => $item->global_status, 'global_status' => $item->global_status,
'pdf_file_url' => $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' => $item->description, 'description' => $isLocked ? null : $item->description,
'is_locked' => $isLocked,
]; ];
} }
} }

View File

@@ -24,9 +24,6 @@ class GlobalSearchApiController extends Controller
{ {
use BaseApiResponse; use BaseApiResponse;
/**
* Massive Search across ALL data types
*/
public function search(Request $request) public function search(Request $request)
{ {
$request->validate([ $request->validate([
@@ -42,7 +39,7 @@ class GlobalSearchApiController extends Controller
$results = collect(); $results = collect();
if (!empty($searchTerm)) { if (!empty($searchTerm)) {
$limitPerTable = 100; // Performance safeguard $limitPerTable = 100;
// 1. Search Bylaw Documents // 1. Search Bylaw Documents
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
@@ -111,7 +108,6 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'art'; $formatted['type'] = 'art';
} else { } else {
$formatted = $this->formatStructure($item, $modelClass); $formatted = $this->formatStructure($item, $modelClass);
// Standardize the structural type names (laws, sections, etc.)
$formatted['type'] = ($table === 'section') ? 'sections' : $table; $formatted['type'] = ($table === 'section') ? 'sections' : $table;
} }
return $formatted; return $formatted;
@@ -121,12 +117,9 @@ class GlobalSearchApiController extends Controller
} }
} }
// --- Execute Manual Pagination on the Combined Collection ---
$total = $results->count(); $total = $results->count();
// Slice the array for the exact page requested
$paginatedItems = $results->forPage($page, $perPage)->values(); $paginatedItems = $results->forPage($page, $perPage)->values();
// Build a proper Laravel paginator object so the response metadata remains identical
$paginator = new LengthAwarePaginator( $paginator = new LengthAwarePaginator(
$paginatedItems, $paginatedItems,
$total, $total,
@@ -150,8 +143,15 @@ class GlobalSearchApiController extends Controller
* FORMATTING HELPERS * FORMATTING HELPERS
* ----------------------------------------------------------------- */ * ----------------------------------------------------------------- */
private function isLockedForCurrentUserGeneric($item): bool
{
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
private function formatBylaw($item) private function formatBylaw($item)
{ {
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,
@@ -159,42 +159,48 @@ class GlobalSearchApiController extends Controller
'approval_authority_name' => optional($item->approvalAuthority)->name, 'approval_authority_name' => optional($item->approvalAuthority)->name,
'approval_date' => $item->approval_date, 'approval_date' => $item->approval_date,
'promulgation_number' => $item->promulgation_number, 'promulgation_number' => $item->promulgation_number,
'description' => $item->description, 'description' => $isLocked ? null : $item->description,
'is_locked' => $isLocked,
]; ];
} }
private function formatJudicial($item) private function formatJudicial($item)
{ {
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->subject, // Map subject to title for universal UI parsing 'title' => $item->subject,
'ruling_number' => $item->ruling_number, 'ruling_number' => $item->ruling_number,
'ruling_date' => $item->ruling_date, 'ruling_date' => $item->ruling_date,
'text' => $item->full_text, 'text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority, 'issuing_authority' => $item->issuing_authority,
'category_name' => optional($item->category)->name, 'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
]; ];
} }
private function formatOpinion($item) private function formatOpinion($item)
{ {
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->subject, // Map subject to title for universal UI parsing 'title' => $item->subject,
'opinion_number' => $item->opinion_number, 'opinion_number' => $item->opinion_number,
'opinion_date' => $item->opinion_date, 'opinion_date' => $item->opinion_date,
'text' => $item->full_text, 'text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority, 'issuing_authority' => $item->issuing_authority,
'category_name' => optional($item->category)->name, 'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
]; ];
} }
private function formatArt($item, $searchTerm, $modelClass) private function formatArt($item, $searchTerm, $modelClass)
{ {
$law = Law::find($item->law_id); $law = Law::find($item->law_id);
$isLocked = $this->isLockedForCurrentUser($law); $isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
// Text snippet highlighting logic (from ArtController)
$text = $item->text ?? ''; $text = $item->text ?? '';
$searchQuoted = preg_quote($searchTerm, '/'); $searchQuoted = preg_quote($searchTerm, '/');
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) { if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
@@ -221,7 +227,7 @@ class GlobalSearchApiController extends Controller
private function formatStructure($item, $modelClass) private function formatStructure($item, $modelClass)
{ {
$law = Law::find($item?->law_id ?? $item->id); $law = Law::find($item?->law_id ?? $item->id);
$isLocked = $this->isLockedForCurrentUser($law); $isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
$payload = [ $payload = [
'id' => $item->id, 'id' => $item->id,
@@ -240,15 +246,6 @@ class GlobalSearchApiController extends Controller
return $payload; return $payload;
} }
/* -----------------------------------------------------------------
* UTILITY METHODS
* ----------------------------------------------------------------- */
private function isLockedForCurrentUser(?Law $law): bool
{
return ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
}
private function route($modelClass, $query) private function route($modelClass, $query)
{ {
if ((new $modelClass)->getTable() == 'art') { if ((new $modelClass)->getTable() == 'art') {

View File

@@ -59,9 +59,6 @@ class JudicialPrecedentApiController extends Controller
return $this->success($responseData, 'Success', 'Judicial Precedents Fetched'); return $this->success($responseData, 'Success', 'Judicial Precedents Fetched');
} }
/**
* Search Judicial Precedents by Keyword
*/
public function search(Request $request) public function search(Request $request)
{ {
$request->validate([ $request->validate([
@@ -141,20 +138,25 @@ class JudicialPrecedentApiController extends Controller
return $this->success($results, 'Success', 'Pager Window Details'); return $this->success($results, 'Success', 'Pager Window Details');
} }
/** private function isLockedForCurrentUser($item): bool
* Helper method to format payload uniformly {
*/ return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
private function formatItem($item) private function formatItem($item)
{ {
$isLocked = $this->isLockedForCurrentUser($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'ruling_number' => $item->ruling_number, 'ruling_number' => $item->ruling_number,
'ruling_date' => $item->ruling_date, 'ruling_date' => $item->ruling_date,
'subject' => $item->subject, 'subject' => $item->subject,
'full_text' => $item->full_text, 'full_text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority, 'issuing_authority' => $item->issuing_authority,
'category_id' => $item->category_id, 'category_id' => $item->category_id,
'category_name' => optional($item->category)->name, 'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
]; ];
} }
} }

View File

@@ -59,9 +59,6 @@ class LegalOpinionApiController extends Controller
return $this->success($responseData, 'Success', 'Legal Opinions Fetched'); return $this->success($responseData, 'Success', 'Legal Opinions Fetched');
} }
/**
* Search Legal Opinions by Keyword
*/
public function search(Request $request) public function search(Request $request)
{ {
$request->validate([ $request->validate([
@@ -153,20 +150,25 @@ class LegalOpinionApiController extends Controller
return $this->success($results, 'Success', 'Pager Window Details'); return $this->success($results, 'Success', 'Pager Window Details');
} }
/** private function isLockedForCurrentUser($item): bool
* Helper method to format payload uniformly {
*/ return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
private function formatItem($item) private function formatItem($item)
{ {
$isLocked = $this->isLockedForCurrentUser($item);
return [ return [
'id' => $item->id, 'id' => $item->id,
'opinion_number' => $item->opinion_number, 'opinion_number' => $item->opinion_number,
'opinion_date' => $item->opinion_date, 'opinion_date' => $item->opinion_date,
'subject' => $item->subject, 'subject' => $item->subject,
'full_text' => $item->full_text, 'full_text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority, 'issuing_authority' => $item->issuing_authority,
'category_id' => $item->category_id, 'category_id' => $item->category_id,
'category_name' => optional($item->category)->name, 'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
]; ];
} }
} }