Overview
The AlertsRepository provides CRUD operations for managing alerts, as well as catalog methods for alert types and severities. It also includes action methods for resolving and reopening alerts. All methods are suspend functions that return Result<T> for consistent error handling.
Catalog Methods
getAlertTypes()
Fetches all alert types from the catalog.
suspend fun getAlertTypes(): Result<List<AlertType>>
Returns
Result containing a list of AlertType objects or an error
getAlertSeverities()
Fetches all alert severities from the catalog.
suspend fun getAlertSeverities(): Result<List<AlertSeverityCatalog>>
Returns
Result<List<AlertSeverityCatalog>>
Result containing a list of AlertSeverityCatalog objects or an error
CRUD Methods
getAlertsByTenantId()
Fetches all alerts for a specific tenant.
suspend fun getAlertsByTenantId(tenantId: Long): Result<List<Alert>>
Parameters
The tenant ID to filter alerts by
Returns
Result containing a list of Alert objects or an error
createAlert()
Creates a new alert for a tenant.
suspend fun createAlert(tenantId: Long, request: AlertCreateRequest): Result<Alert>
Parameters
request
AlertCreateRequest
required
The alert data to create
Returns
Result containing the created Alert object or an error
updateAlert()
Updates an existing alert.
suspend fun updateAlert(tenantId: Long, alertId: Long, request: AlertUpdateRequest): Result<Alert>
Parameters
request
AlertUpdateRequest
required
The alert data to update
Returns
Result containing the updated Alert object or an error
deleteAlert()
Deletes an alert by ID.
suspend fun deleteAlert(tenantId: Long, alertId: Long): Result<Unit>
Parameters
Returns
Result indicating success or error
Action Methods
resolveAlert()
Resolves an alert.
suspend fun resolveAlert(tenantId: Long, alertId: Long, resolvedByUserId: Long? = null): Result<Alert>
Parameters
Optional user ID who resolved the alert
Returns
Result containing the resolved Alert object or an error
reopenAlert()
Reopens a resolved alert.
suspend fun reopenAlert(tenantId: Long, alertId: Long): Result<Alert>
Parameters
Returns
Result containing the reopened Alert object or an error
Usage Example
class AlertsViewModel(
private val alertsRepository: AlertsRepository
) : ViewModel() {
private val _alerts = MutableStateFlow<List<Alert>>(emptyList())
val alerts: StateFlow<List<Alert>> = _alerts.asStateFlow()
private val _alertTypes = MutableStateFlow<List<AlertType>>(emptyList())
val alertTypes: StateFlow<List<AlertType>> = _alertTypes.asStateFlow()
private val _severities = MutableStateFlow<List<AlertSeverityCatalog>>(emptyList())
val severities: StateFlow<List<AlertSeverityCatalog>> = _severities.asStateFlow()
private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error.asStateFlow()
fun loadAlerts(tenantId: Long) {
viewModelScope.launch {
alertsRepository.getAlertsByTenantId(tenantId)
.onSuccess { _alerts.value = it }
.onFailure { _error.value = it.message }
}
}
fun loadCatalogData() {
viewModelScope.launch {
// Load alert types and severities in parallel
launch {
alertsRepository.getAlertTypes()
.onSuccess { _alertTypes.value = it }
.onFailure { _error.value = it.message }
}
launch {
alertsRepository.getAlertSeverities()
.onSuccess { _severities.value = it }
.onFailure { _error.value = it.message }
}
}
}
fun createAlert(tenantId: Long, request: AlertCreateRequest) {
viewModelScope.launch {
alertsRepository.createAlert(tenantId, request)
.onSuccess { loadAlerts(tenantId) }
.onFailure { _error.value = it.message }
}
}
fun resolveAlert(tenantId: Long, alertId: Long, userId: Long?) {
viewModelScope.launch {
alertsRepository.resolveAlert(tenantId, alertId, userId)
.onSuccess { loadAlerts(tenantId) }
.onFailure { _error.value = it.message }
}
}
fun reopenAlert(tenantId: Long, alertId: Long) {
viewModelScope.launch {
alertsRepository.reopenAlert(tenantId, alertId)
.onSuccess { loadAlerts(tenantId) }
.onFailure { _error.value = it.message }
}
}
}
Dependency Injection
Inject the repository using Koin in your Composables:
import org.koin.compose.viewmodel.koinViewModel
@Composable
fun AlertsScreen(tenantId: Long) {
val viewModel: AlertsViewModel = koinViewModel()
val alerts by viewModel.alerts.collectAsState()
val alertTypes by viewModel.alertTypes.collectAsState()
LaunchedEffect(Unit) {
viewModel.loadCatalogData()
viewModel.loadAlerts(tenantId)
}
// UI implementation
}