Communication Recipient Targeting System
π Table of Contentsβ
- Overview
- Quick Start
- Documentation Index
- Problem & Solution
- Key Features
- Architecture Summary
- Getting Started
Overviewβ
The Recipient Targeting System is an enhancement to the communication module that allows businesses to control WHO receives each type of communication (low stock alerts, reports, invoices, etc.) through flexible, configurable rules.
What This Solvesβ
Before: All communications go to ALL business users (hardcoded in handlers)
After: Businesses configure recipients via admin UI:
- Send to specific roles (e.g., all inventory managers)
- Send to custom groups (e.g., "Finance Team" with mixed users + external emails)
- Send to ad-hoc contacts (e.g., specific emails/phone numbers)
- Combine all the above
Quick Startβ
For Product Managers / Business Usersβ
π Start here: RECIPIENT-TARGETING-PROPOSAL.md
- Executive summary
- Real-world examples
- UI mockups
- Business benefits
For Developersβ
π Start here: recipient-targeting-implementation-checklist.md
- Step-by-step implementation guide
- Code examples
- Testing strategies
For Database Adminsβ
π Start here: Migration file 2025-10-26t00:00:00.000z-communication-recipient-targeting.mjs
- Database schema
- Indexes
- Migration up/down scripts
For Architectsβ
π Start here: recipient-targeting-system-design.md
- Detailed architecture
- Data flow diagrams
- Service design
Documentation Indexβ
1. Proposal Summary πβ
File: RECIPIENT-TARGETING-PROPOSAL.md
Audience: Everyone (overview document)
Contents:
- Executive summary
- Current vs proposed state
- Real-world examples
- Mockups
- Timeline
- Risk assessment
When to read: First document to review, especially for decision-makers
2. Changes Summary πβ
File: recipient-targeting-changes-summary.md
Audience: Technical leads, developers
Contents:
- What changes in the system
- Before/after comparisons
- Configuration examples
- API examples
- Decision matrix
When to read: After reading the proposal, before implementation
3. Architecture Design ποΈβ
File: recipient-targeting-system-design.md
Audience: Architects, senior developers
Contents:
- Detailed architecture
- Core tables explained
- Data flow diagrams
- Service design patterns
- Usage examples
- Future enhancements
When to read: During design review or before coding
4. Entity Relationship Diagram πβ
File: recipient-targeting-erd.md
Audience: Database admins, backend developers
Contents:
- Visual ERD diagrams
- Table relationships explained
- Polymorphic relationships
- Index strategies
- Data flow example with SQL queries
When to read: Before implementing repositories or understanding data model
5. Implementation Checklist β β
File: recipient-targeting-implementation-checklist.md
Audience: Development team
Contents:
- 10-phase implementation plan
- Detailed tasks for each phase
- Code templates
- Testing strategies
- Timeline estimates
When to read: During sprint planning and implementation
6. Database Migration πΎβ
File: packages/backend/database/src/migrations/2025-10-26t00:00:00.000z-communication-recipient-targeting.mjs
Audience: Database admins, backend developers
Contents:
- SQL DDL for 4 new tables
- Indexes
- Constraints
- Rollback script
When to read: Before running migration
Problem & Solutionβ
The Problemβ
Current communication handlers are hardcoded:
// β Problem: Hardcoded recipient logic
const businessUsers = await this.businessUsersService.findByBusinessId(businessId);
for (const user of businessUsers) {
await this.communicationsService.send({
recipientContact: user.email
});
}
Issues:
- β Can't filter by role
- β Can't send to external contacts (suppliers, accountants)
- β Can't create distribution lists
- β Changing recipients requires code changes + deployment
- β Every business gets same recipients
- β No audit trail
The Solutionβ
Flexible, rule-based recipient resolution:
// β
Solution: Dynamic, rule-based resolution
const recipients = await this.recipientResolverService.resolveRecipients(
businessId,
'low_stock_alert',
'email'
);
// Recipients resolved from database rules:
// - All users with role 'inventory_manager'
// - All members of "Purchasing Team" group (users + external emails)
// - Ad-hoc: supplier@vendor.com
for (const recipient of recipients) {
await this.communicationsService.send({
recipientContact: recipient.contact,
metadata: { source: recipient.source } // Audit trail
});
}
Benefits:
- β Filter by roles, groups, or ad-hoc
- β Include external contacts
- β Businesses configure via admin UI
- β No code changes to modify recipients
- β Per-business customization
- β Full audit trail
Key Featuresβ
1. Role-Based Targetingβ
Send to all users with a specific role.
Example:
"Send low stock alerts to all inventory managers"
β Resolves to all business users with role 'inventory_manager'
2. Custom Groupsβ
Create groups with mixed internal users and external contacts.
Example:
"Finance Team" group:
- John Doe (internal user)
- Jane Smith (internal user)
- cpa@accounting-firm.com (external email)
- +1-555-ACCOUNTANT (external phone)
3. Ad-Hoc Recipientsβ
Include specific emails or phone numbers.
Example:
"Send emergency alerts to security service: +1-555-SECURITY"
4. Multi-Channel Supportβ
Different recipients per channel.
Example:
Low Stock Alerts:
- Email β inventory_manager role + Purchasing Team group
- SMS β Owner's phone + Manager's phone
5. Combination Targetingβ
Mix multiple targeting types.
Example:
"Send daily reports to:"
- Role: owner + admin
- Group: Finance Team
- Ad-hoc: accountant@firm.com
6. Recipient Previewβ
See who will receive before saving.
Feature: Preview API endpoint shows resolved recipients list with source attribution.
7. Audit Trailβ
Log who was selected and why.
Feature: communication_recipient_log table tracks every recipient selection with source information.
Architecture Summaryβ
New Tables (4)β
communication_recipient_group- Custom groups businesses createcommunication_group_member- Members in groups (users, emails, phones)business_communication_recipient_rule- Rules for who receives whatcommunication_recipient_log- Audit trail of selections
Key Servicesβ
RecipientResolverService- Core logic to resolve rules to recipientsRecipientGroupsService- Manage groups and membersRecipientRulesService- Manage targeting rules
Data Flowβ
Event β Handler β RecipientResolverService β Query Rules β Resolve Recipients
β
For each rule type:
β’ role β query users
β’ group β query members
β’ ad_hoc β use directly
β
Deduplicate β Return list
β
Send + Log
Getting Startedβ
Phase 1: Review Documentation (1-2 days)β
- Read proposal summary
- Review architecture design
- Understand ERD and data model
- Review implementation checklist
Phase 2: Approve & Plan (1 day)β
- Get stakeholder approval
- Create Jira tickets from checklist
- Assign ownership
- Schedule sprints
Phase 3: Database Setup (1 day)β
- Review migration file
- Test in development
- Run in staging
- Run in production
Phase 4: Backend Implementation (10-14 days)β
- Repositories
- Services (especially RecipientResolverService)
- APIs
- Update handlers
- Tests
Phase 5: Frontend Implementation (5-7 days)β
- Group management UI
- Rule configuration UI
- Integration with settings
Phase 6: Testing & Deployment (3-4 days)β
- Comprehensive testing
- Staging deployment
- Production deployment
- Monitoring
Total: 4-6 weeks
Real-World Examplesβ
Example 1: Restaurant Chainβ
Requirement: "Send low stock alerts for perishables to our supply manager + 2 external vendors"
Configuration:
Rules for low_stock_alert (email):
1. Role: supply_manager
2. Ad-hoc: vendor1@produce.com
3. Ad-hoc: vendor2@meats.com
Example 2: Retail Storeβ
Requirement: "Send daily sales reports to owner + 'Finance Team' (accountant + 2 managers + external CPA)"
Configuration:
Finance Team group:
- accountant (business_user)
- manager1 (business_user)
- manager2 (business_user)
- cpa@firm.com (email)
Rules for daily_report (email):
1. Role: owner
2. Group: Finance Team
Example 3: Multi-Location Businessβ
Requirement: "Emergency alerts via SMS to all owners, all admins, and security service"
Configuration:
Rules for emergency_alert (sms):
1. Role: owner
2. Role: admin
3. Ad-hoc: +1-555-SECURITY
Success Criteriaβ
Functionalβ
- β 100% of communication types support recipient targeting
- β Zero code changes needed to modify recipients
- β External contacts can receive communications
- β Multiple targeting types can be combined
- β Complete audit trail
Performanceβ
- β Recipient resolution < 500ms
- β No impact on communication send time
- β Database queries optimized
Usabilityβ
- β Admin UI is intuitive
- β Preview feature works
- β 80% of businesses use custom groups within 1 month
Questions?β
Technical Questionsβ
Contact: Backend team lead
Product Questionsβ
Contact: Product manager
Implementation Questionsβ
Refer to: Implementation Checklist
Related Documentationβ
- Multi-Channel Communication System
- Business Communication Configuration
- Template Rendering
- WhatsApp Templates
Version Historyβ
| Version | Date | Changes |
|---|---|---|
| 1.0 | 2025-10-26 | Initial proposal and design |
Appendix: Quick Referenceβ
Rule Typesβ
| Type | Use When | Example |
|---|---|---|
role | All users with specific role | All inventory managers |
group | Specific team/department | Finance Team |
ad_hoc_email | Single external email | supplier@vendor.com |
ad_hoc_phone | Single phone number | +1234567890 |
ad_hoc_whatsapp | Single WhatsApp | +1234567890 |
Member Typesβ
| Type | Use When | Example |
|---|---|---|
business_user | Internal employee | John Doe (user) |
email | External email contact | accountant@firm.com |
phone | Phone for SMS | +1234567890 |
whatsapp | WhatsApp number | +1234567890 |
Communication Types (Examples)β
low_stock_alertdaily_reportinvoicecollection_noticeemergency_alertinvitation
Channelsβ
emailsmswhatsappmessenger