Add support for optional bayId in license plate scans

- Updated `plateScansRoute` to handle an optional `bay_id` parameter in plate scan requests.
- Extended `plate_scans_o` object to include `bay_id` property and related database operations.
- Enhanced `add` method to validate and process `bayId` if provided.
This commit is contained in:
Jeppe Bundgaard
2025-12-15 10:14:00 +01:00
parent 9c9df0f1f4
commit 5a71889b26
2 changed files with 27 additions and 4 deletions
+11 -2
View File
@@ -12,6 +12,7 @@ class plate_scans_o extends db
public object_property $plate_scanner_id;
public object_property $plate;
public object_property $bay_id;
public function structure(): void
{
@@ -40,14 +41,19 @@ class plate_scans_o extends db
{
$this->plate_scanner_id = new object_property($this->table, $this->id, 'plate_scanner_id', 'int');
$this->plate = new object_property($this->table, $this->id, 'plate', 'string');
$this->bay_id = new object_property($this->table, $this->id, 'bay_id', 'string');
}
public function add(int $plate_scanner_id, string $plate): void
public function add(int $plate_scanner_id, string $plate, string $bayId = null): void
{
global $db, $response;
try {
// Avoid SQL injection
$plate = $db->escape_string($plate);
// If the bayId is set, validate it
if ($bayId !== null) {
$bayId = $db->escape_string($bayId);
}
// Get the department id from the plate scanner id
$sql = "SELECT department_id FROM plate_scanners WHERE id = $plate_scanner_id";
$result = $db->query($sql);
@@ -59,9 +65,12 @@ class plate_scans_o extends db
// Get the id of the new record
$this->id = $db->insert_id();
// Set the values of the object properties
$this->getObjectProperties();
// Set the bayId if provided
if ($bayId !== null) {
$this->bay_id->set((string)$bayId);
}
} catch (\Exception $e) {
$response->error($e->getMessage());
}
+16 -2
View File
@@ -23,6 +23,20 @@ class plateScansRoute
$this->requirePlateScannerAuth();
// Get the plate scanner object
$plate_scanner = (new authentication())->get_plate_scanner();
/**
* Define the optional request body parameters
* - plate: The license plate to scan (string, required)
* - bayId: The wash bay id (int, optional) - XLVASK lanes only
*/
$bayId = null;
// Validate the bayId if set
$bayIdParameterKey = 'bay_id';
if (self::isParametersSet([$bayIdParameterKey])) {
$bayId = self::getParameter($bayIdParameterKey);
self::requireType($bayId, self::type_string());
self::requireMinLength($bayIdParameterKey, 1);
self::requireMaxLength($bayIdParameterKey, 255);
}
// Get the post data
$data = json_decode(file_get_contents('php://input'), true);
// Check if the required fields are set
@@ -30,11 +44,11 @@ class plateScansRoute
$response->error('Missing required body parameter plate', 400);
}
// Add the number plate scanner
(new plate_scans_o())->add($plate_scanner->id, $data['plate']);
(new plate_scans_o())->add($plate_scanner->id, $data['plate'], $bayId);
// Log the incident
(new logs_o())->add('numberplatescans', $plate_scanner->department_id->value(), 1, 0, 'ADD_NUMBER_PLATE_SCANS', 'Successfully added a number plate scan: ' . $data['plate']);
// Return a success message
$response->success(['message' => 'License plate scan recorded.', 'plate' => $data['plate'], 'scanner' => $plate_scanner->name->value()], 201);
$response->success(['message' => 'License plate scan recorded.', 'plate' => $data['plate'], 'scanner' => $plate_scanner->name->value(), ['bay_id' => $bayId]], 201);
});
$this->post('/numberplatescans/department', function () {