payemnt of paitientss - table added
This commit is contained in:
@@ -94,26 +94,30 @@ router.get(
|
||||
"/patient/:patientId",
|
||||
async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
const patientIdParam = req.params.patientId;
|
||||
if (!patientIdParam) {
|
||||
return res.status(400).json({ message: "Missing patientId" });
|
||||
}
|
||||
const patientId = parseInt(patientIdParam);
|
||||
if (isNaN(patientId)) {
|
||||
return res.status(400).json({ message: "Invalid patientId" });
|
||||
}
|
||||
const limit = parseInt(req.query.limit as string) || 10;
|
||||
const offset = parseInt(req.query.offset as string) || 0;
|
||||
|
||||
const parsedPatientId = parseIntOrError(
|
||||
req.params.patientId,
|
||||
"Patient ID"
|
||||
);
|
||||
if (isNaN(patientId)) {
|
||||
return res.status(400).json({ message: "Invalid patient ID" });
|
||||
}
|
||||
|
||||
const payments = await storage.getPaymentsByPatientId(
|
||||
parsedPatientId,
|
||||
userId
|
||||
);
|
||||
const [payments, totalCount] = await Promise.all([
|
||||
storage.getRecentPaymentsByPatientId(patientId, limit, offset),
|
||||
storage.getTotalPaymentCountByPatient(patientId),
|
||||
]);
|
||||
|
||||
if (!payments)
|
||||
return res.status(404).json({ message: "No payments found for claim" });
|
||||
|
||||
res.status(200).json(payments);
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch patient payments:", err);
|
||||
res.status(500).json({ message: "Failed to fetch patient payments" });
|
||||
res.json({ payments, totalCount });
|
||||
} catch (error) {
|
||||
console.error("Failed to retrieve payments for patient:", error);
|
||||
res.status(500).json({ message: "Failed to retrieve patient payments" });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -183,14 +183,16 @@ export interface IStorage {
|
||||
): Promise<Payment>;
|
||||
deletePayment(id: number, userId: number): Promise<void>;
|
||||
getPaymentById(id: number, userId: number): Promise<PaymentWithExtras | null>;
|
||||
getRecentPaymentsByPatientId(
|
||||
patientId: number,
|
||||
limit: number,
|
||||
offset: number
|
||||
): Promise<PaymentWithExtras[] | null>;
|
||||
getTotalPaymentCountByPatient(patientId: number): Promise<number>;
|
||||
getPaymentsByClaimId(
|
||||
claimId: number,
|
||||
userId: number
|
||||
): Promise<PaymentWithExtras | null>;
|
||||
getPaymentsByPatientId(
|
||||
patientId: number,
|
||||
userId: number
|
||||
): Promise<PaymentWithExtras[]>;
|
||||
getRecentPaymentsByUser(
|
||||
userId: number,
|
||||
limit: number,
|
||||
@@ -762,6 +764,45 @@ export const storage: IStorage = {
|
||||
await db.payment.delete({ where: { id } });
|
||||
},
|
||||
|
||||
async getRecentPaymentsByPatientId(
|
||||
patientId: number,
|
||||
limit: number,
|
||||
offset: number
|
||||
): Promise<PaymentWithExtras[]> {
|
||||
const payments = await db.payment.findMany({
|
||||
where: { claim: { patientId } },
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip: offset,
|
||||
take: limit,
|
||||
include: {
|
||||
claim: {
|
||||
include: {
|
||||
serviceLines: true,
|
||||
},
|
||||
},
|
||||
serviceLineTransactions: {
|
||||
include: {
|
||||
serviceLine: true,
|
||||
},
|
||||
},
|
||||
updatedBy: true,
|
||||
},
|
||||
});
|
||||
|
||||
return payments.map((payment) => ({
|
||||
...payment,
|
||||
patientName: payment.claim?.patientName ?? "",
|
||||
paymentDate: payment.createdAt,
|
||||
paymentMethod: payment.serviceLineTransactions[0]?.method ?? "OTHER",
|
||||
}));
|
||||
},
|
||||
|
||||
async getTotalPaymentCountByPatient(patientId: number): Promise<number> {
|
||||
return db.payment.count({
|
||||
where: { claim: { patientId } },
|
||||
});
|
||||
},
|
||||
|
||||
async getPaymentById(
|
||||
id: number,
|
||||
userId: number
|
||||
@@ -824,35 +865,6 @@ export const storage: IStorage = {
|
||||
};
|
||||
},
|
||||
|
||||
async getPaymentsByPatientId(
|
||||
patientId: number,
|
||||
userId: number
|
||||
): Promise<PaymentWithExtras[]> {
|
||||
const payments = await db.payment.findMany({
|
||||
where: { patientId, userId },
|
||||
include: {
|
||||
claim: {
|
||||
include: {
|
||||
serviceLines: true,
|
||||
},
|
||||
},
|
||||
serviceLineTransactions: {
|
||||
include: {
|
||||
serviceLine: true,
|
||||
},
|
||||
},
|
||||
updatedBy: true,
|
||||
},
|
||||
});
|
||||
|
||||
return payments.map((payment) => ({
|
||||
...payment,
|
||||
patientName: payment.claim?.patientName ?? "",
|
||||
paymentDate: payment.createdAt,
|
||||
paymentMethod: payment.serviceLineTransactions[0]?.method ?? "OTHER",
|
||||
}));
|
||||
},
|
||||
|
||||
async getRecentPaymentsByUser(
|
||||
userId: number,
|
||||
limit: number,
|
||||
|
||||
Reference in New Issue
Block a user