Notification Read State Service
Component Detail
Description
Service handling all read-state mutations for notifications — marking individual items read, marking all as read, and clearing all notifications. Wraps repository calls with optimistic UI updates via BLoC events and rolls back on failure. Ensures read_at timestamps are set correctly for audit trail integrity.
notification-read-service
Summaries
The Notification Read State Service directly shapes the quality of the user experience by ensuring the notification badge count and feed state update instantly when users interact with their notifications, rather than waiting for a server round-trip. This responsiveness is a key quality signal: users who see immediate feedback trust the app and engage more frequently. The service also maintains a complete audit trail via read_at timestamps, which is valuable for programme monitoring — coordinators and admins can verify when notifications were acknowledged, supporting accountability and compliance reporting. Its rollback capability on server errors means users are never left with misleading state, protecting data integrity without requiring manual refresh.
The Notification Read State Service is a medium-complexity component whose primary delivery risk lies in the optimistic update and rollback logic. Simulating server failures in automated tests requires dependency injection of the repository with mock failure responses — ensure the test harness supports this before integration testing begins. The service depends on the notification repository, which must be stable first. All three mutation operations (mark-read, mark-all-read, clear-all) need explicit test coverage including both success and failure paths.
The operationId-based success/failure callbacks suggest async operation tracking — this pattern must be consistent with how the BLoC consumes events to avoid race conditions when multiple operations are triggered in quick succession, particularly mark-all-read followed immediately by clear-all.
Notification Read State Service wraps notification-repository mutation calls with an optimistic-update pattern coordinated via the notification-bloc. On markAsRead(notificationId), it emits an optimistic BLoC event immediately to update UI state, fires the async repository call, then emits onOperationSuccess or onOperationFailure based on the result — triggering a revert event on failure. markAllAsRead(userId) and clearAll(userId) follow the same pattern but operate on full collections. The getUnreadCount(userId) method delegates directly to the repository without optimistic logic since it is a read-only query.
The operationId parameter passed to success/failure callbacks enables correlation between async completions and the originating BLoC event in cases where multiple operations are in-flight. Ensure read_at timestamps use UTC and are set server-side (via Supabase default or trigger) rather than client-side to prevent clock-skew issues in audit trail integrity.
Responsibilities
- Mark individual notifications as read with read_at timestamp
- Mark all notifications as read in a single batch operation
- Clear all notifications for the current user
- Apply optimistic state updates before server confirmation
- Roll back optimistic updates on server error
Interfaces
markAsRead(String notificationId)
markAllAsRead(String userId)
clearAll(String userId)
getUnreadCount(String userId)
onOperationSuccess(String operationId)
onOperationFailure(String operationId, Object error)
Relationships
Related Data Entities (2)
Data entities managed by this component