From 94916ea5848ab3b2eb51bb4d329edbfe40abfb0e Mon Sep 17 00:00:00 2001 From: denverm Date: Sun, 18 Jan 2026 00:29:59 +0200 Subject: [PATCH] Update scripts/test-resend-webhook.ts --- scripts/test-resend-webhook.ts | 80 ++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/scripts/test-resend-webhook.ts b/scripts/test-resend-webhook.ts index 1e193e5..9d89d77 100644 --- a/scripts/test-resend-webhook.ts +++ b/scripts/test-resend-webhook.ts @@ -1,34 +1,47 @@ #!/usr/bin/env tsx /** * Test Resend Webhook Locally - * + * * This script simulates a Resend webhook event to test your webhook handler. - * + * * Usage: * npx tsx scripts/test-resend-webhook.ts [emailId] [eventType] - * + * * Examples: * npx tsx scripts/test-resend-webhook.ts # Uses first email from DB * npx tsx scripts/test-resend-webhook.ts abc123 delivered # Specific email and event */ -import { prisma } from '../src/lib/db'; +import { prisma } from "../src/lib/db"; -const BASE_URL = process.env.APP_URL || 'http://localhost:3001'; +const BASE_URL = process.env.APP_URL || "http://localhost:3001"; + +type EmailLogPreview = { + id: string; + resendMessageId: string | null; + recipientEmail: string; + status: string; + createdAt?: Date; +}; async function testWebhook() { const args = process.argv.slice(2); let emailId = args[0]; - const eventType = args[1] || 'email.delivered'; + const eventType = args[1] || "email.delivered"; - console.log('šŸ” Testing Resend Webhook Locally\n'); + console.log("šŸ” Testing Resend Webhook Locally\n"); // If no emailId provided, get the most recent one from DB if (!emailId) { const recentEmail = await prisma.emailLog.findFirst({ where: { resendMessageId: { not: null } }, - orderBy: { createdAt: 'desc' }, - select: { id: true, resendMessageId: true, recipientEmail: true, status: true }, + orderBy: { createdAt: "desc" }, + select: { + id: true, + resendMessageId: true, + recipientEmail: true, + status: true, + }, }); if (recentEmail?.resendMessageId) { @@ -39,23 +52,33 @@ async function testWebhook() { console.log(` Recipient: ${recentEmail.recipientEmail}`); console.log(` Current Status: ${recentEmail.status}\n`); } else { - console.log('āŒ No emails found in database with resendMessageId'); - console.log(' Send an email first, then run this script again.\n'); - + console.log("āŒ No emails found in database with resendMessageId"); + console.log(" Send an email first, then run this script again.\n"); + // Show all emails for debugging const allEmails = await prisma.emailLog.findMany({ take: 5, - orderBy: { createdAt: 'desc' }, - select: { id: true, resendMessageId: true, recipientEmail: true, status: true, createdAt: true }, + orderBy: { createdAt: "desc" }, + select: { + id: true, + resendMessageId: true, + recipientEmail: true, + status: true, + createdAt: true, + }, }); - + if (allEmails.length > 0) { - console.log('šŸ“‹ Recent emails in database:'); - allEmails.forEach(e => { - console.log(` - ${e.recipientEmail} | Status: ${e.status} | ResendID: ${e.resendMessageId || 'null'}`); + console.log("šŸ“‹ Recent emails in database:"); + allEmails.forEach((e: EmailLogPreview) => { + console.log( + ` - ${e.recipientEmail} | Status: ${e.status} | ResendID: ${ + e.resendMessageId || "null" + }` + ); }); } - + process.exit(1); } } @@ -66,9 +89,9 @@ async function testWebhook() { created_at: new Date().toISOString(), data: { email_id: emailId, - from: 'noreply@themoyos.co.za', - to: ['test@example.com'], - subject: 'Test Email', + from: "noreply@themoyos.co.za", + to: ["test@example.com"], + subject: "Test Email", created_at: new Date().toISOString(), }, }; @@ -78,9 +101,9 @@ async function testWebhook() { try { const response = await fetch(`${BASE_URL}/api/webhooks/resend`, { - method: 'POST', + method: "POST", headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", // Note: Not including svix headers since we're testing without signature verification // In production, Resend sends: svix-id, svix-timestamp, svix-signature }, @@ -88,7 +111,7 @@ async function testWebhook() { }); const responseText = await response.text(); - let responseData; + let responseData: unknown; try { responseData = JSON.parse(responseText); } catch { @@ -113,11 +136,10 @@ async function testWebhook() { } else { console.log(`\nāŒ Webhook failed`); } - } catch (error) { - console.error('āŒ Error calling webhook:', error); + } catch (error: unknown) { + console.error("āŒ Error calling webhook:", error); } - } // Run the test -testWebhook().catch(console.error); +testWebhook().catch((err: unknown) => console.error(err)); \ No newline at end of file