echos-ocr/src/app/api/email-watch/test/route.ts
Randall Stillwell 41000337e3 Add IMAP email monitoring for scanned card attachments
Watches an email inbox via IMAP IDLE for incoming scanned cards,
extracts PDF/image attachments, and feeds them through the existing
OCR pipeline. Configurable via the Settings page with test connection
support.

Made-with: Cursor
2026-04-07 11:41:52 -05:00

41 lines
1.3 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { testEmailConnection } from "@/lib/email-watcher";
export async function POST(request: NextRequest) {
try {
const body = await request.json().catch(() => ({}));
const host = String(body.host || "");
const port = parseInt(String(body.port)) || 993;
const user = String(body.user || "");
const pass = String(body.pass || "");
const tls = body.tls !== false;
if (!host || !user || !pass) {
return NextResponse.json(
{ error: "Host, username, and password are required" },
{ status: 400 }
);
}
const result = await testEmailConnection({ host, port, user, pass, tls, folder: "INBOX" });
if (result.ok) {
return NextResponse.json({
ok: true,
folders: result.folders,
message: `Connected to ${host}${result.folders?.length || 0} folder(s) found`,
});
} else {
return NextResponse.json(
{ ok: false, error: result.error },
{ status: 400 }
);
}
} catch (error) {
console.error("[email-watch/test POST]", error);
const message =
error instanceof Error ? error.message : "Connection test failed";
return NextResponse.json({ ok: false, error: message }, { status: 500 });
}
}