diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 79a2a93..0417071 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -148,6 +148,8 @@ $routes->group('api', ['filter' => ["jwtAuth:1,2,3,4,agent","appSignature"] ], f //claims $routes->get('claim/claimStatusList', 'ClaimController::claimStatusList'); $routes->get('claim/ClaimList', 'ClaimController::ClaimList'); + $routes->get('claim/getClaimById', 'ClaimController::getClaimById'); + $routes->get('claim/downloadClaimFile', 'ClaimController::downloadClaimFile'); $routes->post('claim/createClaim', 'ClaimController::createClaim'); $routes->post('claim/updateClaim', 'ClaimController::updateClaim'); diff --git a/app/Controllers/ClaimController.php b/app/Controllers/ClaimController.php index 7e6297d..6c016a2 100644 --- a/app/Controllers/ClaimController.php +++ b/app/Controllers/ClaimController.php @@ -90,7 +90,7 @@ class ClaimController extends ResourceController } $claim = $this->formatClaimRow($claim); - $claim['files'] = $this->getClaimFiles((int) $claim['id']); + $claim['files'] = $this->decorateFilesWithUrls($this->getClaimFiles((int) $claim['id'])); return $this->respond(['status' => 'success', 'code' => 200, 'data' => $claim], 200); } @@ -129,7 +129,7 @@ class ClaimController extends ResourceController foreach ($data as $key => $row) { $data[$key] = $this->formatClaimRow($row); - $data[$key]['files'] = $filesByClaim[$row['id']] ?? []; + $data[$key]['files'] = $this->decorateFilesWithUrls($filesByClaim[$row['id']] ?? []); } return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data], 200); @@ -225,7 +225,7 @@ class ClaimController extends ResourceController 'data' => [ 'claim_id' => $claimId, 'uploaded_files' => $fileResult['saved'], - 'files' => $this->getClaimFiles($claimId), + 'files' => $this->decorateFilesWithUrls($this->getClaimFiles($claimId)), ], ], 200); } catch (\Exception $e) { @@ -320,7 +320,7 @@ class ClaimController extends ResourceController 'data' => [ 'claim_id' => (int) $claimId, 'uploaded_files' => $fileResult['saved'], - 'files' => $this->getClaimFiles((int) $claimId), + 'files' => $this->decorateFilesWithUrls($this->getClaimFiles((int) $claimId)), ], ], 200); } catch (\Exception $e) { @@ -328,6 +328,93 @@ class ClaimController extends ResourceController } } + /** + * Fetch a single claim by primary id with related lookups and file preview URLs. + * Used to populate the edit form when the user clicks the edit button on a row. + */ + public function getClaimById() + { + try { + $id = $this->request->getGet('id'); + + if (empty($id)) { + return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'id is required'], 400); + } + + $claim = $this->db->table('partner_claims pc') + ->select('pc.*, + i.name as insurer_name, + i.short_name as insurer_short_name, + pct.claim_type as claim_type_value, + pa.name as agent_name, + tcs.claim_status as claim_status_value, + pp.start_date as policy_start_date, + pp.end_date as policy_end_date, + v.vehicle_no as reg_no') + ->join('insurers i', 'i.id = pc.insurer_id', 'left') + ->join('partner_claim_type_master pct', 'pct.id = pc.claim_type', 'left') + ->join('partner_agent pa', 'pa.id = pc.agent_id', 'left') + ->join('ticket_claim_status tcs', 'tcs.id = pc.claim_status_id AND tcs.ticket_type = ' . self::CLAIM_TICKET_TYPE, 'left') + ->join('partner_policy pp', 'pp.id = pc.policy_id', 'left') + ->join('vehicle v', 'v.id = pp.vehicle_id', 'left') + ->where('pc.id', (int) $id) + ->where('pc.is_active', 1) + ->get() + ->getRowArray(); + + if (!$claim) { + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Claim not found'], 404); + } + + $claim = $this->formatClaimRow($claim); + $claim['files'] = $this->decorateFilesWithUrls($this->getClaimFiles((int) $claim['id'])); + + return $this->respond(['status' => 'success', 'code' => 200, 'data' => $claim], 200); + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + + /** + * Stream a single claim file inline (used as preview / download URL). + */ + public function downloadClaimFile() + { + try { + $fileId = $this->request->getGet('file_id'); + + if (empty($fileId)) { + return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'file_id is required'], 400); + } + + $file = $this->ClaimFilesModel + ->where('id', (int) $fileId) + ->where('is_active', 1) + ->first(); + + if (!$file) { + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 404); + } + + $relativePath = ltrim((string) ($file['file_path'] ?? ''), '/'); + $filePath = WRITEPATH . $relativePath; + + if (empty($relativePath) || !file_exists($filePath)) { + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 404); + } + + $mime = $file['file_mime_type'] ?? mime_content_type($filePath) ?? 'application/octet-stream'; + $downloadName = $file['file_name'] ?: basename($filePath); + + return $this->response + ->setHeader('Content-Type', $mime) + ->setHeader('Content-Disposition', 'inline; filename="' . $downloadName . '"') + ->setBody(file_get_contents($filePath)); + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + private function formatClaimRow(array $row): array { if (!empty($row['created_at'])) { @@ -364,6 +451,22 @@ class ClaimController extends ResourceController ->findAll(); } + /** + * Add preview_url and download_url to each claim file row. + */ + private function decorateFilesWithUrls(array $files): array + { + foreach ($files as $key => $file) { + $fileId = $file['id'] ?? null; + $url = $fileId ? base_url('api/claim/downloadClaimFile?file_id=' . $fileId) : null; + + $files[$key]['preview_url'] = $url; + $files[$key]['download_url'] = $url; + } + + return $files; + } + private function getClaimFilesGrouped(array $claimIds): array { $claimIds = array_values(array_filter(array_map('intval', $claimIds)));