π Backend Implementation Plan
Date: November 1, 2025
Focus: PDF Integration, Event Handlers, Backend Tests
Estimated Time: 12-16 hours
π CURRENT STATUSβ
What Already Exists β β
Communication System:
- β Communications service with channel adapters (Email, SMS, WhatsApp)
- β Template renderer service
- β Attachment handler service
- β Rate limiter service
- β Webhook controller (basic)
- β Event handlers (low stock alerts, invites)
- β Queue system
- β Repositories and database layer
PDF System:
- β Complete PDF generation infrastructure
- β Puppeteer-based PDF generator
- β Handlebars template renderer
- β File system storage
- β 13+ document templates (invoices, receipts, etc.)
- β Template management system
Testing:
- β Jest configuration exists
- β Some spec files exist (service.spec.ts)
- β οΈ Tests need to be written/completed
π― WHAT NEEDS TO BE IMPLEMENTEDβ
1. PDF Integration for Communications (4-6 hours)β
A) Communication PDF Templates (2h)β
Create HTML templates for common communication types:
- Invoice communication (with invoice details)
- Payment confirmation
- Order confirmation
- General announcement letter
Files to Create:
apps/backend/src/pdf/infrastructure/templates/
βββ communication-invoice.html
βββ communication-payment.html
βββ communication-order.html
βββ communication-general.html
B) PDF Attachment Service (2h)β
Enhance attachment handler to:
- Generate PDFs on-demand for communications
- Attach generated PDFs to emails
- Store PDFs in file system
- Return PDF URLs for frontend download
Files to Enhance:
apps/backend/src/communications/application/services/
βββ attachment-handler.service.ts (enhance)
Files to Create:
apps/backend/src/communications/application/use-cases/
βββ generate-communication-pdf.use-case.ts
βββ attach-pdf-to-communication.use-case.ts
C) Integration Points (1-2h)β
- Connect PDF service to communication service
- Add PDF generation to send-communication flow
- Add PDF download endpoint
Files to Enhance:
apps/backend/src/communications/
βββ communications.service.ts
βββ interfaces/communications.controller.ts
2. Event Handlers (3-4 hours)β
A) Provider Webhook Handlers (2h)β
Handle real-time events from providers:
SendGrid Webhooks:
- Email delivered
- Email opened
- Email clicked
- Email bounced
- Email dropped
Twilio Webhooks:
- SMS delivered
- SMS failed
- SMS status update
WhatsApp (Twilio) Webhooks:
- Message delivered
- Message read
- Message failed
Files to Enhance:
apps/backend/src/communications/interfaces/
βββ webhooks.controller.ts (enhance with all providers)
Files to Create:
apps/backend/src/communications/application/services/
βββ sendgrid-webhook-handler.service.ts
βββ twilio-webhook-handler.service.ts
βββ whatsapp-webhook-handler.service.ts
B) System Event Handlers (1-2h)β
Handle internal system events:
Events to Handle:
- Communication sent β Update stats
- Communication failed β Trigger retry logic
- Communication delivered β Update customer last contact
- Queue job completed β Clean up resources
- Template used β Update usage stats
Files to Create:
apps/backend/src/communications/application/events/
βββ on-communication-sent.handler.ts
βββ on-communication-failed.handler.ts
βββ on-communication-delivered.handler.ts
βββ on-queue-job-completed.handler.ts
C) Event Tracking Service (1h)β
Centralized event tracking and logging:
Files to Create:
apps/backend/src/communications/application/services/
βββ event-tracking.service.ts
3. Backend Tests (5-6 hours)β
A) Unit Tests (3h)β
Test individual services and components:
Services to Test:
communications.service.tstemplate-renderer.service.tsattachment-handler.service.tsrate-limiter.service.ts- Channel adapters (email, sms, whatsapp)
Files to Complete:
apps/backend/src/communications/application/
βββ communications.service.spec.ts (complete)
βββ services/template-renderer.service.spec.ts (complete)
βββ services/attachment-handler.service.spec.ts (complete)
βββ services/rate-limiter.service.spec.ts (complete)
βββ adapters/email-adapter.service.spec.ts (complete)
βββ adapters/sms-adapter.service.spec.ts (complete)
βββ adapters/whatsapp-adapter.service.spec.ts (complete)
B) Integration Tests (2h)β
Test complete flows:
Flows to Test:
- Send email communication end-to-end
- Send SMS with retries
- Generate PDF and attach to email
- Process queue jobs
- Handle webhook events
Files to Create:
apps/backend/src/communications/
βββ __tests__/
βββ send-communication.integration.spec.ts
βββ pdf-attachment.integration.spec.ts
βββ queue-processing.integration.spec.ts
βββ webhook-handling.integration.spec.ts
C) E2E Tests (1h)β
Test complete user workflows:
Workflows to Test:
- Create template β Send communication β Track delivery
- Send bulk communications via queue
- Webhook received β Status updated β Frontend notified
Files to Create:
apps/backend/test/
βββ communications/
βββ communication-lifecycle.e2e-spec.ts
βββ bulk-sending.e2e-spec.ts
π IMPLEMENTATION ORDERβ
Phase 1: Testing Infrastructure (1h)β
Why First: Need solid tests before adding features
- β Jest config verified
- β Test utilities created
- β Mock data helpers created
- β Test database setup
Phase 2: Backend Tests (4h)β
Why Second: Understand existing code, ensure stability
- Complete unit tests for existing services
- Write integration tests for key flows
- Add E2E tests for critical paths
- Result: Confidence in existing code
Phase 3: Event Handlers (3h)β
Why Third: Foundation for PDF and webhook integration
- Enhance webhook controller
- Create provider-specific handlers
- Add system event handlers
- Add event tracking
- Result: Real-time status updates
Phase 4: PDF Integration (4h)β
Why Last: Builds on stable, tested foundation
- Create PDF templates
- Enhance attachment service
- Add PDF generation use cases
- Add download endpoints
- Result: PDF attachments working
π― PRIORITY MATRIXβ
| Feature | Priority | Impact | Effort | Order |
|---|---|---|---|---|
| Backend Tests | π΄ CRITICAL | Very High | 5h | 1 |
| Event Handlers | π΄ HIGH | High | 3h | 2 |
| PDF Integration | π‘ MEDIUM | Medium | 4h | 3 |
π SUCCESS CRITERIAβ
PDF Integration β β
- Can generate PDF from communication template
- PDF automatically attached to email
- PDF accessible via download endpoint
- 4+ communication PDF templates created
- Tests passing
Event Handlers β β
- SendGrid webhooks fully handled
- Twilio webhooks fully handled
- WhatsApp webhooks fully handled
- System events tracked
- Real-time status updates working
- Tests passing
Backend Tests β β
- 80%+ code coverage
- All critical paths tested
- Integration tests passing
- E2E tests passing
- CI/CD ready
π QUICK STARTβ
Option 1: Start with Tests (Recommended)β
cd /Users/luisrangel/devLR/rpa/flowpos-workspace/apps/backend
# Run existing tests
pnpm test
# Run in watch mode
pnpm test:watch
# Check coverage
pnpm test:cov
Option 2: Start with Event Handlersβ
# Check existing webhook controller
cat src/communications/interfaces/webhooks.controller.ts
# Enhance with provider-specific handlers
Option 3: Start with PDF Integrationβ
# Check PDF infrastructure
ls -la src/pdf/
# Check existing templates
ls -la src/pdf/infrastructure/templates/
π FILE STRUCTUREβ
apps/backend/src/communications/
βββ application/
β βββ adapters/
β β βββ email-adapter.service.ts β
β β βββ email-adapter.service.spec.ts (complete)
β β βββ sms-adapter.service.ts β
β β βββ sms-adapter.service.spec.ts (complete)
β β βββ whatsapp-adapter.service.ts β
β β βββ whatsapp-adapter.service.spec.ts (complete)
β βββ events/
β β βββ on-communication-sent.handler.ts (NEW)
β β βββ on-communication-failed.handler.ts (NEW)
β β βββ on-communication-delivered.handler.ts (NEW)
β β βββ on-queue-job-completed.handler.ts (NEW)
β βββ services/
β β βββ attachment-handler.service.ts β
(enhance)
β β βββ sendgrid-webhook-handler.service.ts (NEW)
β β βββ twilio-webhook-handler.service.ts (NEW)
β β βββ whatsapp-webhook-handler.service.ts (NEW)
β β βββ event-tracking.service.ts (NEW)
β βββ use-cases/
β βββ generate-communication-pdf.use-case.ts (NEW)
β βββ attach-pdf-to-communication.use-case.ts (NEW)
βββ interfaces/
β βββ webhooks.controller.ts β
(enhance)
βββ __tests__/
βββ send-communication.integration.spec.ts (NEW)
βββ pdf-attachment.integration.spec.ts (NEW)
βββ queue-processing.integration.spec.ts (NEW)
βββ webhook-handling.integration.spec.ts (NEW)
apps/backend/src/pdf/infrastructure/templates/
βββ communication-invoice.html (NEW)
βββ communication-payment.html (NEW)
βββ communication-order.html (NEW)
βββ communication-general.html (NEW)
apps/backend/test/communications/
βββ communication-lifecycle.e2e-spec.ts (NEW)
βββ bulk-sending.e2e-spec.ts (NEW)
π― ESTIMATED TIMEβ
| Task | Estimated | Actual | Status |
|---|---|---|---|
| Testing Infrastructure | 1h | β | β |
| Unit Tests | 3h | β | β |
| Integration Tests | 2h | β | β |
| E2E Tests | 1h | β | β |
| Webhook Handlers | 2h | β | β |
| System Event Handlers | 1-2h | β | β |
| Event Tracking | 1h | β | β |
| PDF Templates | 2h | β | β |
| PDF Service Integration | 2h | β | β |
| PDF Endpoints | 1h | β | β |
| TOTAL | 15-17h | β | β |
π NEXT STEPSβ
Choose your starting point:
A) Tests First (Recommended)β
- Build confidence in existing code
- Catch bugs early
- Set up CI/CD foundation
- Start: Complete existing
.spec.tsfiles
B) Event Handlers Firstβ
- Get real-time updates working
- Foundation for monitoring
- Improve user experience
- Start: Enhance
webhooks.controller.ts
C) PDF Integration Firstβ
- Most visible feature
- Direct user value
- Uses existing PDF infrastructure
- Start: Create communication PDF templates
π RESOURCESβ
Documentationβ
- SendGrid Webhook Docs: https://docs.sendgrid.com/for-developers/tracking-events/event
- Twilio SMS Callbacks: https://www.twilio.com/docs/sms/api/message-resource#message-status-values
- Twilio WhatsApp Status: https://www.twilio.com/docs/whatsapp/api#message-status-values
- Jest NestJS Testing: https://docs.nestjs.com/fundamentals/testing
- Puppeteer PDF Generation: https://pptr.dev/#?product=Puppeteer&version=v21.0.0&show=api-pagepdfoptions
Existing Code to Referenceβ
- PDF Generation:
apps/backend/src/pdf/ - Communication Services:
apps/backend/src/communications/application/ - Existing Tests: Look for
.spec.tsfiles
β COMPLETION CHECKLISTβ
PDF Integrationβ
- 4 PDF templates created
- PDF generation service working
- PDF attachment to emails working
- PDF download endpoint working
- Tests written and passing
Event Handlersβ
- SendGrid webhooks handled
- Twilio SMS webhooks handled
- WhatsApp webhooks handled
- System events tracked
- Event logging working
- Tests written and passing
Backend Testsβ
- All existing spec files completed
- Unit tests at 80%+ coverage
- Integration tests passing
- E2E tests passing
- CI/CD pipeline configured
Document Version: 1.0
Last Updated: November 1, 2025
Status: π READY TO IMPLEMENT