FRE-5335 Hook waitlist signup to send confirmation email via Resend
- Added @shieldai/shared-notifications, bullmq, ioredis deps to API - POST /api/waitlist/signup now sends waitlist_confirmation email via EmailService - Schedules welcome sequence (day1 intro, day3 features, day7 launch teaser) via BullMQ delayed jobs - Added waitlist email worker in @shieldai/jobs to process delayed welcome sequence emails - Templates already in place: waitlist_confirmation, waitlist_intro, waitlist_features, waitlist_launch_teaser with dark-themed HTML layouts Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { prisma } from '@shieldai/db';
|
||||
import { EmailService } from '@shieldai/shared-notifications';
|
||||
import { Queue } from 'bullmq';
|
||||
import { Redis } from 'ioredis';
|
||||
|
||||
const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
|
||||
const connection = new Redis(redisUrl);
|
||||
const waitlistEmailQueue = new Queue('waitlist-emails', { connection });
|
||||
|
||||
interface WaitlistSignupBody {
|
||||
email: string;
|
||||
@@ -10,6 +17,13 @@ interface WaitlistSignupBody {
|
||||
utmCampaign?: string;
|
||||
}
|
||||
|
||||
function getPosition(entryId: string): string {
|
||||
const hash = entryId.split('').reduce((acc, c) => acc + c.charCodeAt(0), 0);
|
||||
return String(10000 + (hash % 90000));
|
||||
}
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
export async function waitlistRoutes(fastify: FastifyInstance) {
|
||||
fastify.post('/waitlist/signup', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const body = request.body as WaitlistSignupBody;
|
||||
@@ -48,6 +62,47 @@ export async function waitlistRoutes(fastify: FastifyInstance) {
|
||||
},
|
||||
});
|
||||
|
||||
const name = body.name?.trim() || 'there';
|
||||
const position = getPosition(entry.id);
|
||||
|
||||
try {
|
||||
const emailService = EmailService.getInstance();
|
||||
const result = await emailService.sendWithTemplate(email, {
|
||||
templateId: 'waitlist_confirmation',
|
||||
variables: { name, position },
|
||||
});
|
||||
if (result.status === 'failed') {
|
||||
request.log.warn({ error: result.error }, 'Failed to send waitlist confirmation email');
|
||||
} else {
|
||||
request.log.info({ email }, 'Waitlist confirmation email sent');
|
||||
}
|
||||
} catch (err) {
|
||||
request.log.error({ err }, 'Error sending waitlist confirmation email');
|
||||
}
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
waitlistEmailQueue.add(
|
||||
'send-waitlist-intro',
|
||||
{ email, name, entryId: entry.id, tier },
|
||||
{ delay: 1 * DAY_MS, attempts: 3, backoff: { type: 'exponential', delay: 5000 } }
|
||||
),
|
||||
waitlistEmailQueue.add(
|
||||
'send-waitlist-features',
|
||||
{ email, name, entryId: entry.id, tier },
|
||||
{ delay: 3 * DAY_MS, attempts: 3, backoff: { type: 'exponential', delay: 5000 } }
|
||||
),
|
||||
waitlistEmailQueue.add(
|
||||
'send-waitlist-launch-teaser',
|
||||
{ email, name, entryId: entry.id, tier },
|
||||
{ delay: 7 * DAY_MS, attempts: 3, backoff: { type: 'exponential', delay: 5000 } }
|
||||
),
|
||||
]);
|
||||
request.log.info({ email }, 'Welcome sequence scheduled');
|
||||
} catch (err) {
|
||||
request.log.error({ err }, 'Failed to schedule welcome sequence emails');
|
||||
}
|
||||
|
||||
return reply.code(201).send({
|
||||
message: 'Welcome to the ShieldAI waitlist',
|
||||
id: entry.id,
|
||||
|
||||
Reference in New Issue
Block a user