From 9c4fcd8b5216381ac3960d712d9f6707b0ec6faf Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 20 Jan 2026 22:22:19 +0000 Subject: [PATCH] Add test endpoint for verifying email configuration 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 --- src/app/api/test/email/route.ts | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/app/api/test/email/route.ts diff --git a/src/app/api/test/email/route.ts b/src/app/api/test/email/route.ts new file mode 100644 index 0000000..45b9869 --- /dev/null +++ b/src/app/api/test/email/route.ts @@ -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 }, + ); + } +}