self-replicating/packages/orchestrator/src/workflows/06-email-automation.ts

82 lines
2.4 KiB
TypeScript

import { WorkflowBase, WorkflowContext, WorkflowResult } from './workflow-base';
import { WorkflowType } from '@prisma/client';
import { SendgridClient } from '../integrations/email/sendgrid-client';
import { log } from '../monitoring/logger';
import { integrations } from '../utils/config';
export class EmailAutomationWorkflow extends WorkflowBase {
protected type: WorkflowType = 'EMAIL_AUTOMATION';
private sendgrid: SendgridClient;
constructor(db: any, alerts: any) {
super(db, alerts);
this.sendgrid = new SendgridClient();
}
protected async execute(context: WorkflowContext): Promise<WorkflowResult> {
const business = await this.getBusiness(context.businessId);
log.info('Starting email automation setup', { businessId: context.businessId });
try {
if (!integrations.hasSendgrid()) {
log.warn('Sendgrid not configured - skipping email automation', {
businessId: context.businessId,
});
return {
success: true,
data: { configured: false, reason: 'Sendgrid not configured' },
};
}
// Create email templates
const templates = await this.createEmailTemplates(business);
log.info('Email templates created', {
businessId: context.businessId,
templateCount: templates.length,
});
// Update business
await this.db.business.update({
where: { id: context.businessId },
data: { emailAutomation: true },
});
return {
success: true,
data: {
configured: true,
templates: templates.length,
},
};
} catch (error) {
log.error('Email automation workflow failed', error, {
businessId: context.businessId,
});
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
private async createEmailTemplates(business: any): Promise<any[]> {
const templates = [
{
name: 'Welcome Email',
subject: `Welcome to ${business.name}!`,
content: `Thank you for joining ${business.name}. We're excited to have you!`,
},
{
name: 'Onboarding Sequence',
subject: `Get started with ${business.name}`,
content: `Here's how to make the most of ${business.name}...`,
},
];
// In production, would create actual templates via Sendgrid API
return templates;
}
}