Add departmentSelfserveConditionsRoute and departmentSelfserveConditionRulesRoute with CRUD operations and department-level access control; update OpenAPI spec accordingly

This commit is contained in:
Jeppe Bundgaard
2026-01-09 12:36:32 +01:00
parent b4eda9ed9c
commit 3aefa4a00a
3 changed files with 835 additions and 0 deletions
+381
View File
@@ -1434,6 +1434,328 @@ paths:
'404':
$ref: '#/components/responses/NotFound'
/department/selfserve/conditions:
get:
tags:
- Self-Serve
summary: List self-serve conditions
description: Retrieve a list of self-serve conditions for a department, lane, or product.
operationId: listSelfserveConditions
parameters:
- name: id
in: query
description: Filter by condition ID
schema:
type: integer
- name: department
in: query
description: Filter by department ID
schema:
type: integer
- name: lane
in: query
description: Filter by lane ID
schema:
type: integer
- name: product
in: query
description: Filter by product ID
schema:
type: integer
- $ref: '#/components/parameters/PageParam'
- $ref: '#/components/parameters/PerPageParam'
- $ref: '#/components/parameters/SearchParam'
- $ref: '#/components/parameters/FiltersParam'
responses:
'200':
description: Successfully retrieved conditions
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/DepartmentSelfserveCondition'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
post:
tags:
- Self-Serve
summary: Add self-serve condition
description: Add a new self-serve condition.
operationId: addSelfserveCondition
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- department
- lane
- product
- name
- description
properties:
department:
type: integer
lane:
type: integer
product:
type: integer
name:
type: string
description:
type: string
responses:
'200':
description: Successfully added condition
content:
application/json:
schema:
$ref: '#/components/schemas/DepartmentSelfserveCondition'
'400':
$ref: '#/components/responses/BadRequest'
'500':
$ref: '#/components/responses/InternalServerError'
put:
tags:
- Self-Serve
summary: Update self-serve condition
description: Update an existing self-serve condition.
operationId: updateSelfserveCondition
parameters:
- name: id
in: query
required: true
description: Condition ID
schema:
type: integer
requestBody:
content:
application/json:
schema:
type: object
properties:
department:
type: integer
lane:
type: integer
product:
type: integer
name:
type: string
description:
type: string
responses:
'200':
description: Successfully updated condition
content:
application/json:
schema:
$ref: '#/components/schemas/DepartmentSelfserveCondition'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/InternalServerError'
delete:
tags:
- Self-Serve
summary: Delete self-serve condition
description: Delete a self-serve condition.
operationId: deleteSelfserveCondition
parameters:
- name: id
in: query
required: true
description: Condition ID
schema:
type: integer
responses:
'200':
description: Successfully deleted condition
content:
application/json:
schema:
type: string
example: Condition deleted
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
/department/selfserve/condition/rules:
get:
tags:
- Self-Serve
summary: List self-serve condition rules
description: Retrieve a list of self-serve condition rules.
operationId: listSelfserveConditionRules
parameters:
- name: id
in: query
description: Filter by rule ID
schema:
type: integer
- name: condition_id
in: query
description: Filter by condition ID
schema:
type: integer
- name: type
in: query
description: Filter by rule type
schema:
type: string
- name: object_type
in: query
description: Filter by object type
schema:
type: string
- name: object_id
in: query
description: Filter by object ID
schema:
type: integer
- $ref: '#/components/parameters/PageParam'
- $ref: '#/components/parameters/PerPageParam'
- $ref: '#/components/parameters/SearchParam'
- $ref: '#/components/parameters/FiltersParam'
responses:
'200':
description: Successfully retrieved condition rules
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/DepartmentSelfserveConditionRule'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
post:
tags:
- Self-Serve
summary: Add self-serve condition rule
description: Add a new self-serve condition rule.
operationId: addSelfserveConditionRule
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- condition_id
- type
- object_type
- object_id
- name
- description
properties:
condition_id:
type: integer
type:
type: string
object_type:
type: string
object_id:
type: integer
name:
type: string
description:
type: string
responses:
'200':
description: Successfully added condition rule
content:
application/json:
schema:
$ref: '#/components/schemas/DepartmentSelfserveConditionRule'
'400':
$ref: '#/components/responses/BadRequest'
'500':
$ref: '#/components/responses/InternalServerError'
put:
tags:
- Self-Serve
summary: Update self-serve condition rule
description: Update an existing self-serve condition rule.
operationId: updateSelfserveConditionRule
parameters:
- name: id
in: query
required: true
description: Rule ID
schema:
type: integer
requestBody:
content:
application/json:
schema:
type: object
properties:
condition_id:
type: integer
type:
type: string
object_type:
type: string
object_id:
type: integer
name:
type: string
description:
type: string
responses:
'200':
description: Successfully updated condition rule
content:
application/json:
schema:
$ref: '#/components/schemas/DepartmentSelfserveConditionRule'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/InternalServerError'
delete:
tags:
- Self-Serve
summary: Delete self-serve condition rule
description: Delete a self-serve condition rule.
operationId: deleteSelfserveConditionRule
parameters:
- name: id
in: query
required: true
description: Rule ID
schema:
type: integer
responses:
'200':
description: Successfully deleted condition rule
content:
application/json:
schema:
type: string
example: Rule deleted
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
/department/selfserve/tasks:
get:
tags:
@@ -4719,6 +5041,65 @@ components:
type: string
format: date-time
DepartmentSelfserveCondition:
type: object
properties:
id:
type: integer
description: Condition ID
department:
type: integer
description: Department ID
lane:
type: integer
description: Lane ID
product:
type: integer
description: Product ID
name:
type: string
description: Condition name
description:
type: string
description: Condition description
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
DepartmentSelfserveConditionRule:
type: object
properties:
id:
type: integer
description: Rule ID
condition_id:
type: integer
description: Condition object ID
type:
type: string
description: Condition type (e.g., IS_TRUE, IS_FALSE)
object_type:
type: string
description: The object type to which the condition applies (e.g., question, task, etc.)
object_id:
type: integer
description: The object id to which the condition applies
name:
type: string
description: Condition name
description:
type: string
description: Condition description
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
OrderCreate:
type: object
required:
@@ -0,0 +1,244 @@
<?php
/**
* Route for department self-serve condition rules
*/
namespace routes;
use classes\authentication;
use classes\response;
use objects\department_selfserve_conditions_o;
use objects\department_selfserve_condition_rules_o;
use objects\logs_o;
use traits\route_t;
class departmentSelfserveConditionRulesRoute
{
use route_t;
public function run(): void
{
/**
* List department self-serve condition rules
*/
$this->get('/department/selfserve/condition/rules', function () {
global $response;
$this->requirePermission('list_department_selfserve_condition_rules');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('department_selfserve_condition_rules', 'global', 1, $user->id, 'LIST_RULES', 'User listed department self-serve condition rules');
$rules_o = new department_selfserve_condition_rules_o();
// If an ID is provided, return that specific rule
if (self::isParametersSet(['id'])) {
$rules_o->select((int)self::getParameter('id'));
if ($rules_o->exists()) {
// Check access via condition -> department
$condition_o = new department_selfserve_conditions_o();
$condition_o->select((int)$rules_o->condition_id->value());
$authorized_department_ids = $user->getGroup()->getDepartments();
if ($condition_o->exists() && !in_array((int)$condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
$response->success($rules_o->asArray());
} else {
$response->error('Rule not found', 404);
}
}
$filters = [];
if (self::isParametersSet(['condition_id'])) {
$filters['condition_id'] = (int)self::getParameter('condition_id');
// Verify access to this condition
$condition_o = new department_selfserve_conditions_o();
$condition_o->select($filters['condition_id']);
if ($condition_o->exists()) {
$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 (self::isParametersSet(['type'])) {
$filters['type'] = (string)self::getParameter('type');
}
if (self::isParametersSet(['object_type'])) {
$filters['object_type'] = (string)self::getParameter('object_type');
}
if (self::isParametersSet(['object_id'])) {
$filters['object_id'] = (int)self::getParameter('object_id');
}
$response->success(
$rules_o->setSearchableFields(['id', 'condition_id', 'type', 'object_type', 'object_id', 'name', 'description', 'deleted_at'])
->listObjectsWithPaginationIfSet(function ($rule) {
$r = new department_selfserve_condition_rules_o();
$r->select((int)$rule['id']);
return $r->asArray();
}, $rules_o->forceRestrictFilters($filters))
);
} else {
$response->error('Invalid session', 400);
}
}, [
'list_department_selfserve_condition_rules' => 'List all department self-serve condition rules'
]);
/**
* Add a department self-serve condition rule
*/
$this->post('/department/selfserve/condition/rules', function () {
global $response;
$this->requirePermission('add_department_selfserve_condition_rules');
$user = (new authentication())->get_user();
if ($user) {
$condition_id = (int)$response->getRequestParameter('condition_id');
$type = (string)$response->getRequestParameter('type');
$object_type = (string)$response->getRequestParameter('object_type');
$object_id = (int)$response->getRequestParameter('object_id');
$name = (string)$response->getRequestParameter('name');
$description = (string)$response->getRequestParameter('description');
if (!$condition_id || !$type || !$object_type || !$object_id || !$name || !$description) {
$response->error('Missing required fields', 400);
}
// Verify access to this condition
$condition_o = new department_selfserve_conditions_o();
$condition_o->select($condition_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);
}
try {
$rule_o = (new department_selfserve_condition_rules_o())->add(
$condition_id,
$type,
$object_type,
$object_id,
$name,
$description
);
(new logs_o())->add('department_selfserve_condition_rules', 'global', 1, $user->id, 'ADD_RULE', 'User added department self-serve condition rule ' . $rule_o->id);
$response->success($rule_o->asArray());
} catch (\Exception $e) {
$response->error($e->getMessage(), 500);
}
} else {
$response->error('Invalid session', 400);
}
}, [
'add_department_selfserve_condition_rules' => 'Add a department self-serve condition rule'
]);
/**
* Update a department self-serve condition rule
*/
$this->put('/department/selfserve/condition/rules', function () {
global $response;
$this->requirePermission('update_department_selfserve_condition_rules');
$user = (new authentication())->get_user();
if ($user) {
$id = (int)$response->getRequestParameter('id');
if (!$id) {
$response->error('Missing required fields', 400);
}
$rule_o = new department_selfserve_condition_rules_o();
$rule_o->select($id);
if (!$rule_o->exists()) {
$response->error('Rule not found', 404);
}
// Verify access via condition -> department
$condition_o = new department_selfserve_conditions_o();
$condition_o->select((int)$rule_o->condition_id->value());
$authorized_department_ids = $user->getGroup()->getDepartments();
if ($condition_o->exists() && !in_array((int)$condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
if ($response->isRequestParameterSet('condition_id')) {
$new_condition_id = (int)$response->getRequestParameter('condition_id');
$new_condition_o = new department_selfserve_conditions_o();
$new_condition_o->select($new_condition_id);
if (!$new_condition_o->exists()) {
$response->error('Target condition not found', 404);
}
if (!in_array((int)$new_condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to the target department', 403);
}
$rule_o->condition_id->update($new_condition_id);
}
if ($response->isRequestParameterSet('type')) {
$rule_o->type->update((string)$response->getRequestParameter('type'));
}
if ($response->isRequestParameterSet('object_type')) {
$rule_o->object_type->update((string)$response->getRequestParameter('object_type'));
}
if ($response->isRequestParameterSet('object_id')) {
$rule_o->object_id->update((int)$response->getRequestParameter('object_id'));
}
if ($response->isRequestParameterSet('name')) {
$rule_o->name->update((string)$response->getRequestParameter('name'));
}
if ($response->isRequestParameterSet('description')) {
$rule_o->description->update((string)$response->getRequestParameter('description'));
}
(new logs_o())->add('department_selfserve_condition_rules', 'global', 1, $user->id, 'UPDATE_RULE', 'User updated department self-serve condition rule ' . $id);
$response->success($rule_o->asArray());
} else {
$response->error('Invalid session', 400);
}
}, [
'update_department_selfserve_condition_rules' => 'Update a department self-serve condition rule'
]);
/**
* Delete a department self-serve condition rule
*/
$this->delete('/department/selfserve/condition/rules', function () {
global $response;
$this->requirePermission('delete_department_selfserve_condition_rules');
$user = (new authentication())->get_user();
if ($user) {
$id = (int)$response->getRequestParameter('id');
if (!$id) {
$response->error('Missing required fields', 400);
}
$rule_o = new department_selfserve_condition_rules_o();
$rule_o->select($id);
if (!$rule_o->exists()) {
$response->error('Rule not found', 404);
}
// Verify access via condition -> department
$condition_o = new department_selfserve_conditions_o();
$condition_o->select((int)$rule_o->condition_id->value());
$authorized_department_ids = $user->getGroup()->getDepartments();
if ($condition_o->exists() && !in_array((int)$condition_o->department->value(), $authorized_department_ids)) {
$response->error('You do not have access to this department', 403);
}
$rule_o->delete();
(new logs_o())->add('department_selfserve_condition_rules', 'global', 1, $user->id, 'DELETE_RULE', 'User deleted department self-serve condition rule ' . $id);
$response->success('Rule deleted');
} else {
$response->error('Invalid session', 400);
}
}, [
'delete_department_selfserve_condition_rules' => 'Delete a department self-serve condition rule'
]);
}
}
@@ -0,0 +1,210 @@
<?php
/**
* Route for department self-serve conditions
*/
namespace routes;
use classes\authentication;
use classes\response;
use objects\department_selfserve_conditions_o;
use objects\logs_o;
use traits\route_t;
class departmentSelfserveConditionsRoute
{
use route_t;
public function run(): void
{
/**
* List department self-serve conditions
*/
$this->get('/department/selfserve/conditions', function () {
global $response;
$this->requirePermission('list_department_selfserve_conditions');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('department_selfserve_conditions', 'global', 1, $user->id, 'LIST_CONDITIONS', 'User listed department self-serve conditions');
$conditions_o = new department_selfserve_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(['product'])) {
$filters['product'] = (int)self::getParameter('product');
}
$response->success(
$conditions_o->setSearchableFields(['id', 'department', 'lane', 'product', 'name', 'description', 'deleted_at'])
->listObjectsWithPaginationIfSet(function ($condition) {
$c = new department_selfserve_conditions_o();
$c->select((int)$condition['id']);
return $c->asArray();
}, $conditions_o->forceRestrictFilters($filters))
);
} else {
$response->error('Invalid session', 400);
}
}, [
'list_department_selfserve_conditions' => 'List all department self-serve conditions'
]);
/**
* Add a department self-serve condition
*/
$this->post('/department/selfserve/conditions', function () {
global $response;
$this->requirePermission('add_department_selfserve_conditions');
$user = (new authentication())->get_user();
if ($user) {
$department = (int)$response->getRequestParameter('department');
$lane = (int)$response->getRequestParameter('lane');
$product = (int)$response->getRequestParameter('product');
$name = (string)$response->getRequestParameter('name');
$description = (string)$response->getRequestParameter('description');
if (!$department || !$lane || !$product || !$name || !$description) {
$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_conditions_o())->add(
$department,
$lane,
$product,
$name,
$description
);
(new logs_o())->add('department_selfserve_conditions', 'global', 1, $user->id, 'ADD_CONDITION', 'User added department self-serve 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_conditions' => 'Add a department self-serve condition'
]);
/**
* Update a department self-serve condition
*/
$this->put('/department/selfserve/conditions', function () {
global $response;
$this->requirePermission('update_department_selfserve_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_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('product')) {
$condition_o->product->update((int)$response->getRequestParameter('product'));
}
if ($response->isRequestParameterSet('name')) {
$condition_o->name->update((string)$response->getRequestParameter('name'));
}
if ($response->isRequestParameterSet('description')) {
$condition_o->description->update((string)$response->getRequestParameter('description'));
}
(new logs_o())->add('department_selfserve_conditions', 'global', 1, $user->id, 'UPDATE_CONDITION', 'User updated department self-serve condition ' . $id);
$response->success($condition_o->asArray());
} else {
$response->error('Invalid session', 400);
}
}, [
'update_department_selfserve_conditions' => 'Update a department self-serve condition'
]);
/**
* Delete a department self-serve condition
*/
$this->delete('/department/selfserve/conditions', function () {
global $response;
$this->requirePermission('delete_department_selfserve_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_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_conditions', 'global', 1, $user->id, 'DELETE_CONDITION', 'User deleted department self-serve condition ' . $id);
$response->success('Condition deleted');
} else {
$response->error('Invalid session', 400);
}
}, [
'delete_department_selfserve_conditions' => 'Delete a department self-serve condition'
]);
}
}