Add department-based filtering to vehicle search

- Introduced optional `department` parameter in `vehiclesRoute` for filtering booked registrations.
- Validated `department` input to ensure existence and access rights.
- Updated `getBookedRegs` to support dynamic filtering with department-specific criteria.
This commit is contained in:
Jeppe Bundgaard
2025-11-24 13:23:44 +01:00
parent 67927f8ab8
commit 36d902d833
+20 -2
View File
@@ -734,13 +734,29 @@ class vehiclesRoute
$search = (string)self::getParameter('search');
self::requireMinLength('search', 1);
self::requireMaxLength('search', 12);
$department = null;
// If department is set, validate it
if (self::isParametersSet(['department'])) {
$department = (int)self::getParameter('department');
self::requireType($department, self::type_int());
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);
}
self::requireDepartmentAccess($department);
}
// Strip the search term of whitespace
$search = preg_replace('/\s+/', '', $search);
/** Get the different lists of registration numbers */
$options = ['limit' => 10];
$booked_filters = [...($department ? ['department' => $department] : [])];
$verified_regs = self::getVerifiedRegs($search, $options);
$known_regs = self::getKnownRegs($search, $options);
$booked_regs = self::getBookedRegs($search, $options);
$booked_regs = self::getBookedRegs($search, $options, $booked_filters);
$unknown_regs = array_filter(array_unique(array_merge([...self::getUnknownRegs($search, $options), $search]))); // Add the search term as the first item, to ensure it is included in the results
/**
* Prevent overlaps between the lists
@@ -930,7 +946,7 @@ class vehiclesRoute
}, $results);
}
private static function getBookedRegs(string $search, array $options = ['limit' => null]): array
private static function getBookedRegs(string $search, array $options = ['limit' => null], ?array $filters = []): array
{
$bookings_o = new order_bookings_o();
/**
@@ -957,6 +973,7 @@ class vehiclesRoute
[
'reg_1' => $search,
'order_id' => null,
...$filters
],
['reg_1'],
$options
@@ -965,6 +982,7 @@ class vehiclesRoute
[
'reg_2' => $search,
'order_id' => null,
...$filters
],
['reg_2'],
$options