Add departmentSelfserveVehicleConditionsRoute with CRUD operations and department-level access control; update department_selfserve_vehicle_conditions_o to support customer_id property.

This commit is contained in:
Jeppe Bundgaard
2026-01-12 11:20:51 +01:00
parent d8f102b764
commit 92c5a03791
2 changed files with 224 additions and 3 deletions
@@ -36,10 +36,11 @@ class department_selfserve_vehicle_conditions_o extends db
* @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): department_selfserve_vehicle_conditions_o
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;
@@ -51,6 +52,7 @@ class department_selfserve_vehicle_conditions_o extends db
$reg = selfserve::standardize_registration($reg);
$question = (int)$question;
$value = (bool)$value;
$customer_id = $customer_id !== null ? (int)$customer_id : null;
// Add the object
$tmp_id = self::add_object([
'department' => $department,
@@ -58,6 +60,7 @@ class department_selfserve_vehicle_conditions_o extends db
'reg' => $reg,
'question' => $question,
'value' => $value,
'customer_id' => $customer_id,
]);
$this->id = $tmp_id;
self::getObjectProperties();
@@ -89,13 +92,13 @@ class department_selfserve_vehicle_conditions_o extends db
'id' => (int)$this->id,
'department' => (int)$this->department->value(),
'lane' => (int)$this->lane->value(),
'customer_id' => (int)$this->customer_id->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' => (string)$this->deleted_at->value(),
'deleted_at' => is_null($this->deleted_at->value()) ? null : (string)$this->deleted_at->value(),
];
}
}
@@ -0,0 +1,218 @@
<?php
/**
* Route for department self-serve vehicle conditions
*/
namespace routes;
use classes\authentication;
use classes\response;
use classes\selfserve;
use objects\department_selfserve_vehicle_conditions_o;
use objects\logs_o;
use traits\route_t;
class departmentSelfserveVehicleConditionsRoute
{
use route_t;
public function run(): void
{
/**
* List department self-serve vehicle conditions
*/
$this->get('/department/selfserve/vehicle/conditions', function () {
global $response;
$this->requirePermission('list_department_selfserve_vehicle_conditions');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('department_selfserve_vehicle_conditions', 'global', 1, $user->id, 'LIST_VEHICLE_CONDITIONS', 'User listed department self-serve vehicle conditions');
$conditions_o = new department_selfserve_vehicle_conditions_o();
$authorized_department_ids = $user->getGroup()->getDepartments();
// If an ID is provided, return that specific condition
if (self::isParametersSet(['id'])) {
$conditions_o->select((int)self::getParameter('id'));
if ($conditions_o->exists()) {
if (!in_array((int)$conditions_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
$response->success($conditions_o->asArray());
} else {
$response->error('Condition not found', 404);
}
}
$filters = [];
if (self::isParametersSet(['department'])) {
$requested_department = (int)self::getParameter('department');
if (!in_array($requested_department, $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
$filters['department'] = $requested_department;
} else {
$filters['department'] = $authorized_department_ids;
}
if (self::isParametersSet(['lane'])) {
$filters['lane'] = (int)self::getParameter('lane');
}
if (self::isParametersSet(['reg'])) {
$filters['reg'] = (string)self::getParameter('reg');
}
if (self::isParametersSet(['question'])) {
$filters['question'] = (int)self::getParameter('question');
}
if (self::isParametersSet(['customer_id'])) {
$filters['customer_id'] = (int)self::getParameter('customer_id');
}
$response->success(
$conditions_o->setSearchableFields(['id', 'department', 'lane', 'customer_id', 'reg', 'question', 'value', 'created_at', 'updated_at', 'deleted_at'])
->listObjectsWithPaginationIfSet(function ($condition) {
$c = new department_selfserve_vehicle_conditions_o();
$c->select((int)$condition['id']);
return $c->asArray();
}, $conditions_o->forceRestrictFilters($filters))
);
} else {
$response->error('Invalid session', 400);
}
}, [
'list_department_selfserve_vehicle_conditions' => 'List all department self-serve vehicle conditions'
]);
/**
* Add a department self-serve vehicle condition
*/
$this->post('/department/selfserve/vehicle/conditions', function () {
global $response;
$this->requirePermission('add_department_selfserve_vehicle_conditions');
$user = (new authentication())->get_user();
if ($user) {
$department = (int)$response->getRequestParameter('department');
$lane = (int)$response->getRequestParameter('lane');
$reg = (string)$response->getRequestParameter('reg');
$question = (int)$response->getRequestParameter('question');
$value = (bool)$response->getRequestParameter('value');
$customer_id = $response->isRequestParameterSet('customer_id') ? (int)$response->getRequestParameter('customer_id') : null;
if (!$department || !$lane || !$reg || !$question) {
$response->error('Missing required fields', 400);
}
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!in_array($department, $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
try {
$condition_o = new department_selfserve_vehicle_conditions_o();
$condition_o->add($department, $lane, $reg, $question, $value, $customer_id);
(new logs_o())->add('department_selfserve_vehicle_conditions', 'global', 1, $user->id, 'ADD_VEHICLE_CONDITION', 'User added department self-serve vehicle condition ' . $condition_o->id);
$response->success($condition_o->asArray());
} catch (\Exception $e) {
$response->error($e->getMessage(), 500);
}
} else {
$response->error('Invalid session', 400);
}
}, [
'add_department_selfserve_vehicle_conditions' => 'Add a department self-serve vehicle condition'
]);
/**
* Update a department self-serve vehicle condition
*/
$this->put('/department/selfserve/vehicle/conditions', function () {
global $response;
$this->requirePermission('update_department_selfserve_vehicle_conditions');
$user = (new authentication())->get_user();
if ($user) {
$id = (int)$response->getRequestParameter('id');
if (!$id) {
$response->error('Missing required fields', 400);
}
$condition_o = new department_selfserve_vehicle_conditions_o();
$condition_o->select($id);
if (!$condition_o->exists()) {
$response->error('Condition not found', 404);
}
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!in_array((int)$condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
if ($response->isRequestParameterSet('department')) {
$new_department = (int)$response->getRequestParameter('department');
if (!in_array($new_department, $authorized_department_ids)) {
$response->error('You do not have access to the target department', 403);
}
$condition_o->department->update($new_department);
}
if ($response->isRequestParameterSet('lane')) {
$condition_o->lane->update((int)$response->getRequestParameter('lane'));
}
if ($response->isRequestParameterSet('reg')) {
$condition_o->reg->update(selfserve::standardize_registration((string)$response->getRequestParameter('reg')));
}
if ($response->isRequestParameterSet('question')) {
$condition_o->question->update((int)$response->getRequestParameter('question'));
}
if ($response->isRequestParameterSet('value')) {
$condition_o->value->update((bool)$response->getRequestParameter('value'));
}
if ($response->isRequestParameterSet('customer_id')) {
$condition_o->customer_id->update((int)$response->getRequestParameter('customer_id'));
}
(new logs_o())->add('department_selfserve_vehicle_conditions', 'global', 1, $user->id, 'UPDATE_VEHICLE_CONDITION', 'User updated department self-serve vehicle condition ' . $id);
$response->success($condition_o->asArray());
} else {
$response->error('Invalid session', 400);
}
}, [
'update_department_selfserve_vehicle_conditions' => 'Update a department self-serve vehicle condition'
]);
/**
* Delete a department self-serve vehicle condition
*/
$this->delete('/department/selfserve/vehicle/conditions', function () {
global $response;
$this->requirePermission('delete_department_selfserve_vehicle_conditions');
$user = (new authentication())->get_user();
if ($user) {
$id = (int)$response->getRequestParameter('id');
if (!$id) {
$response->error('Missing required fields', 400);
}
$condition_o = new department_selfserve_vehicle_conditions_o();
$condition_o->select($id);
if (!$condition_o->exists()) {
$response->error('Condition not found', 404);
}
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!in_array((int)$condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
$condition_o->delete();
(new logs_o())->add('department_selfserve_vehicle_conditions', 'global', 1, $user->id, 'DELETE_VEHICLE_CONDITION', 'User deleted department self-serve vehicle condition ' . $id);
$response->success('Condition deleted');
} else {
$response->error('Invalid session', 400);
}
}, [
'delete_department_selfserve_vehicle_conditions' => 'Delete a department self-serve vehicle condition'
]);
}
}