Refactor booking wash certificate handling logic
Replaced `wash_certificate_store` with `pdf_store` for handling wash certificate operations. Enhanced error handling with booking status checks (pending, completed, or cancelled) and added validation for wash certificate URL existence. This improves code clarity and ensures better consistency in managing wash certificates.
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use classes\pdf_store;
|
||||
use classes\response;
|
||||
use classes\wash_certificate_store;
|
||||
use objects\bookings_new_o;
|
||||
use objects\bookings_o;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
@@ -238,15 +239,34 @@ class bookingsRoute
|
||||
if (!$user->hasAccessToBooking($id)) {
|
||||
$response->error('You are not allowed to download this wash certificate', 400);
|
||||
}
|
||||
// Check if the booking is completed
|
||||
if (!(new bookings_new_o())->select($id)->exists()) {
|
||||
$response->error('Booking not found', 404);
|
||||
}
|
||||
$bookings_new = (new bookings_new_o())->select($id);
|
||||
$wash_certificate_status = $bookings_new->washCertificateStatus->value();
|
||||
switch ($wash_certificate_status) {
|
||||
case 'pending':
|
||||
$response->error('Wash certificate has not been issued yet', 400);
|
||||
case 'completed':
|
||||
// The wash certificate has been issued, so we can proceed
|
||||
break;
|
||||
case 'cancelled':
|
||||
$response->error('Wash certificate is cancelled', 400);
|
||||
default:
|
||||
$response->error('Wash certificate status is unknown', 500);
|
||||
}
|
||||
// Get the wash certificate object
|
||||
$wash_certificate_object = $bookings_new->washCertificateUrl->value();
|
||||
// Create the connection
|
||||
$wash_certificate_store = new wash_certificate_store();
|
||||
$pdf_storage = new pdf_store();
|
||||
// Check if the wash certificate exists.
|
||||
if (!$wash_certificate_store->washCertificateExists($id)) {
|
||||
$response->error('The wash certificate does not exist. id: ' . $id, 404);
|
||||
if (!$pdf_storage->doesObjectExist($wash_certificate_object)) {
|
||||
$response->error('Wash certificate not found, but it should exist', 500);
|
||||
}
|
||||
// Generate the download link
|
||||
$response->success(
|
||||
["link" => $wash_certificate_store->getWashCertificateDownload($id)]
|
||||
["link" => $pdf_storage->getPresignedUrl($wash_certificate_object)]
|
||||
);
|
||||
},
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user