Campaigns Module Specification
1. Purpose & Overview
The module manages WhatsApp marketing broadcast campaigns, recipient list selection, execution scheduling, and campaign performance analytics.
campaigns2. Responsibilities
- Maintain and
Campaignmodels.CampaignRecipient - Track campaign states (,
DRAFT,SCHEDULED,RUNNING,PAUSED,COMPLETED).FAILED - Interface with the module for executing mass broadcast dispatches.
scheduler
3. Directory Structure
text
services/api/campaigns/
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── serializers.py
└── views.py
4. Models & Database Schemas
python
class CampaignStatus(models.TextChoices):
DRAFT = 'DRAFT', 'Draft'
SCHEDULED = 'SCHEDULED', 'Scheduled'
RUNNING = 'RUNNING', 'Running'
PAUSED = 'PAUSED', 'Paused'
COMPLETED = 'COMPLETED', 'Completed'
FAILED = 'FAILED', 'Failed'
class Campaign(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
tenant = models.ForeignKey('tenants.Tenant', on_delete=models.CASCADE)
name = models.CharField(max_length=255)
status = models.CharField(max_length=20, choices=CampaignStatus.choices, default=CampaignStatus.DRAFT)
template = models.ForeignKey('templates.Template', on_delete=models.PROTECT)
scheduled_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
5. Services & Business Logic
- : Transition state to
Campaign.start_broadcast()and trigger background worker dispatch.RUNNING - : Computes total sent, delivered, read, and failed counts.
Campaign.calculate_analytics()
6. Serializers & Data Transfer Objects
- : Validates template selection and execution parameters.
CampaignSerializer - : Includes analytics metrics and recipient breakdown.
CampaignDetailSerializer
7. Views & API Endpoints
- : List campaigns for active tenant.
GET /api/campaigns/ - : Create new broadcast campaign.
POST /api/campaigns/ - : Retrieve campaign analytics.
GET /api/campaigns/{id}/
8. Permissions & Role Rules
- &
TENANT_ADMIN: Can create and view campaigns within their active tenant.TENANT_USER
9. Signals & Event Listeners
- : Notifies operators upon campaign completion or failure.
post_save
10. Background Tasks & Celery Jobs
- Tasks dispatched to .
scheduler.tasks.process_scheduler_job
11. Data Flow & External Dependencies
- Interfaces with module for HSM template validation and
templatesmodule for Celery execution.scheduler
12. Business Rules & Validations
- Campaigns cannot be executed without a Meta-approved HSM message template.
- Scheduled time must be in the future.
scheduled_at
13. Sequence Diagram (Mermaid)
Rendering diagram...
14. Known Limitations & Technical Debt
- Recipient lists are stored as relational foreign keys (can incur database I/O overhead for campaigns exceeding 100,000 recipients).
15. Future Improvements
- Implement chunked recipient storage using Redis Bitmaps or Parquet files for ultra-large broadcasts (> 1 million recipients).