Skip to content

Commit

Permalink
Completion of storing transcations in db
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Jun 5, 2024
1 parent a284ec5 commit 70c8c34
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app/api/transactions/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}

0 comments on commit 70c8c34

Please sign in to comment.