Files
api/services/nginx/app/routes/departmentsRoute.php
T
Jepp9350 c240bbd94f Refactor database object trait and improve data handling
Added `delete`, `restore`, and other utility methods to enhance CRUD operations, including support for soft deletes. Introduced `objectChanged` hooks across objects for better cache or event handling, ensuring scalability and maintainability. Refactored and standardized object property handling while restructuring related methods.
2025-02-12 09:23:35 +01:00

132 lines
6.1 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\departments_o;
use objects\logs_o;
use traits\route_t;
class departmentsRoute
{
use route_t;
public function run(): void
{
$this->get('/departments', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_departments');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENTS', 'Successfully listed departments');
// Return the list of departments
$response->success(
(new departments_o())
->setSearchableFields([
// The fields that can be searched. This would otherwise make it possible to get secret information from the database, simply by searching for it and getting the result count back
'id',
'name',
'description',
'economic_department_id'
])
->listObjectsWithPaginationIfSet(
function ($department) use ($user) {
$tmp_department = [
'id' => (int)$department['id'],
'name' => (string)$department['name'],
'description' => $department['description'],
'economic_department_id' => (int)$department['economic_department_id'],
'created_at' => (string)$department['created_at'],
'updated_at' => (string)$department['updated_at'],
'dimension' => (int)$department['dimension']
];
// If the user has the permission to view the slack webhook, add it to the response
if ($user->hasPermission('view_slack_webhook')) {
$tmp_department['slack_webhook'] = $department['slack_webhook'];
}
return $tmp_department;
}
)
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
$this->post('/departments', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('add_department');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the post data
$data = json_decode(file_get_contents('php://input'), true);
// Check if the required fields are set
if (!isset($data['name'])) {
$response->error('Name is required', 400);
}
if (!isset($data['description'])) {
$response->error('Description is required', 400);
}
// Add the department
(new departments_o())->create($data['name'], $data['description']);
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'ADD_DEPARTMENT', 'Successfully added a department ' . $data['name']);
// Return a success message
$response->success(['message' => 'Department added successfully']);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'ADD_DEPARTMENT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
$this->put('/departments', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('edit_department');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the post data
$data = json_decode(file_get_contents('php://input'), true);
// Check if the required fields are set
if (!isset($data['id'])) {
$response->error('ID is required', 400);
}
if (!isset($data['name'])) {
$response->error('Name is required', 400);
}
if (!isset($data['description'])) {
$response->error('Description is required', 400);
}
if (!isset($data['economic_department_id'])) {
$response->error('Economic department ID is required', 400);
}
// Update the department
(new departments_o())->edit($data['id'], $data['name'], $data['description'], (int)$data['economic_department_id']);
// Log the incident
(new logs_o())->add('departments', $data['id'], 1, $user->id, 'EDIT_DEPARTMENT', 'Successfully updated a department ' . $data['name']);
// Return a success message
$response->success(['message' => 'Department updated successfully']);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'EDIT_DEPARTMENT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}