From bc085d0b2370c108035926ed28e67ea115be7ed5 Mon Sep 17 00:00:00 2001 From: marcelovicentegc Date: Sun, 5 Nov 2023 11:58:52 -0300 Subject: [PATCH] feat: call callback url after review --- app/api/data/completions/review/route.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/api/data/completions/review/route.js b/app/api/data/completions/review/route.js index b4ba631..c228f2f 100644 --- a/app/api/data/completions/review/route.js +++ b/app/api/data/completions/review/route.js @@ -1,4 +1,6 @@ import { authOptions } from "@/app/api/auth/[...nextauth]/route"; +import { CHIRON_VENDOR_ID } from "@/lib/config"; +import { getApiKey } from "@/lib/db/reads"; import { reviewCompletion } from "@/lib/db/writes"; import { getServerSession } from "next-auth"; import { NextResponse } from "next/server"; @@ -36,7 +38,26 @@ export async function POST(req) { const result = await reviewCompletion(data, direction); if (result?.acknowledged || result?.insertedId) { - // TODO: Call the vendor's webhook based on the result property + const vendorId = data[CHIRON_VENDOR_ID]; + const apiKey = await getApiKey(vendorId); + + const { vendorCallbackUrl } = apiKey; + + try { + await fetch(vendorCallbackUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + } catch (error) { + console.error(error); + + return new NextResponse(JSON.stringify("Internal Server Error"), { + status: 500, + }); + } } return new NextResponse(JSON.stringify(result), {