kamau1's picture
feat(notifications): integrate notification system across core services
0e0561d

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

  1. 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 | βœ“ | βœ— | βœ“ | βœ— |

  1. 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)

  1. 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
  2. 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?