Update scripts/test-resend-webhook.ts
This commit is contained in:
@@ -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));
|
||||
Reference in New Issue
Block a user