Files
api/services/nginx/app/routes/departmentNotificationSmsRoute.php
T
Jepp9350 3b6949cb21 Refactor phone number field and add update endpoint.
Renamed `phone_number` field to `phone` across the codebase for consistency and simplicity. Introduced a new endpoint to update department notification SMS, supporting edits to fields like `label`, `phone_country_code`, `phone`, and `enabled`.
2025-04-22 14:54:38 +02:00

187 lines
9.5 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\response;
use classes\router;
use objects\department_notification_sms_o;
use objects\logs_o;
use traits\route_t;
class departmentNotificationSmsRoute
{
use route_t;
public function run(): void
{
global /** @var response $response */
/** @var router $router */
$router, $response;
/** Department Notification SMS -> Get */
$this->get('/department/notification/sms', function () {
global $response;
self::requirePermission('department_notification_sms_get');
$user = (new authentication())->get_user();
if ($user) {
$department_notification_sms = new department_notification_sms_o();
$result = $department_notification_sms->listObjectsWithPaginationIfSet(
function ($tmp_object_array) {
return [
'id' => (int)$tmp_object_array['id'],
'department' => (int)$tmp_object_array['department'],
'label' => (string)$tmp_object_array['label'],
'phone_country_code' => (int)$tmp_object_array['phone_country_code'],
'phone' => (int)$tmp_object_array['phone'],
'created_at' => (string)$tmp_object_array['created_at'],
'enabled' => (boolean)$tmp_object_array['enabled'],
];
},
$department_notification_sms->forceRestrictFilters(
[
// This makes sure that the user can only see orders from the departments they explicitly have access to
'department' => $user->getGroup()->getDepartments(),
]
)
);
(new logs_o())->add('department_notification_sms', 'global', 0, $user->id, 'DEPARTMENT_NOTIFICATION_SMS_GET', 'Get department notification SMS');
$response->success($result);
} else {
(new logs_o())->add('department_notification_sms', 'global', 0, 0, 'DEPARTMENT_NOTIFICATION_SMS_GET', 'Get department notification SMS failed');
$response->error('Invalid session', 400);
}
},
[
'department_notification_sms_get' => 'List department notification SMS, in the departments the user has access to',
]
);
/** Department Notification SMS -> Add */
$this->post('/department/notification/sms', function () {
global $response;
self::requirePermission('department_notification_sms_add');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters([
'department',
'label',
'phone_country_code',
'phone',
]);
self::requireType(self::getParameter('department'), self::type_int());
self::requireMinValue((int)self::getParameter('department'), 1);
self::requireType(self::getParameter('label'), self::type_string());
self::requireType(self::getParameter('phone_country_code'), self::type_int());
self::requireMinValue((int)self::getParameter('phone_country_code'), 1);
self::requireType(self::getParameter('phone'), self::type_int());
self::requireMinValue((int)self::getParameter('phone'), 1);
self::requirePermission('department_notification_sms_add');
self::requireDepartmentAccess((int)self::getParameter('department'));
$department_notification_sms = new department_notification_sms_o();
$department_notification_sms->add(
(int)self::getParameter('department'),
[
'label' => (string)self::getParameter('label'),
'phone_country_code' => (int)self::getParameter('phone_country_code'),
'phone' => (int)self::getParameter('phone'),
]
);
(new logs_o())->add('department_notification_sms', 'global', 0, $user->id, 'DEPARTMENT_NOTIFICATION_SMS_ADD', 'Add department notification SMS');
$response->success('Department notification SMS added');
} else {
(new logs_o())->add('department_notification_sms', 'global', 0, 0, 'DEPARTMENT_NOTIFICATION_SMS_ADD', 'Add department notification SMS failed');
$response->error('Invalid session', 400);
}
},
[
'department_notification_sms_add' => 'Add a department notification SMS, in the departments the user has access to',
]
);
/** Department Notification SMS -> Update */
$this->put('/department/notification/sms', function () {
global $response;
self::requirePermission('department_notification_sms_update');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters([
'id',
]);
self::requireType(self::getParameter('id'), self::type_int());
self::requireMinValue((int)self::getParameter('id'), 1);
$data = [];
if (self::isParametersSet(['label'])) {
self::requireType(self::getParameter('label'), self::type_string());
$data['label'] = (string)self::getParameter('label');
}
if (self::isParametersSet(['phone_country_code'])) {
self::requireType(self::getParameter('phone_country_code'), self::type_int());
self::requireMinValue((int)self::getParameter('phone_country_code'), 1);
$data['phone_country_code'] = (int)self::getParameter('phone_country_code');
}
if (self::isParametersSet(['phone'])) {
self::requireType(self::getParameter('phone'), self::type_int());
self::requireMinValue((int)self::getParameter('phone'), 1);
$data['phone'] = (int)self::getParameter('phone');
}
if (self::isParametersSet(['enabled'])) {
self::requireType(self::getParameter('enabled'), self::type_bool());
$data['enabled'] = self::getParameter('enabled') ? 1 : 0;
}
self::requirePermission('department_notification_sms_update');
$department_notification_sms = new department_notification_sms_o();
$department_notification_sms->select((int)self::getParameter('id'));
self::requireDepartmentAccess((int)$department_notification_sms->department->value());
if (empty($data)) {
$response->error('No data to update', 400);
}
$department_notification_sms->update(
[
...$data
]
);
(new logs_o())->add('department_notification_sms', 'global', 0, $user->id, 'DEPARTMENT_NOTIFICATION_SMS_UPDATE', 'Update department notification SMS');
$response->success('Department notification SMS updated');
} else {
(new logs_o())->add('department_notification_sms', 'global', 0, 0, 'DEPARTMENT_NOTIFICATION_SMS_UPDATE', 'Update department notification SMS failed');
$response->error('Invalid session', 400);
}
},
[
'department_notification_sms_update' => 'Update a department notification SMS, in the departments the user has access to',
]
);
/** Department Notification SMS -> Delete */
$this->delete('/department/notification/sms', function () {
global $response;
self::requirePermission('department_notification_sms_delete');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters([
'id',
]);
self::requireType(self::getParameter('id'), self::type_int());
self::requireMinValue((int)self::getParameter('id'), 1);
self::requirePermission('department_notification_sms_delete');
$department_notification_sms = new department_notification_sms_o();
$department_notification_sms->select((int)self::getParameter('id'));
self::requireDepartmentAccess($department_notification_sms->department);
$department_notification_sms->delete();
(new logs_o())->add('department_notification_sms', 'global', 0, $user->id, 'DEPARTMENT_NOTIFICATION_SMS_DELETE', 'Delete department notification SMS');
$response->success('Department notification SMS deleted');
} else {
(new logs_o())->add('department_notification_sms', 'global', 0, 0, 'DEPARTMENT_NOTIFICATION_SMS_DELETE', 'Delete department notification SMS failed');
$response->error('Invalid session', 400);
}
},
[
'department_notification_sms_delete' => 'Delete a department notification SMS, in the departments the user has access to',
]
);
}
}