Estimation Pipeline
The Campaign Quotation & Cost Estimation Engine calculates campaign costs using a step-by-step pipeline designed for speed and reliability.
⚙️ The Estimation Pipeline Steps
[ POST /pricing/estimate ]
│
▼
1. Resolve Audience (Labels / Contact Lists)
│
▼
2. Apply Deduplication & Extract Integrity Metrics
│
▼
3. Group Recipients by Destination Country Code
│
▼
4. Fetch Base Meta Rates & Determine Region Groups
│
▼
5. Resolve Monthly Volume and Apply Volume Tiers (Utility/Auth Only)
│
▼
6. Resolve Markup Precedence Rules (Platform Fees & overrides)
│
▼
7. Query Wallet Feasibility Status (If Shared WABA plan)
│
▼
8. Freeze Assumed Data Structures to Immutable Snapshot
│
▼
9. Persist Quotation & Enqueue PDF Task to BullMQ
│
▼
[ JSON Response returned to Frontend in <100ms ]
🛠️ Step-by-Step Processing Details
Step 1: Audience Resolution
- The payload contains an abstraction specifying the source (e.g.
{ type: "LABEL", ids: ["label-uuid"] }). - The
AudienceResolverServicequeries only theidcolumn from theContactmodel to avoid pulling large row properties into memory.
Step 2: Deduplication & Integrity Checks
- Compares and filters unique contact IDs to prevent double-billing on matching phone numbers.
- Computes integrity counts:
totalResolved: Initial match count.uniqueRecipients: Target message count.duplicateRecipients: Dropped phone records.invalidRecipientsandexcludedContacts.
Step 3: Destination Grouping
- Groups contacts by country code to calculate international rates.
- Resolves calling prefix numbers (e.g.
+91→IN,+1→US). - Runs a fast, indexed SQL aggregation query directly in PostgreSQL:
SELECT "countryCode", COUNT(*)::bigint as countFROM "Contact"WHERE "vendorId" = $1 AND "id" = ANY($2)GROUP BY "countryCode"
Step 4: Base Cost Mapping
- Matches country codes against mapped
regionGrouptables. - Fallback maps default unmatched locations to
OTHER. - Multiplies the resulting
ratePerUnitagainst destination counts to determine subtotal items.
Step 5: Volume Tier Discounts
- Checks if the template category qualifies for volume discounts (UTILITY and AUTHENTICATION only; MARKETING is excluded per Meta specifications).
- Discounts apply dynamically based on monthly recipient counts.
Step 6: Platform Markup Calculations
- Resolves markups based on the priority chain: custom overrides first, then global defaults, and finally a hardcoded fallback if no database rules exist.
- Calculates percentages, flats, or hybrid margins.
Step 7: Wallet Gating
- Gathers current wallet balances for Shared WABA accounts.
- Enforces strict minimum balance rules before campaign execution.
Step 8: Assumption Freezing
- Encodes a JSON snapshot containing all rates, regions, fees, and assumptions used.
- Signs the payload using a SHA256 checksum to prevent tampering.
Step 9: Quotation Registration & Queue Dispatch
- Inserts the quote header and line items.
- Dispatches an asynchronous job to BullMQ for Puppeteer rendering.
- Returns the quotation response immediately to the frontend.