Skip to content

Commit

Permalink
feat : add transactions api to get all transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Aug 8, 2024
1 parent 47ebc12 commit ca91a75
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions app/api/admin/transactions/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import dbConfig from "@utils/db";
import { decrypt } from "@sessions/sessionUtils";
import { Admin, Hospital, Patient, Transaction } from "@models/index";
import { TransactionDetails } from "@types";

export async function GET(request: Request) {
const session = request.headers.get("Authorization");
if (!session) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

try {
const token = session.split("Bearer ")[1];
const decryptedUser = await decrypt(token);
const email = decryptedUser.user.email;

await dbConfig();

const adminData = await Admin.findOne({ email });

if (!adminData) {
return Response.json({ error: "Admin not found" }, { status: 404 });
}

const transactions = await Transaction.find({}).sort({ createdAt: -1 });

const transactionDetails: TransactionDetails[] = await Promise.all(
transactions.map(async (transaction) => {
const patient = await Patient.findById(transaction.patient);
const hospital = await Hospital.findById(transaction.hospital);

return {
transaction_id: transaction.transaction_id,
patient: {
name: `${patient?.firstname} ${patient?.lastname}` || "",
profile: patient?.profile || "",
},

hospital: {
name: `${hospital?.firstname} ${hospital?.lastname}` || "",
profile: hospital?.profile || "",
},
disease: transaction.disease,
description: transaction.description,
amount: transaction.amount,
status: transaction.status,
date: transaction.createdAt.toISOString(),
};
})
);

return Response.json(transactionDetails);
} catch (error) {
console.error("Error fetching Transaction data:", error);
return Response.json({ error: "Internal Server Error" }, { status: 500 });
}
}

0 comments on commit ca91a75

Please sign in to comment.