Spaces:
Sleeping
Sleeping
| import { NextResponse } from "next/server"; | |
| export const runtime = "nodejs"; | |
| export async function POST(req: Request) { | |
| try { | |
| const input = await req.json(); | |
| const webhookUrl = process.env.RESULTS_WEBHOOK_URL; | |
| const webhookToken = process.env.RESULTS_WEBHOOK_TOKEN; | |
| if (!webhookUrl || !webhookToken) { | |
| return NextResponse.json( | |
| { ok: false, error: "Missing RESULTS_WEBHOOK_URL or RESULTS_WEBHOOK_TOKEN in Space Secrets." }, | |
| { status: 500 } | |
| ); | |
| } | |
| // Forward full payload to Google Apps Script. | |
| // Apps Script will: | |
| // 1) Append a row to Google Sheet | |
| // 2) Generate a PDF report | |
| // 3) Save to Drive (optional folder) | |
| // 4) Write back the PDF URL into the sheet row (and/or return it) | |
| const r = await fetch(webhookUrl, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| token: webhookToken, | |
| ...input | |
| }) | |
| }); | |
| const txt = await r.text(); | |
| return NextResponse.json({ ok: r.ok, status: r.status, response: txt.slice(0, 4000) }); | |
| } catch (e: any) { | |
| return NextResponse.json({ ok: false, error: e?.message ?? "Server error" }, { status: 500 }); | |
| } | |
| } | |