File size: 1,220 Bytes
7f9f518
 
 
 
 
 
 
 
 
 
 
 
20da2be
 
 
 
7f9f518
 
999451c
 
 
 
 
 
 
7f9f518
20da2be
999451c
 
 
 
7f9f518
 
999451c
 
7f9f518
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 });
  }
}