Skip to main content

πŸš€ 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.ts
  • template-renderer.service.ts
  • attachment-handler.service.ts
  • rate-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:

  1. Send email communication end-to-end
  2. Send SMS with retries
  3. Generate PDF and attach to email
  4. Process queue jobs
  5. 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:

  1. Create template β†’ Send communication β†’ Track delivery
  2. Send bulk communications via queue
  3. 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

  1. βœ… Jest config verified
  2. βœ… Test utilities created
  3. βœ… Mock data helpers created
  4. βœ… Test database setup

Phase 2: Backend Tests (4h)​

Why Second: Understand existing code, ensure stability

  1. Complete unit tests for existing services
  2. Write integration tests for key flows
  3. Add E2E tests for critical paths
  4. Result: Confidence in existing code

Phase 3: Event Handlers (3h)​

Why Third: Foundation for PDF and webhook integration

  1. Enhance webhook controller
  2. Create provider-specific handlers
  3. Add system event handlers
  4. Add event tracking
  5. Result: Real-time status updates

Phase 4: PDF Integration (4h)​

Why Last: Builds on stable, tested foundation

  1. Create PDF templates
  2. Enhance attachment service
  3. Add PDF generation use cases
  4. Add download endpoints
  5. Result: PDF attachments working

🎯 PRIORITY MATRIX​

FeaturePriorityImpactEffortOrder
Backend TestsπŸ”΄ CRITICALVery High5h1
Event HandlersπŸ”΄ HIGHHigh3h2
PDF Integration🟑 MEDIUMMedium4h3

πŸ“Š 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​

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​

TaskEstimatedActualStatus
Testing Infrastructure1hβ€”β˜
Unit Tests3hβ€”β˜
Integration Tests2hβ€”β˜
E2E Tests1hβ€”β˜
Webhook Handlers2hβ€”β˜
System Event Handlers1-2hβ€”β˜
Event Tracking1hβ€”β˜
PDF Templates2hβ€”β˜
PDF Service Integration2hβ€”β˜
PDF Endpoints1hβ€”β˜
TOTAL15-17hβ€”β˜

πŸŽ‰ NEXT STEPS​

Choose your starting point:

  • Build confidence in existing code
  • Catch bugs early
  • Set up CI/CD foundation
  • Start: Complete existing .spec.ts files

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​

Existing Code to Reference​

  • PDF Generation: apps/backend/src/pdf/
  • Communication Services: apps/backend/src/communications/application/
  • Existing Tests: Look for .spec.ts files

βœ… 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