Add test endpoint for verifying email configuration
All checks were successful
Deploy / deploy (push) Successful in 2m46s

POST /api/test/email with {"to": "email@example.com"} to send a test email.
Protected by CRON_SECRET.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-20 22:22:19 +00:00
parent 140de56450
commit 9c4fcd8b52

View File

@@ -0,0 +1,59 @@
// ABOUTME: Test endpoint for verifying email configuration.
// ABOUTME: Sends a test email to verify Mailgun integration works.
import { NextResponse } from "next/server";
import type { DailyEmailData } from "@/lib/email";
import { sendDailyEmail } from "@/lib/email";
export async function POST(request: Request) {
// Verify cron secret (reuse same auth as cron endpoints)
const authHeader = request.headers.get("authorization");
const expectedSecret = process.env.CRON_SECRET;
if (!expectedSecret || authHeader !== `Bearer ${expectedSecret}`) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await request.json().catch(() => ({}));
const to = body.to as string;
if (!to) {
return NextResponse.json(
{ error: "Missing 'to' email address in request body" },
{ status: 400 },
);
}
const testData: DailyEmailData = {
to,
cycleDay: 15,
phase: "OVULATION",
decision: {
status: "TRAIN",
reason: "This is a test email to verify Mailgun configuration works!",
icon: "🧪",
},
bodyBatteryCurrent: 85,
bodyBatteryYesterdayLow: 45,
hrvStatus: "Balanced",
weekIntensity: 60,
phaseLimit: 80,
remainingMinutes: 20,
seeds: "Sesame (1-2 tbsp) + Sunflower (1-2 tbsp)",
carbRange: "100-150g",
ketoGuidance: "No - exit keto, need carbs for ovulation",
};
try {
await sendDailyEmail(testData, "test-user");
return NextResponse.json({
success: true,
message: `Test email sent to ${to}`,
});
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error";
return NextResponse.json(
{ success: false, error: message },
{ status: 500 },
);
}
}