Merge pull request 'feat: user notification preference toggle (web + Android)' (#80) from feat/version-update-notification into master
Reviewed-on: #80
This commit was merged in pull request #80.
This commit is contained in:
@@ -49,7 +49,7 @@ Repo-specific guidance for OpenCode sessions. Verified against code.
|
|||||||
|
|
||||||
## Database
|
## Database
|
||||||
- Migrations run manually: `mysql servermanager < database/migrations/NNN_name.sql`
|
- Migrations run manually: `mysql servermanager < database/migrations/NNN_name.sql`
|
||||||
- 12 migrations: `001_initial_schema`, `002_server_access`, `003_teams`, `004_soft_deletes`, `005_app_config_and_notifications`, `006_agent_tokens`, `007_server_permission_checks`, `008_notification_reads`, `009_remove_notification_obsolete_columns`, `010_user_fcm_tokens`, `011_cleanup_unused_tables`, `012_server_thresholds`
|
- 13 migrations: `001_initial_schema`, `002_server_access`, `003_teams`, `004_soft_deletes`, `005_app_config_and_notifications`, `006_agent_tokens`, `007_server_permission_checks`, `008_notification_reads`, `009_remove_notification_obsolete_columns`, `010_user_fcm_tokens`, `011_cleanup_unused_tables`, `012_server_thresholds`, `013_user_notification_prefs`
|
||||||
- Users table has `role` ENUM: `super_admin`, `admin`, `operator`
|
- Users table has `role` ENUM: `super_admin`, `admin`, `operator`
|
||||||
- `api_token` column on users for Bearer token auth
|
- `api_token` column on users for Bearer token auth
|
||||||
- `server_permissions` table stores required SSH permissions per server (FK to `servers.id` CASCADE)
|
- `server_permissions` table stores required SSH permissions per server (FK to `servers.id` CASCADE)
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ android {
|
|||||||
applicationId = "com.devlab.app"
|
applicationId = "com.devlab.app"
|
||||||
minSdk = 26
|
minSdk = 26
|
||||||
targetSdk = 37
|
targetSdk = 37
|
||||||
versionCode = 15
|
versionCode = 16
|
||||||
versionName = "1.8.3"
|
versionName = "1.8.4"
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ data class ProfileUpdateRequest(
|
|||||||
val email: String,
|
val email: String,
|
||||||
val current_password: String? = null,
|
val current_password: String? = null,
|
||||||
val new_password: String? = null,
|
val new_password: String? = null,
|
||||||
val confirm_password: String? = null
|
val confirm_password: String? = null,
|
||||||
|
val notifications_enabled: Boolean? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@@ -51,5 +52,6 @@ data class UserProfile(
|
|||||||
val username: String = "",
|
val username: String = "",
|
||||||
val email: String = "",
|
val email: String = "",
|
||||||
val role: String = "",
|
val role: String = "",
|
||||||
val created_at: String? = null
|
val created_at: String? = null,
|
||||||
|
val notifications_enabled: Boolean = true
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,14 +24,16 @@ class ProfileRepository {
|
|||||||
email: String,
|
email: String,
|
||||||
currentPassword: String?,
|
currentPassword: String?,
|
||||||
newPassword: String?,
|
newPassword: String?,
|
||||||
confirmPassword: String?
|
confirmPassword: String?,
|
||||||
|
notificationsEnabled: Boolean = true
|
||||||
): Result<String> {
|
): Result<String> {
|
||||||
return try {
|
return try {
|
||||||
val request = ProfileUpdateRequest(
|
val request = ProfileUpdateRequest(
|
||||||
email = email,
|
email = email,
|
||||||
current_password = currentPassword?.takeIf { it.isNotBlank() },
|
current_password = currentPassword?.takeIf { it.isNotBlank() },
|
||||||
new_password = newPassword?.takeIf { it.isNotBlank() },
|
new_password = newPassword?.takeIf { it.isNotBlank() },
|
||||||
confirm_password = confirmPassword?.takeIf { it.isNotBlank() }
|
confirm_password = confirmPassword?.takeIf { it.isNotBlank() },
|
||||||
|
notifications_enabled = notificationsEnabled
|
||||||
)
|
)
|
||||||
val response = api.updateProfile(request)
|
val response = api.updateProfile(request)
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
|
|||||||
@@ -249,6 +249,49 @@ fun ProfileScreen(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Card(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
colors = CardDefaults.cardColors(
|
||||||
|
containerColor = MaterialTheme.colorScheme.surface
|
||||||
|
),
|
||||||
|
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.padding(16.dp)) {
|
||||||
|
Text(
|
||||||
|
text = "Notifications",
|
||||||
|
style = MaterialTheme.typography.titleMedium,
|
||||||
|
fontWeight = FontWeight.SemiBold
|
||||||
|
)
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(12.dp))
|
||||||
|
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.weight(1f)) {
|
||||||
|
Text(
|
||||||
|
text = "Push Notifications",
|
||||||
|
style = MaterialTheme.typography.bodyLarge
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
text = "Receive alerts for threshold breaches and app updates",
|
||||||
|
style = MaterialTheme.typography.bodySmall,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Switch(
|
||||||
|
checked = state.notificationsEnabled,
|
||||||
|
onCheckedChange = viewModel::updateNotificationsEnabled,
|
||||||
|
enabled = !state.isSaving
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (state.error != null) {
|
if (state.error != null) {
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
Card(
|
Card(
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ data class ProfileUiState(
|
|||||||
val successMessage: String? = null,
|
val successMessage: String? = null,
|
||||||
val emailError: String? = null,
|
val emailError: String? = null,
|
||||||
val newPasswordError: String? = null,
|
val newPasswordError: String? = null,
|
||||||
val confirmPasswordError: String? = null
|
val confirmPasswordError: String? = null,
|
||||||
|
val notificationsEnabled: Boolean = true
|
||||||
)
|
)
|
||||||
|
|
||||||
class ProfileViewModel : ViewModel() {
|
class ProfileViewModel : ViewModel() {
|
||||||
@@ -44,7 +45,8 @@ class ProfileViewModel : ViewModel() {
|
|||||||
isLoading = false,
|
isLoading = false,
|
||||||
username = profile.username,
|
username = profile.username,
|
||||||
email = profile.email,
|
email = profile.email,
|
||||||
role = profile.role
|
role = profile.role,
|
||||||
|
notificationsEnabled = profile.notifications_enabled
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onFailure = { e ->
|
onFailure = { e ->
|
||||||
@@ -61,6 +63,10 @@ class ProfileViewModel : ViewModel() {
|
|||||||
_uiState.value = _uiState.value.copy(email = value, error = null, successMessage = null, emailError = null)
|
_uiState.value = _uiState.value.copy(email = value, error = null, successMessage = null, emailError = null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateNotificationsEnabled(value: Boolean) {
|
||||||
|
_uiState.value = _uiState.value.copy(notificationsEnabled = value)
|
||||||
|
}
|
||||||
|
|
||||||
fun updateCurrentPassword(value: String) {
|
fun updateCurrentPassword(value: String) {
|
||||||
_uiState.value = _uiState.value.copy(currentPassword = value, error = null, successMessage = null)
|
_uiState.value = _uiState.value.copy(currentPassword = value, error = null, successMessage = null)
|
||||||
}
|
}
|
||||||
@@ -103,7 +109,8 @@ class ProfileViewModel : ViewModel() {
|
|||||||
state.email,
|
state.email,
|
||||||
state.currentPassword.takeIf { it.isNotBlank() },
|
state.currentPassword.takeIf { it.isNotBlank() },
|
||||||
state.newPassword.takeIf { it.isNotBlank() },
|
state.newPassword.takeIf { it.isNotBlank() },
|
||||||
state.confirmPassword.takeIf { it.isNotBlank() }
|
state.confirmPassword.takeIf { it.isNotBlank() },
|
||||||
|
state.notificationsEnabled
|
||||||
)
|
)
|
||||||
result.fold(
|
result.fold(
|
||||||
onSuccess = { message ->
|
onSuccess = { message ->
|
||||||
|
|||||||
2
database/migrations/013_user_notification_prefs.sql
Normal file
2
database/migrations/013_user_notification_prefs.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE `users`
|
||||||
|
ADD COLUMN `notifications_enabled` TINYINT(1) NOT NULL DEFAULT 1 AFTER `status`;
|
||||||
Binary file not shown.
@@ -452,6 +452,15 @@ class ApiController
|
|||||||
|
|
||||||
if (!empty($breaches)) {
|
if (!empty($breaches)) {
|
||||||
$userIds = $serverModel->getAccessibleUserIds((int) $server['id']);
|
$userIds = $serverModel->getAccessibleUserIds((int) $server['id']);
|
||||||
|
|
||||||
|
$db = \ServerManager\Core\Database::getInstance();
|
||||||
|
$placeholders = implode(',', array_fill(0, count($userIds), '?'));
|
||||||
|
$enabled = $db->fetchCol(
|
||||||
|
"SELECT id FROM users WHERE id IN ({$placeholders}) AND notifications_enabled = 1",
|
||||||
|
$userIds
|
||||||
|
);
|
||||||
|
$userIds = $enabled;
|
||||||
|
|
||||||
$notificationModel = new Notification();
|
$notificationModel = new Notification();
|
||||||
$fcm = new FCMService();
|
$fcm = new FCMService();
|
||||||
foreach ($breaches as $b) {
|
foreach ($breaches as $b) {
|
||||||
@@ -572,6 +581,7 @@ class ApiController
|
|||||||
'email' => $user['email'],
|
'email' => $user['email'],
|
||||||
'role' => $user['role'],
|
'role' => $user['role'],
|
||||||
'created_at' => $user['created_at'],
|
'created_at' => $user['created_at'],
|
||||||
|
'notifications_enabled' => (bool) ($user['notifications_enabled'] ?? true),
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -597,6 +607,10 @@ class ApiController
|
|||||||
|
|
||||||
$updateData = ['email' => $input['email']];
|
$updateData = ['email' => $input['email']];
|
||||||
|
|
||||||
|
if (isset($input['notifications_enabled'])) {
|
||||||
|
$updateData['notifications_enabled'] = $input['notifications_enabled'] ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($input['new_password'])) {
|
if (!empty($input['new_password'])) {
|
||||||
if (strlen($input['new_password']) < 8) {
|
if (strlen($input['new_password']) < 8) {
|
||||||
$this->view->json([
|
$this->view->json([
|
||||||
|
|||||||
@@ -194,6 +194,12 @@ class AuthController
|
|||||||
|
|
||||||
$updateData = ['email' => $_POST['email']];
|
$updateData = ['email' => $_POST['email']];
|
||||||
|
|
||||||
|
if (isset($_POST['notifications_enabled'])) {
|
||||||
|
$updateData['notifications_enabled'] = 1;
|
||||||
|
} else {
|
||||||
|
$updateData['notifications_enabled'] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($_POST['new_password'])) {
|
if (!empty($_POST['new_password'])) {
|
||||||
$updateData['password'] = $_POST['new_password'];
|
$updateData['password'] = $_POST['new_password'];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,18 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-section">
|
||||||
|
<h3>Notification Preferences</h3>
|
||||||
|
<label class="checkbox-label">
|
||||||
|
<input type="checkbox" name="notifications_enabled" value="1"
|
||||||
|
<?= ($user['notifications_enabled'] ?? 1) ? 'checked' : '' ?>>
|
||||||
|
Enable push notifications
|
||||||
|
</label>
|
||||||
|
<p style="color:var(--text-muted);font-size:0.85em;margin:4px 0 0 24px">
|
||||||
|
Receive alerts for threshold breaches and app updates.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit" class="btn btn-primary">
|
<button type="submit" class="btn btn-primary">
|
||||||
<i class="fas fa-save"></i> Update Profile
|
<i class="fas fa-save"></i> Update Profile
|
||||||
|
|||||||
Reference in New Issue
Block a user