-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completion of storing transcations in db
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |