Skip to content

Commit

Permalink
feat: call callback url after review
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovicentegc committed Nov 5, 2023
1 parent a09c2a4 commit bc085d0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/api/data/completions/review/route.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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), {
Expand Down

0 comments on commit bc085d0

Please sign in to comment.