From c6dec2b56c8eab88a1d3919839e7b63a6417e5fc Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 13 Oct 2023 00:22:27 +0800 Subject: [PATCH] added api to trigger eco spreadsheet --- src/app/api/eco-spreadsheet/route.ts | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/app/api/eco-spreadsheet/route.ts diff --git a/src/app/api/eco-spreadsheet/route.ts b/src/app/api/eco-spreadsheet/route.ts new file mode 100644 index 0000000..fb94eb9 --- /dev/null +++ b/src/app/api/eco-spreadsheet/route.ts @@ -0,0 +1,56 @@ +import { NextRequest, NextResponse } from "next/server"; +import { z } from "zod"; + +const BodySchema = z.object({ + folderKey: z.string(), + userId: z.string(), +}); + +export async function POST(request: NextRequest) { + const body = await request.json(); + let validatedBody; + + try { + validatedBody = BodySchema.parse(body); + } catch (error) { + console.error("Invalid input: ", error); + return new NextResponse(JSON.stringify({ msg: "Invalid input" }), { + status: 400, + }); + } + + try { + // We'll trigger the plan build in our fastapi backend and then the user will just have to + // wait for the plan to be ready + + const headers = { + "Content-Type": "application/json", + }; + + console.log("validatedBody", validatedBody); + + const response = await fetch( + `${process.env.ECO_SPREADSHEET_API_URL}/eco-spreadsheet`, + { + method: "POST", + headers: headers, + body: JSON.stringify(validatedBody), + } + ); + + if (!response.ok) { + throw new Error("API request failed"); + } + + const responseData = await response.json(); + + return new NextResponse(JSON.stringify(responseData), { + status: 200, + }); + } catch (error) { + console.error(error); + return new NextResponse(JSON.stringify({ msg: "Internal server error" }), { + status: 500, + }); + } +}