Create InstaReport Automation Processor
Overview
The Create InstaReport automation generates automated marketing reports (InstaReports) for business contacts associated with deals when they enter a specific pipeline stage. Supports custom templates and email/SMS notifications.
Source File: queue-manager/queues/deals/automations/instareport.js
Triggered By: queue-manager/services/deals/automations.js
Business Impact: HIGH - Lead nurturing & value demonstration
Processing Logic
sequenceDiagram
participant AUTO as Automation Service
participant QUEUE as InstaReport Queue
participant DEAL as Deals
participant CONTACT as Contacts
participant REPORT as InstaReport Utility
participant NOTIFY as Email/SMS
AUTO->>QUEUE: Add automation job
QUEUE->>DEAL: Find eligible deals<br/>(with business contact)
loop For Each Deal
QUEUE->>CONTACT: Validate business contact
QUEUE->>REPORT: Create instareport<br/>(template + business)
REPORT-->>QUEUE: Report ID
opt Notifications Enabled
QUEUE->>NOTIFY: Send email/SMS with report link
end
QUEUE->>DEAL: Mark automation triggered
end
Key Features
Template Requirement
const templateID =
automation.data && automation.data.template_id ? automation.data.template_id : null;
if (!templateID) {
throw new Error('Template not provided!');
}
Business Contact Requirement
// Only processes deals with business contacts
const business = deal.business ? deal.business : null;
if (business) {
const businessRS = await Contact.findById(business).exec();
if (!businessRS) {
throw new Error('Invalid business ID.');
}
// Create instareport...
} else {
throw new Error('No business contact associated with deal');
}
Notification Configuration
const notifications =
automation.data && automation.data.notifications ? automation.data.notifications : {};
// Normalize field names
if (notifications.sms?.message) notifications.sms.content = notifications.sms.message;
if (notifications.email?.message) notifications.email.content = notifications.email.message;
InstaReport Creation
const accountRS = await Account.findById(accountID, 'parent_account').lean().exec();
const parentAccount = accountRS?.parent_account || accountID;
const result = await addInstareport(parentAccount, userID, [business], notifications, templateID);
Collections Used
Input: deal-automation
Query: deal (with business field)
Validation: contact (business contact)
Query: account (parent account lookup)
Create: instareports-* collections (via utility)
Optional: communication (email/SMS records)
Automation Data Structure
{
template_id: ObjectId("templateId"), // Required
notifications: {
email: {
enabled: true,
subject: "Your Custom Report",
content: "Hi {{business.company}}, check out your report...",
sender: "reports@company.com"
},
sms: {
enabled: true,
content: "Your report is ready: {{report.url}}"
}
}
}
Use Cases
Example 1: Lead Magnet
// Automation
{
stage_id: "new_lead",
action: "create_instareport",
data: {
template_id: seoAuditTemplateID,
notifications: {
email: {
enabled: true,
subject: "Free SEO Audit - {{business.company}}",
content: "Thank you for your interest. Here's your free SEO audit..."
}
}
},
delay: 0
}
// Result: Instant value delivery to new leads
Example 2: Proposal Supporting Data
// Automation
{
stage_id: "proposal_sent",
action: "create_instareport",
data: {
template_id: competitorAnalysisTemplateID,
notifications: {
email: {
enabled: true,
subject: "Competitor Analysis for {{business.company}}",
content: "To support our proposal, we've prepared a competitive analysis..."
}
}
},
delay: 3600 // 1 hour after proposal
}
Example 3: Onboarding Report
// Automation
{
stage_id: "closed_won",
action: "create_instareport",
data: {
template_id: onboardingReportTemplateID,
notifications: {
email: {
enabled: true,
subject: "Welcome! Your Onboarding Report",
content: "Congratulations on joining us! Here's your baseline report..."
},
sms: {
enabled: true,
content: "Welcome aboard! Your onboarding report: {{report.url}}"
}
}
},
delay: 0
}
Parent Account Handling
const accountRS = await Account.findById(accountID, 'parent_account').lean().exec();
const parentAccount = accountRS?.parent_account || accountID;
Uses parent account for instareport creation if account is a sub-account.
Error Handling
Common Errors
- No Template: template_id not provided
- Invalid Business: Contact doesn't exist
- No Business: Deal has no business contact
- Template Not Found: Invalid template ID
- Report Generation Failed: Utility error
Error Response
{
deal: dealID,
business: businessID,
message: "Invalid business ID.",
additional_info: {...}
}
Notification Flow
- InstaReport Created: Report generated with template
- Email Sent (if enabled): Email with report link
- SMS Sent (if enabled): SMS with shortened link
- Activity Logged: Report creation tracked
Performance
Execution Time: 2-5 seconds per report (report generation intensive)
Batch Processing: Sequential (not parallel) due to report generation load
Complexity: HIGH
Lines of Code: 193
Business Value: Automated value demonstration, lead nurturing, differentiation