FIX_Endorsement file

This commit is contained in:
sanjeev.p 2026-03-09 13:57:30 +05:30
parent bfc0a54d57
commit b1ce730687
2 changed files with 124 additions and 5 deletions

View File

@ -28,7 +28,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
//api's with token
$routes->group('api', ['filter' => ["jwtAuth:1,2,3,4,agent","appSignature"] ], function ($routes) {
$routes->group('api', ['filter' => ["jwtAuth:1,2,3,4,agent","appSignature"] ], function ($routes) {
//logout
$routes->get('auth/logout', 'StaffAuthController::logout');
@ -141,7 +141,9 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
$routes->post('endorsement/createEndorsement', 'EndorsementController::createEndorsement');
$routes->post('endorsement/updateEndorsement', 'EndorsementController::updateEndorsement');
$routes->get('endorsement/downloadEndorsementCompletionFile', 'EndorsementController::downloadEndorsementCompletionFile');
$routes->post('endorsement/uploadEndorsementFile', 'EndorsementController::uploadEndorsementFile');
$routes->get('endorsement/deleteEndorsement', 'EndorsementController::deleteEndorsement');
//dashboard
$routes->get('dashboard/managerDashboard', 'DashboardController::managerDashboard');
$routes->get('dashboard/handlerDashboard', 'DashboardController::handlerDashboard');

View File

@ -408,6 +408,30 @@ class EndorsementController extends ResourceController
}
}
public function deleteEndorsement()
{
try {
$id = $this->request->getGet('id');
// check if EndorsementModel exists
$file = $this->EndorsementModel->find((int)$id);
if (!$file) {
return $this->respond(['status' => 'failed','code' => 200, 'data' => 'Endorsement not found'], 200);
}
// update status
$this->EndorsementModel->update($id, ['is_active' => 0]);
return $this->respond([
'status' => 'success', 'code' => 200,'data' => "Endorsement Deleted"], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
// public function createEndorsement()
// {
@ -556,6 +580,7 @@ class EndorsementController extends ResourceController
{
try {
$id = $this->request->getGet('id');
$type = $this->request->getGet('type'); // 'completion' or 'original'
if (!$id) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 200);
@ -568,7 +593,23 @@ class EndorsementController extends ResourceController
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found'], 200);
}
$filePath = WRITEPATH . 'uploads/endorsement/' . $fileRecord['endorsement_file_name'];
// $filePath = WRITEPATH . 'uploads/endorsement/' . $fileRecord['endorsement_file_name'];
// ✅ If type is null/empty → original (default)
// ✅ If type = 'completion' → endorsement_pdf/ subfolder
if (!$type || $type === 'original') {
$fileName = $fileRecord['endorsement_file_name'] ?? null;
$subPath = '';
} else if ($type === 'completion') {
$fileName = $fileRecord['endorsement_completion_file'] ?? null;
$subPath = 'endorsement_pdf/';
}
if (!$fileName) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'No file uploaded yet'], 200);
}
$filePath = WRITEPATH . 'uploads/endorsement/' . $subPath . $fileName;
if (!file_exists($filePath)) {
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200);
@ -623,11 +664,87 @@ class EndorsementController extends ResourceController
}
}
public function uploadEndorsementFile()
{
try {
$data = $this->request->getPost();
// ✅ Validate required POST fields first
if (empty($data['id']) || empty($data['updated_by'])) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Missing required fields'], 400);
}
$uploadPath = WRITEPATH . 'uploads/endorsement/';
$allowedTypes = ['application/pdf'];
$pdfPath = $uploadPath . 'endorsement_pdf/';
$endorsementPdf = $this->request->getFile('endorsement_completion_file');
// ✅ Check file existence and validity BEFORE accessing its properties
if (!$endorsementPdf || !$endorsementPdf->isValid()) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'No valid file received'], 400);
}
// ✅ MIME validation only after confirming file exists
if (!in_array($endorsementPdf->getMimeType(), $allowedTypes)) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'Only PDF files are allowed'], 400);
}
// ✅ Fetch existing record to get old file name BEFORE uploading
$existingRecord = $this->EndorsementModel->find($data['id']);
$oldFileName = $existingRecord['endorsement_completion_file'] ?? null;
// ✅ Ensure upload directory exists
if (!is_dir($pdfPath)) {
mkdir($pdfPath, 0777, true);
}
// ✅ Upload new file
$pdfFileName = time() . '_' . $endorsementPdf->getRandomName();
$endorsementPdf->move($pdfPath, $pdfFileName);
// ✅ Guard: ensure file was actually saved on disk
if (!$pdfFileName || !file_exists($pdfPath . $pdfFileName)) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'File upload failed'], 400);
}
// ✅ Update DB with new file name
$updateData = [
'endorsement_completion_file' => $pdfFileName,
'updated_by' => $data['updated_by'],
];
$updated = $this->EndorsementModel->update($data['id'], $updateData);
// ✅ DB update failed → delete the newly uploaded file to avoid orphan files
if (!$updated) {
if (file_exists($pdfPath . $pdfFileName)) {
unlink($pdfPath . $pdfFileName);
}
log_message('error', 'DB update failed for endorsement ID: ' . $data['id'] . '. New file removed.');
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => 'DB update failed, file not saved'], 500);
}
// ✅ DB success → NOW safe to delete old file
if ($oldFileName) {
$oldFilePath = $pdfPath . $oldFileName;
if (file_exists($oldFilePath)) {
if (!unlink($oldFilePath)) {
log_message('warning', 'DB updated but failed to delete old file: ' . $oldFilePath);
} else {
log_message('info', 'Old file deleted after successful DB update: ' . $oldFilePath);
}
}
}
log_message('info', 'Endorsement PDF uploaded successfully. ID: ' . $data['id'] . ', File: ' . $pdfFileName);
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $data['id']], 200);
} catch (\Exception $e) {
log_message('error', 'uploadEndorsementFile error: ' . $e->getMessage());
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
}
}