- Introduced tests for `SelfserveNonOwnedVehicleWashAccess` to validate customer-scoped conditions for non-owned vehicles. - Added `SelfservePropertyGatePermissionBypassTest` to ensure proper permission handling for lanes and departments. - Updated `department_selfserve_vehicle_conditions_o` and routes to prevent cross-customer answer persistence. - Enhanced `selfserve_wash_flow` with customer-scoped persisted answer logic and improved method parameters for vehicle eligibility and session synchronization. - Adjusted OpenAPI spec and unit tests to reflect new customer-scoping behavior in self-serve operations.
132 lines
5.1 KiB
PHP
132 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use classes\selfserve;
|
|
use Exception;
|
|
use traits\db_object_t;
|
|
|
|
class department_selfserve_vehicle_conditions_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $department; // The department id
|
|
public object_property $lane; // The lane id
|
|
public object_property $customer_id; // The customer id
|
|
public object_property $reg; // The vehicle registration number
|
|
public object_property $question; // The question id
|
|
public object_property $value; // The answer value
|
|
public object_property $created_at;
|
|
public object_property $updated_at;
|
|
public object_property $deleted_at;
|
|
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('department_selfserve_vehicle_conditions');
|
|
}
|
|
|
|
|
|
/**
|
|
* Add a department lane
|
|
* @param int $department The department id
|
|
* @param int $lane The lane id
|
|
* @param string $reg The vehicle registration number
|
|
* @param int $question The question id
|
|
* @param bool $value The answer value
|
|
* @param int|null $customer_id The customer id
|
|
* @return department_selfserve_vehicle_conditions_o
|
|
* @throws Exception If the object was not created successfully
|
|
*/
|
|
public function add(int $department, int $lane, string $reg, int $question, bool $value, ?int $customer_id = null): department_selfserve_vehicle_conditions_o
|
|
{
|
|
global /** @var db $db */
|
|
$db;
|
|
// Sanitize the input
|
|
$department = (int)$department;
|
|
$lane = (int)$lane;
|
|
$reg = $db->escape_string($reg);
|
|
// Standardize the registration number
|
|
$reg = selfserve::standardize_registration($reg);
|
|
$question = (int)$question;
|
|
$value = (bool)$value;
|
|
$customer_id = $customer_id !== null ? (int)$customer_id : null;
|
|
// Remove any existing entry for the same department, lane, customer, reg and question.
|
|
// Saved answers must not bleed across customers that temporarily wash the same plate.
|
|
$customer_filter = $customer_id === null ? 'customer_id IS NULL' : 'customer_id = ' . $customer_id;
|
|
$sql = "DELETE FROM $this->table WHERE department = $department AND lane = $lane AND $customer_filter AND reg = '$reg' AND question = $question";
|
|
$db->query($sql);
|
|
// Add the object
|
|
$tmp_id = self::add_object([
|
|
'department' => $department,
|
|
'lane' => $lane,
|
|
'reg' => $reg,
|
|
'question' => $question,
|
|
'value' => $value,
|
|
'customer_id' => $customer_id,
|
|
]);
|
|
$this->id = $tmp_id;
|
|
self::getObjectProperties();
|
|
self::objectChanged();
|
|
return $this;
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->department = new object_property($this->table, $this->id, 'department', 'int', false);
|
|
$this->lane = new object_property($this->table, $this->id, 'lane', 'int', false);
|
|
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', false);
|
|
$this->reg = new object_property($this->table, $this->id, 'reg', 'string', false);
|
|
$this->question = new object_property($this->table, $this->id, 'question', 'int', false);
|
|
$this->value = new object_property($this->table, $this->id, 'value', 'bool', false);
|
|
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
|
|
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
|
|
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
//TODO: Add cache invalidation
|
|
}
|
|
|
|
public function asArray(): array
|
|
{
|
|
return [
|
|
'id' => (int)$this->id,
|
|
'department' => (int)$this->department->value(),
|
|
'lane' => (int)$this->lane->value(),
|
|
'customer_id' => is_null($this->customer_id->value()) ? null : (int)$this->customer_id->value(),
|
|
'reg' => (string)$this->reg->value(),
|
|
'question' => (int)$this->question->value(),
|
|
'value' => (bool)$this->value->value(),
|
|
'created_at' => (string)$this->created_at->value(),
|
|
'updated_at' => (string)$this->updated_at->value(),
|
|
'deleted_at' => is_null($this->deleted_at->value()) ? null : (string)$this->deleted_at->value(),
|
|
];
|
|
}
|
|
|
|
public function getAnswerMapForVehicle(int $departmentId, int $laneId, string $reg, ?int $customerId = null): array
|
|
{
|
|
if ($customerId === null || $customerId <= 0) {
|
|
return [];
|
|
}
|
|
|
|
$rows = self::getFieldsWhere([
|
|
'department' => $departmentId,
|
|
'lane' => $laneId,
|
|
'customer_id' => $customerId,
|
|
'reg' => selfserve::standardize_registration($reg),
|
|
'deleted_at' => null,
|
|
], ['question', 'value']);
|
|
|
|
$answers = [];
|
|
foreach ($rows as $row) {
|
|
$answers[(int)$row['question']] = (bool)$row['value'];
|
|
}
|
|
|
|
return $answers;
|
|
}
|
|
}
|