Spaces:
Sleeping
Notification System Design Plan Current State Analysis β Already Built:
Database schema with all necessary fields Notification model with polymorphic support (source_type, source_id) NotificationService with email/WhatsApp/in-app support Basic CRUD operations (create, read, mark as read, delete) User notification queries with filters β Missing:
Integration hooks - No automatic notifications on events Event triggers - Not connected to business logic API endpoints - No REST API for frontend Real-time delivery - No WebSocket/SSE for live updates Notification Strategy
- Notification Types & Triggers A. Ticket Lifecycle Notifications
βοΈ Ticket Assigned β Field agent gets notified βοΈ Ticket Status Changed β PM/Dispatcher notified (in_progress, completed, cancelled) βοΈ Ticket Overdue β PM/Dispatcher gets daily reminder βοΈ Ticket Completed β PM gets completion notification βοΈ Assignment Rejected β Dispatcher notified when agent rejects B. Sales Order Notifications
βοΈ Bulk Import Complete β User who uploaded gets summary (success/failed counts) βοΈ Bulk Promote Complete β User gets ticket creation summary βοΈ Sales Order Assigned to Region β Regional manager notified C. Expense Notifications
βοΈ Expense Submitted β PM/Dispatcher notified for approval βοΈ Expense Approved β Agent notified βοΈ Expense Rejected β Agent notified with reason βοΈ Expense Paid β Agent notified D. Team & Project Notifications
βοΈ User Invited to Project β Invitee gets invitation βοΈ User Added to Team β User notified of project assignment βοΈ Project Status Changed β Team members notified E. Financial Notifications
βοΈ Payroll Generated β Worker notified βοΈ Payment Processed β Recipient notified βοΈ Invoice Created β Contractor notified F. System Notifications
βοΈ Daily Summary β PM gets daily stats (tickets completed, pending, overdue) βοΈ Weekly Report β Platform admin gets usage metrics βοΈ SLA Violations β PM/Client admin notified 2. Notification Channels by Role | Role | In-App | WhatsApp | Email | SMS | |------|--------|----------|-------|-----| | Field Agent | β | β (urgent) | β | β | | Dispatcher | β | β | β | β | | PM | β | β | β | β | | Sales Manager | β | β | β | β | | Client Admin | β | β | β | β | | Platform Admin | β | β | β | β |
- Implementation Approach Phase 1: Core Integration (Week 1)
Create notification helper functions in services Add notification triggers to existing services: ticket_service.py β ticket lifecycle events ticket_assignment_service.py β assignment events sales_order_service.py β bulk operation results ticket_expense_service.py β expense approval flow Phase 2: API Endpoints (Week 1) 3. Create /api/v1/notifications endpoints:
GET /notifications - List user notifications GET /notifications/stats - Get unread count PUT /notifications/{id}/read - Mark as read PUT /notifications/mark-all-read - Mark all as read DELETE /notifications/{id} - Delete notification Phase 3: Real-time (Week 2) 4. Add WebSocket/SSE for live notifications (optional) 5. Add background job for email/WhatsApp delivery
Phase 4: Advanced Features (Week 2) 6. User notification preferences (which events to receive) 7. Notification templates 8. Digest notifications (daily/weekly summaries)
- Code Structure src/app/ βββ services/ β βββ notification_service.py (β exists) β βββ notification_helper.py (NEW - convenience functions) βββ api/v1/ β βββ notifications.py (NEW - REST endpoints) βββ schemas/ β βββ notification.py (check if exists, add if needed) βββ templates/ βββ emails/ β βββ ticket_assigned.html β βββ expense_approved.html β βββ bulk_import_complete.html βββ whatsapp/ βββ ticket_assigned.txt βββ expense_approved.txt
- Example Integration Points A. Ticket Assignment (in ticket_assignment_service.py)
After assigning ticket
await NotificationHelper.notify_ticket_assigned( db=db, ticket=ticket, agent=agent, assigned_by=current_user ) B. Bulk Import Complete (in sales_order_service.py)
After bulk import
await NotificationHelper.notify_bulk_import_complete( db=db, user_id=current_user.id, total=result.total, successful=result.successful, failed=result.failed ) C. Expense Approved (in ticket_expense_service.py)
After approval
await NotificationHelper.notify_expense_approved( db=db, expense=expense, approved_by=current_user ) 6. Notification Priority Levels π΄ Urgent (WhatsApp + In-App): Ticket assigned, SLA violation π‘ High (Email + In-App): Expense needs approval, ticket overdue π’ Normal (In-App only): Status updates, bulk operation results βͺ Low (In-App only): Daily summaries, informational Next Steps - What Should We Build First? I recommend this order:
Create notification_helper.py - Convenience functions for common notifications Add triggers to ticket assignment - Most critical for field operations Add bulk operation notifications - Your immediate need (sales order imports) Create API endpoints - So frontend can display notifications Add expense notifications - Complete the approval workflow Should I start implementing? Which part would you like me to build first? I suggest starting with:
Notification helper functions Bulk operation notifications (sales orders, ticket creation) Basic API endpoints This will give you immediate value and we can expand from there. What do you think?