Add userNotificationsRoute to manage user notification settings

- Introduced new endpoint to update user notification preferences (email, SMS, wash certificate notifications).
- Enhanced `users_o` with new properties: `sms_notifications_enabled`, `email_notifications_enabled`, and `wash_certificate_email`.
- Added methods to set notification preferences in `users_o`.
This commit is contained in:
Jeppe Bundgaard
2025-11-11 14:34:29 +01:00
parent 664ed6410d
commit eca3b79f26
2 changed files with 95 additions and 0 deletions
+27
View File
@@ -37,6 +37,9 @@ class users_o extends db
public object_property $phone_country_code;
public object_property $phone;
public object_property $email;
public object_property $sms_notifications_enabled;
public object_property $email_notifications_enabled;
public object_property $wash_certificate_email; // Optional
protected array $wash_subscription_transactions;
public function structure(): void
@@ -94,6 +97,9 @@ class users_o extends db
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
$this->keys = (new user_key_value_pairs_o())->setUser($this->id);
$this->price_overrides = (new user_price_overrides_o())->setUser($this->id);
$this->sms_notifications_enabled = new object_property($this->table, $this->id, 'sms_notifications_enabled', 'bool', false);
$this->email_notifications_enabled = new object_property($this->table, $this->id, 'email_notifications_enabled', 'bool', false);
$this->wash_certificate_email = new object_property($this->table, $this->id, 'wash_certificate_email', 'string', false);
$this->renderLanguagePack();
}
@@ -328,6 +334,11 @@ class users_o extends db
'number' => $phone,
],
'email' => $this->email->value(),
'notifications' => [
'sms_notifications_enabled' => (bool)$this->sms_notifications_enabled->value(),
'email_notifications_enabled' => (bool)$this->email_notifications_enabled->value(),
'wash_certificate_email' => $this->wash_certificate_email->value(),
],
'created_at' => $this->created_at->value(),
'updated_at' => $this->updated_at->value(),
];
@@ -1345,4 +1356,20 @@ class users_o extends db
$this->objectChanged();
}
public function setSMSNotificationsEnabled(bool $enabled): void
{
self::requireSelected();
// Set SMS notifications enabled/disabled
$this->sms_notifications_enabled->set($enabled ? 1 : 0);
$this->objectChanged();
}
public function setEmailNotificationsEnabled(bool $enabled): void
{
self::requireSelected();
// Set email notifications enabled/disabled
$this->email_notifications_enabled->set($enabled ? 1 : 0);
$this->objectChanged();
}
}
@@ -0,0 +1,68 @@
<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use traits\route_t;
class userNotificationsRoute
{
use route_t;
public function run(): void
{
$this->put('/account/notifications', function () {
// Require the user to be logged in
global $response;
self::requirePermission('user_notifications_update');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('user_notifications', 'global', 0, 0, 'USER_NOTIFICATIONS_UPDATE', 'User not logged in');
$response->error('Invalid session', 400);
}
/**
* Parameters
*/
$wash_certificate_email = self::isParametersSet(['wash_certificate_email']) ? (string)self::getParameter('wash_certificate_email') : null;
$sms_notifications_enabled = self::isParametersSet(['sms_notifications_enabled']) ? (bool)self::getParameter('sms_notifications_enabled') : null;
$email_notifications_enabled = self::isParametersSet(['email_notifications_enabled']) ? (bool)self::getParameter('email_notifications_enabled') : null;
/**
* Wash Certificate Email
*/
if ($wash_certificate_email !== null) {
self::requireMinLength('wash_certificate_email', 5);
self::requireMaxLength('wash_certificate_email', 255);
self::requireType($wash_certificate_email, self::type_string());
if (!filter_var($wash_certificate_email, FILTER_VALIDATE_EMAIL)) {
(new logs_o())->add('user_notifications', 'global', 0, $user->id, 'USER_NOTIFICATIONS_UPDATE', 'Invalid wash certificate email');
$response->error('Invalid wash certificate email', 400);
}
$user->wash_certificate_email->set((string)$wash_certificate_email);
}
/**
* SMS Notifications Enabled
*/
if ($sms_notifications_enabled !== null) {
self::requireType($sms_notifications_enabled, self::type_bool());
$user->sms_notifications_enabled->set($sms_notifications_enabled ? 1 : 0);
}
/**
* Email Notifications Enabled
*/
if ($email_notifications_enabled !== null) {
self::requireType($email_notifications_enabled, self::type_bool());
$user->email_notifications_enabled->set($email_notifications_enabled ? 1 : 0);
}
// Log the update
(new logs_o())->add('user_notifications', 'global', 0, $user->id, 'USER_NOTIFICATIONS_UPDATE', 'User notification settings updated');
// Return success
$response->success(['message' => 'User notification settings updated']);
},
[
'user_notifications_update' => 'Update user notification settings',
]
);
}
}