Documentation Index
Fetch the complete documentation index at: https://mintlify.com/EdgarJr30/proyecto-de-grado-cms/llms.txt
Use this file to discover all available pages before exploring further.
Notifications Database Schema
The notification system in MLM CMMS is implemented through a set of database tables, functions, and triggers defined insql/modules/core_cmms/16_notifications.sql.
Architecture Overview
The notification system uses a multi-stage pipeline:- Events → Create notification events with metadata
- Deliveries → One delivery record per recipient (in-app)
- Outbox → Push notification queue with retry logic
- Subscriptions → Push endpoint registration per device
Core Tables
notification_events
Central event table storing notification metadata.| Column | Type | Description |
|---|---|---|
| id | uuid | Primary key |
| category | text | Category (assignments, comments, status_changes, deadlines, admin_system) |
| actor_user_id | uuid | User who triggered the event |
| title | text | Notification title |
| body | text | Notification body text |
| data | jsonb | Additional metadata (ticket_id, etc.) |
| channel_mask | integer | Delivery channels (1=in-app, 2=push, 3=both) |
| created_at | timestamptz | Event creation timestamp |
idx_notification_events_created_aton (created_at)idx_notification_events_categoryon (category)
sql/modules/core_cmms/16_notifications.sql
notification_deliveries
Per-recipient delivery records for in-app notifications.| Column | Type | Description |
|---|---|---|
| id | uuid | Primary key |
| event_id | uuid | Foreign key to notification_events |
| recipient_user_id | uuid | Foreign key to auth.users |
| status | text | Delivery status (delivered, seen, read) |
| delivered_at | timestamptz | Delivery timestamp |
| seen_at | timestamptz | First seen timestamp |
| read_at | timestamptz | Marked as read timestamp |
idx_notification_deliveries_recipienton (recipient_user_id, status)idx_notification_deliveries_eventon (event_id)
sql/modules/core_cmms/16_notifications.sql
notification_outbox
Push notification queue with retry and deduplication.| Column | Type | Description |
|---|---|---|
| id | uuid | Primary key |
| event_id | uuid | Foreign key to notification_events |
| recipient_user_id | uuid | Foreign key to auth.users |
| subscription_id | uuid | Foreign key to notification_push_subscriptions |
| status | text | Status (pending, processing, sent, failed, cancelled) |
| attempts | integer | Retry attempt count |
| last_error | text | Last error message |
| next_attempt_at | timestamptz | Next retry timestamp |
| sent_at | timestamptz | Successful send timestamp |
| created_at | timestamptz | Entry creation timestamp |
| processing_lease_until | timestamptz | Claimed by worker until this time |
idx_notification_outbox_status_next_attempton (status, next_attempt_at)idx_notification_outbox_recipienton (recipient_user_id)idx_notification_outbox_subscriptionon (subscription_id)
- Deduplication: Same event + subscription = single push
- Retry logic: Exponential backoff on failure (attempts 1, 2, 3…)
- Lease mechanism: Workers claim rows with
processing_lease_untilto prevent concurrent processing - Status tracking: pending → processing → sent/failed
sql/modules/core_cmms/16_notifications.sql
notification_push_subscriptions
Push notification endpoint registration.| Column | Type | Description |
|---|---|---|
| id | uuid | Primary key |
| user_id | uuid | Foreign key to auth.users |
| endpoint | text | Push service endpoint URL |
| keys_p256dh | text | P256DH encryption key (base64) |
| keys_auth | text | Auth secret (base64) |
| user_agent | text | Browser/device user agent |
| created_at | timestamptz | Registration timestamp |
| last_used_at | timestamptz | Last successful push timestamp |
idx_notification_push_subscriptions_useron (user_id)
sql/modules/core_cmms/16_notifications.sql
Key Functions
create_notification_event
Creates a notification event and triggers deliveries.p_category: One of assignments, comments, status_changes, deadlines, admin_systemp_actor_user_id: User who performed the actionp_title: Notification title (e.g., “New Assignment”)p_body: Notification body (e.g., “You have been assigned to Ticket #123”)p_data: JSON metadata (e.g.,{"ticket_id": 123, "url": "/tickets/123"})p_recipient_user_ids: Array of user UUIDs to notifyp_channel_mask: 1=in-app only, 2=push only, 3=both (default)
handle_notification_event() trigger creates deliveries and outbox entries.
Source: sql/modules/core_cmms/16_notifications.sql
send_self_test_notification
Admin function to send a test notification to yourself.p_title: Test notification titlep_body: Test notification bodyp_push_only: If true, only send push (no in-app)
sql/modules/core_cmms/16_notifications.sql
Triggers
handle_notification_event
Automatically creates deliveries and outbox entries after event creation.- Insert one
notification_deliveriesrow per recipient - If
channel_maskincludes push (2 or 3), insertnotification_outboxrows for each recipient’s active subscriptions - Deduplicate using UPSERT with ON CONFLICT DO NOTHING
sql/modules/core_cmms/16_notifications.sql
handle_ticket_comment_added
Creates notification events when comments are added to tickets.- Determine recipients (ticket assignees, requester, previous commenters)
- Call
create_notification_event()with category=‘comments’ - Include ticket_id and comment_id in data payload
sql/modules/core_cmms/16_notifications.sql
Notification Categories
| Category | Description | Typical Recipients |
|---|---|---|
| assignments | Work order assigned to technician | Assigned technicians |
| comments | New comment on ticket | Ticket assignees, requester, other commenters |
| status_changes | Ticket status changed | Assigned technicians, requester |
| deadlines | Deadline approaching or overdue | Assigned technicians, manager |
| admin_system | System announcements | All users or specific roles |
Channel Mask
Thechannel_mask field controls delivery channels using bitwise flags:
| Value | Binary | In-App | Push | Use Case |
|---|---|---|---|---|
| 1 | 01 | ✅ | ❌ | Low-priority, in-app only |
| 2 | 10 | ❌ | ✅ | Push only (mobile alerts) |
| 3 | 11 | ✅ | ✅ | Both (default) |
Retry Logic
The outbox uses exponential backoff for failed push attempts:failed and retries stop.
Subscription Cleanup
The Edge Function automatically removes invalid subscriptions:- HTTP 404/410: Endpoint no longer exists → delete subscription
- HTTP 401: Invalid credentials → delete subscription
- Successful push: Update
last_used_attimestamp
Database Views
v_notification_feed
Denormalized view joining events and deliveries for efficient querying.sql/modules/core_cmms/16_notifications.sql
Admin Queries
Check outbox status
Recent failed pushes
User’s active subscriptions
Notification delivery stats
Security Considerations
Next Steps
Push Setup Guide
Configure push notifications
Edge Functions
Deploy the push worker
PWA Integration
Implement frontend push
Core Tables
Review main database schema