FIX_User_Incentives_api

This commit is contained in:
venba-Inspriron-3558 2025-10-31 12:17:21 +05:30
parent 313a72065c
commit 47fe825522
2 changed files with 44 additions and 12 deletions

View File

@ -779,32 +779,63 @@ class UserController extends AdminController
} }
} }
// public function downloadIncentivesFile($file_id)
// {
// try {
// $result = $this->partnerManagerIncentiveFileModel->where('id', $file_id)->first();
// if (!$result || empty($result['incentive_file_name'])) {
// throw new \Exception("File Name Not Found");
// }
// $incentive_file_name = $result['incentive_file_name'];
// $file_name = basename($incentive_file_name);
// $filePath = WRITEPATH . 'uploads/incentives/' . $file_name;
// if (file_exists($filePath)) {
// return $this->response->download($filePath, null);
// } else {
// throw new \Exception("File Not Found");
// }
// } catch (\Exception $e) {
// $this->myLogger->logme('error', $e->getMessage());
// // Show 404 error view
// $data['message'] = $e->getMessage();
// return view('errors/404', $data);
// }
// }
public function downloadIncentivesFile($file_id) public function downloadIncentivesFile($file_id)
{ {
try { try {
$result = $this->partnerManagerIncentiveFileModel->where('id', $file_id)->first(); $result = $this->partnerManagerIncentiveFileModel->where('id', $file_id)->first();
if (!$result || empty($result['incentive_file_name'])) { if (!$result || empty($result['incentive_file_name'])) {
throw new \Exception("File Name Not Found"); throw new \Exception("File Name Not Found for ID {$file_id}");
} }
$incentive_file_name = $result['incentive_file_name']; $incentive_file_name = $result['incentive_file_name'];
$file_name = basename($incentive_file_name); $file_name = basename($incentive_file_name);
$filePath = WRITEPATH . 'uploads/incentives/' . $file_name; $filePath = WRITEPATH . 'uploads/incentives/' . $file_name;
if (file_exists($filePath)) { if (!file_exists($filePath)) {
return $this->response->download($filePath, null); throw new \Exception("File Not Found: {$file_name}");
} else {
throw new \Exception("File Not Found");
} }
} catch (\Exception $e) { return $this->response->download($filePath, null);
} catch (\Throwable $e) {
// Catch all kinds of exceptions
$this->myLogger->logme('error', $e->getMessage()); $this->myLogger->logme('error', $e->getMessage());
// Show 404 error view // Return proper 404 for missing file, else 500 for unexpected error
$data['message'] = $e->getMessage(); if (str_contains($e->getMessage(), 'Not Found')) {
return view('errors/404', $data); return $this->response->setStatusCode(404)->setBody($e->getMessage());
}
return $this->response->setStatusCode(500)->setBody('Internal Server Error');
} }
} }

View File

@ -1550,9 +1550,10 @@ table.dataTable tbody td {
container.append(`<div class="no-files-message">No Files have been Uploaded</div>`); container.append(`<div class="no-files-message">No Files have been Uploaded</div>`);
} }
}, },
error: function(xhr) { error: function (xhr, status, errorThrown) {
container.html(`<div class="no-files-message">No Files have been Uploaded</div>`); container.html(`<div class="no-files-message">No Files have been Uploaded</div>`);
toastr.error('Server error occurred.', 'Error'); console.log("Error: " + xhr.status + " - " + errorThrown);
if (xhr.status !== 404) toastr.error(errorThrown || xhr.responseText || 'Something went wrong', 'Error');
} }
}); });
} }