Add /vehicles/status route to retrieve vehicle status details

- Introduced a new route `/vehicles/status` to provide detailed vehicle status, including verification, booking, and last order information.
- Added `getLastOrderByPlate` method in `customer_vehicles_o` to retrieve the last order for a vehicle plate.
- Implemented validation for registration number and optional department parameter.
- Enhanced response with multiple status indicators like `verified`, `known`, `booked`, and `card`.
This commit is contained in:
Jeppe Bundgaard
2025-09-15 11:58:32 +02:00
parent c8006bf3bd
commit 5b00e47276
2 changed files with 107 additions and 0 deletions
@@ -4,7 +4,9 @@ namespace routes;
use classes\authentication;
use customers\economic_customer_mo;
use objects\bookings_o;
use objects\customer_vehicles_o;
use objects\departments_o;
use objects\logs_o;
use objects\orders_o;
use objects\products_o;
@@ -638,6 +640,81 @@ class vehiclesRoute
'list_users_with_vehicle_subscriptions' => 'List users with vehicle subscriptions',
]
);
$this->get('/vehicles/status', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('view_vehicle_status');
/** Define parameters */
// "reg" - The registration number to check the status of
self::requireParameters([
'reg',
]);
self::requireType((string)self::getParameter('reg'), self::type_string());
$reg = (string)self::getParameter('reg');
self::requireMinLength('reg', 1);
self::requireMaxLength('reg', 12);
// "department" - The department to check the status of (optional)
if (self::isParametersSet(['department'])) {
self::requireType((int)self::getParameter('department'), self::type_int());
$department = (int)self::getParameter('department');
self::requireMinValue($department, 1);
self::requireMaxValue($department, 999999);
// Check if the department exists
$department_o = new departments_o();
$department_o->select($department);
if (!$department_o->exists()) {
$response->error('Department not found', 404);
}
} else {
$department = null;
}
// Validate the registration number
$this->validateRegistrationNumber($reg, $response);
$customer_vehicles_o = new customer_vehicles_o();
/** Determine the statuses of the vehicle */
$isVerified = $customer_vehicles_o->countRowsWhere(
[
'reg' => $reg,
]
) > 0;
$isKnown = (new orders_o())->countRowsWhere(
[
'reg_1' => $reg,
]
) > 1; // We need to go above one, as the current order will also be counted
$isBooked = (new bookings_o())->countRowsWhere(
[
'regNrTraekker' => $reg,
'status' => 'pending',
...($department ? ['department' => $department] : []), // Filter by department if set
]
) > 0;
// Check if the customer is barred (only if the vehicle is verified)
$customer_number = $isVerified ? ($customer_vehicles_o->getFieldsWhere(
[
'reg' => $reg,
],
['customer_id']
)) : null;
$customer_number = $customer_number ? (int)$customer_number[0]['customer_id'] : null;
$cardPaymentRequired = ($customer_number && (new users_o())->isCustomerBarred((int)$customer_number));
$last_order = $customer_vehicles_o->getLastOrderByPlate($reg);
$response->success([
'department' => $department,
'reg' => $reg,
'verified' => $isVerified,
'known' => $isKnown,
'booked' => $isBooked,
'card' => $cardPaymentRequired,
'unknown' => !$customer_number && !$isKnown && !$isBooked,
'last_order_id' => $last_order?->id ?? null,
]);
},
[
'view_vehicle_status' => 'View vehicle status (Verified, Known, Unknown, Booked, Card payment required, etc.)',
]
);
}
private function getData(mixed $data, $response)