updated apis

This commit is contained in:
m.razaghi
2026-07-10 23:22:56 +03:30
parent d19d4b8667
commit 3b9fcd76df
2 changed files with 102 additions and 21 deletions

View File

@@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
use App\Models\BylawDocument;
use App\Models\BylawLegalAuthority;
use App\Models\BylawSubjectCategory;
use App\Models\BylawNode;
use App\Models\BylawCodificationLink;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
@@ -49,7 +51,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100',
]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true);
if ($request->filled('category_id')) {
@@ -92,6 +94,63 @@ class BylawApiController extends Controller
return $this->success($responseData, 'Success', 'Bylaws Fetched');
}
public function single($id)
{
$bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->find($id);
if (!$bylaw) {
return $this->failed([], [
'title' => 'خطا',
'message' => 'آیین‌نامه مورد نظر یافت نشد.'
], 404);
}
$formatted = $this->formatBylaw($bylaw);
if (!$formatted['is_locked']) {
$nodes = BylawNode::with('type')
->where('document_id', $bylaw->id)
->orderBy('sort_order', 'asc')
->get()
->map(function ($node) {
return [
'id' => $node->id,
'parent_id' => $node->parent_id,
'node_type' => optional($node->type)->name,
'node_identifier' => $node->node_identifier,
'title' => $node->title,
'node_content' => $node->node_content,
'sort_order' => $node->sort_order,
'valid_from' => $node->valid_from,
'valid_to' => $node->valid_to,
'node_status' => $node->node_status,
];
});
$formatted['nodes'] = $nodes->toArray();
$links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc')
->where('source_entity_id', $bylaw->id)
->get()
->map(function ($link) {
return [
'id' => $link->id,
'target_entity_type' => $link->target_entity_type,
'target_entity_id' => $link->target_entity_id,
'link_type' => $link->link_type,
'effective_date' => $link->effective_date,
'justification' => $link->justification,
];
});
$formatted['links'] = $links->toArray();
}
return $this->success($formatted, 'Success', 'Bylaw Document Details Fetched');
}
public function search(Request $request)
{
$request->validate([
@@ -101,7 +160,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100',
]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true);
if ($request->filled('search')) {
@@ -111,7 +170,10 @@ class BylawApiController extends Controller
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%");
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
}
@@ -147,7 +209,7 @@ class BylawApiController extends Controller
{
$searchTerm = $request->input('search');
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true);
if (!empty($searchTerm)) {
@@ -155,7 +217,10 @@ class BylawApiController extends Controller
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%");
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
}
@@ -172,7 +237,7 @@ class BylawApiController extends Controller
public function pager($id)
{
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->find($id);
@@ -183,7 +248,7 @@ class BylawApiController extends Controller
], 404);
}
$previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->where(function ($query) use ($current) {
if ($current->approval_date) {
@@ -200,7 +265,7 @@ class BylawApiController extends Controller
->orderBy('id', 'desc')
->first();
$nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
$nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->where(function ($query) use ($current) {
if ($current->approval_date) {
@@ -238,13 +303,21 @@ class BylawApiController extends Controller
{
$isLocked = $this->isLockedForCurrentUser($item);
// Map description: If description is null, combine all node_content into one full text!
$description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
}
return [
'id' => $item->id,
'title' => $item->title,
'category_id' => $item->category_id,
'category_name' => optional($item->category)->name,
'approval_authority_id' => $item->approval_authority_id,
'approval_authority_name' => optional($item->approvalAuthority)->name,
'approval_date' => $item->approval_date,
'promulgation_authority_id' => $item->promulgation_authority_id,
'promulgation_authority_name'=> optional($item->promulgationAuthority)->name,
'promulgation_date' => $item->promulgation_date,
'promulgation_number' => $item->promulgation_number,
@@ -254,7 +327,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 : $item->description,
'description' => $isLocked ? null : $description,
'is_locked' => $isLocked,
];
}