From 3b6949cb219088c2f84f84095a377166a587bfa9 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Tue, 22 Apr 2025 14:54:38 +0200 Subject: [PATCH] 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`. --- .../objects/department_notification_sms_o.php | 14 ++-- .../routes/departmentNotificationSmsRoute.php | 66 +++++++++++++++++-- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/services/nginx/app/objects/department_notification_sms_o.php b/services/nginx/app/objects/department_notification_sms_o.php index be4a6828..a518cd46 100644 --- a/services/nginx/app/objects/department_notification_sms_o.php +++ b/services/nginx/app/objects/department_notification_sms_o.php @@ -28,9 +28,9 @@ class department_notification_sms_o extends db public object_property $phone_country_code; /** * The phone number - * @var object_property $phone_number The phone number + * @var object_property $phone The phone number */ - public object_property $phone_number; + public object_property $phone; /** * The verified at date * @var object_property $verified_at The verified at date @@ -82,18 +82,18 @@ class department_notification_sms_o extends db $data_default = [ 'label' => '', 'phone_country_code' => 45, - 'phone_number' => 0, + 'phone' => 0, ]; // Merge the default values with the data $data = array_merge($data_default, $data); // Sanitize the input $data['label'] = (string)$db->escape_string($data['label']); $data['phone_country_code'] = (int)$data['phone_country_code']; - $data['phone_number'] = (int)$db->escape_string($data['phone_number']); + $data['phone'] = (int)$db->escape_string($data['phone']); // Add the object $tmp_id = self::add_object([ ...$data, - 'department' => $department, + 'department' => (int)$department, ]); if (!$tmp_id) { throw new Exception('The object was not created successfully.'); @@ -108,7 +108,7 @@ class department_notification_sms_o extends db $this->department = new object_property($this->table, $this->id, 'department', 'int', false); $this->label = new object_property($this->table, $this->id, 'label', 'string', false); $this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false); - $this->phone_number = new object_property($this->table, $this->id, 'phone_number', 'int', false); + $this->phone = new object_property($this->table, $this->id, 'phone', 'int', false); $this->verified_at = new object_property($this->table, $this->id, 'verified_at', 'datetime', false); $this->enabled = new object_property($this->table, $this->id, 'enabled', 'bool', false); $this->verification_code = new object_property($this->table, $this->id, 'verification_code', 'string', false); @@ -131,7 +131,7 @@ class department_notification_sms_o extends db 'department' => $this->department->value(), 'label' => $this->label->value(), 'phone_country_code' => $this->phone_country_code->value(), - 'phone_number' => $this->phone_number->value(), + 'phone' => $this->phone->value(), 'verified_at' => $this->verified_at->value(), 'enabled' => $this->enabled->value(), 'created_at' => $this->created_at->value(), diff --git a/services/nginx/app/routes/departmentNotificationSmsRoute.php b/services/nginx/app/routes/departmentNotificationSmsRoute.php index edeef7f7..eb57e725 100644 --- a/services/nginx/app/routes/departmentNotificationSmsRoute.php +++ b/services/nginx/app/routes/departmentNotificationSmsRoute.php @@ -30,11 +30,13 @@ class departmentNotificationSmsRoute $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_number' => (int)$tmp_object_array['phone_number'], + 'phone' => (int)$tmp_object_array['phone'], 'created_at' => (string)$tmp_object_array['created_at'], + 'enabled' => (boolean)$tmp_object_array['enabled'], ]; }, $department_notification_sms->forceRestrictFilters( @@ -66,15 +68,15 @@ class departmentNotificationSmsRoute 'department', 'label', 'phone_country_code', - 'phone_number', + '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_number'), self::type_int()); - self::requireMinValue((int)self::getParameter('phone_number'), 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')); @@ -84,7 +86,7 @@ class departmentNotificationSmsRoute [ 'label' => (string)self::getParameter('label'), 'phone_country_code' => (int)self::getParameter('phone_country_code'), - 'phone_number' => (int)self::getParameter('phone_number'), + 'phone' => (int)self::getParameter('phone'), ] ); (new logs_o())->add('department_notification_sms', 'global', 0, $user->id, 'DEPARTMENT_NOTIFICATION_SMS_ADD', 'Add department notification SMS'); @@ -99,6 +101,60 @@ class departmentNotificationSmsRoute ] ); + /** 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;