Spaces:
Sleeping
Sleeping
| import { NextRequest, NextResponse } from "next/server"; | |
| import { Resend } from "resend"; | |
| const resend = new Resend(process.env.RESEND_API_KEY); | |
| export async function POST(req: NextRequest) { | |
| try { | |
| const { email, risk_score, grade, flagged_count, total_clauses, source_url } = await req.json(); | |
| if (!email) { | |
| return NextResponse.json({ error: "Email required" }, { status: 400 }); | |
| } | |
| const gradeColor = grade === "F" || grade === "D" ? "#b91c1c" : grade === "C" ? "#a16207" : "#15803d"; | |
| const gradeBg = grade === "F" || grade === "D" ? "#fef2f2" : grade === "C" ? "#fffbeb" : "#f0fdf4"; | |
| const { data, error } = await resend.emails.send({ | |
| from: "ClauseGuard <reports@clauseguardweb.netlify.app>", | |
| to: [email], | |
| subject: `Scan complete — Grade ${grade} (${risk_score}/100 risk)`, | |
| html: ` | |
| <div style="font-family:system-ui,sans-serif;max-width:480px;margin:0 auto;padding:40px 20px;"> | |
| <p style="font-size:12px;color:#a1a1aa;margin:0 0 8px;">ClauseGuard Scan Report</p> | |
| <h1 style="font-size:20px;font-weight:600;color:#18181b;margin:0 0 20px;"> | |
| ${source_url ? new URL(source_url).hostname : "Document"} — Risk ${risk_score}/100 | |
| </h1> | |
| <div style="display:flex;gap:12px;margin-bottom:24px;"> | |
| <div style="background:${gradeBg};color:${gradeColor};padding:12px 20px;border-radius:8px;text-align:center;flex:1;"> | |
| <div style="font-size:24px;font-weight:700;">Grade ${grade}</div> | |
| </div> | |
| <div style="background:#f4f4f5;padding:12px 20px;border-radius:8px;text-align:center;flex:1;"> | |
| <div style="font-size:24px;font-weight:700;">${flagged_count}</div> | |
| <div style="font-size:11px;color:#71717a;">of ${total_clauses} flagged</div> | |
| </div> | |
| </div> | |
| <a href="https://clauseguardweb.netlify.app/dashboard-pages/dashboard" | |
| style="display:inline-block;background:#18181b;color:#fff;padding:10px 20px;border-radius:6px;text-decoration:none;font-size:14px;font-weight:500;"> | |
| View full report | |
| </a> | |
| <p style="font-size:12px;color:#a1a1aa;margin-top:32px;border-top:1px solid #f4f4f5;padding-top:16px;"> | |
| clauseguardweb.netlify.app — Not legal advice. | |
| </p> | |
| </div> | |
| `, | |
| }); | |
| if (error) { | |
| return NextResponse.json({ error: error.message }, { status: 500 }); | |
| } | |
| return NextResponse.json({ id: data?.id }); | |
| } catch (error) { | |
| console.error("Email error:", error); | |
| return NextResponse.json({ error: "Failed to send email" }, { status: 500 }); | |
| } | |
| } | |