From 70c8c34e8e4a7349e587998740de7ce16c95d103 Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Wed, 5 Jun 2024 23:12:18 +0530 Subject: [PATCH] Completion of storing transcations in db --- app/api/transactions/route.ts | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 app/api/transactions/route.ts diff --git a/app/api/transactions/route.ts b/app/api/transactions/route.ts new file mode 100644 index 00000000..349365e4 --- /dev/null +++ b/app/api/transactions/route.ts @@ -0,0 +1,50 @@ +import dbConfig from "@lib/db"; +import { Transaction } from "@/types"; +import { ObjectId } from "mongodb"; + +// saving transaction details in db +export async function POST(req: Request) { + const session = req.headers.get("Authorization"); + if (!session) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); + } + + try { + const { + transaction_id, + timestamp, + patient_id, + hospital_id, + disease, + description, + amount, + status, + }: Transaction = await req.json(); + + const db = await dbConfig(); + const transaction_collection = db.collection("transactions"); + + const transactionData = { + transaction_id, + timestamp, + patient: new ObjectId(patient_id), + hospital: new ObjectId(hospital_id), + disease, + description, + amount, + status, + }; + + const res = await transaction_collection.insertOne(transactionData); + + if (!res) + return Response.json({ + error: "Error saving transaction details", + }); + + return Response.json({ status: 200 }); + } catch (error) { + console.error("Error saving transaction :", error); + return Response.json({ error: "Internal Server Error" }, { status: 500 }); + } +}