payment table - rows 1st checkpoint

This commit is contained in:
2025-08-02 14:24:44 +05:30
parent 5810328711
commit a9a93b1c00
6 changed files with 1762 additions and 632 deletions

View File

@@ -7,7 +7,8 @@ import multer from "multer";
import { forwardToSeleniumClaimAgent } from "../services/seleniumClaimClient";
import path from "path";
import axios from "axios";
import fs from "fs";
import { Prisma } from "@repo/db/generated/prisma";
import { Decimal } from "@prisma/client/runtime/library";
const router = Router();
@@ -284,8 +285,36 @@ router.post("/", async (req: Request, res: Response): Promise<any> => {
userId: req.user!.id,
});
const newClaim = await storage.createClaim(parsedClaim);
res.status(201).json(newClaim);
// Step 1: Calculate total billed from service lines
const serviceLinesCreateInput = (
parsedClaim.serviceLines as Prisma.ServiceLineCreateNestedManyWithoutClaimInput
)?.create;
const lines = Array.isArray(serviceLinesCreateInput)
? (serviceLinesCreateInput as unknown as { amount: number }[])
: [];
const totalBilled = lines.reduce(
(sum, line) => sum + (line.amount ?? 0),
0
);
// Step 2: Create claim (with service lines)
const claim = await storage.createClaim(parsedClaim);
// Step 3: Create empty payment
await storage.createPayment({
patientId: claim.patientId,
userId: req.user!.id,
claimId: claim.id,
totalBilled: new Decimal(totalBilled),
totalPaid: new Decimal(0),
totalDue: new Decimal(totalBilled),
status: "PENDING",
notes: null,
paymentMethod: null,
receivedDate: null,
});
res.status(201).json(claim);
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(400).json({

View File

@@ -38,13 +38,7 @@ const updateAppointmentSchema = (
type UpdateAppointment = z.infer<typeof updateAppointmentSchema>;
//patient types
const PatientSchema = (
PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
).omit({
appointments: true,
});
type Patient = z.infer<typeof PatientUncheckedCreateInputObjectSchema>;
type Patient2 = z.infer<typeof PatientSchema>;
const insertPatientSchema = (
PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
@@ -196,9 +190,17 @@ type PaymentWithExtras = Prisma.PaymentGetPayload<{
include: {
transactions: true;
servicePayments: true;
claim: true;
claim: {
include: {
serviceLines: true;
};
};
};
}>;
}> & {
patientName: string;
paymentDate: Date;
paymentMethod: string;
};
export interface IStorage {
// User methods
@@ -942,45 +944,79 @@ export const storage: IStorage = {
id: number,
userId: number
): Promise<PaymentWithExtras | null> {
return db.payment.findFirst({
const payment = await db.payment.findFirst({
where: { id, userId },
include: {
claim: true,
claim: {
include: {
serviceLines: true,
},
},
transactions: true,
servicePayments: true,
},
});
if (!payment) return null;
return {
...payment,
patientName: payment.claim?.patientName ?? "",
paymentDate: payment.createdAt,
paymentMethod: payment.transactions[0]?.method ?? "OTHER",
};
},
async getPaymentsByClaimId(
claimId: number,
userId: number
): Promise<PaymentWithExtras | null> {
return db.payment.findFirst({
const payment = await db.payment.findFirst({
where: { claimId, userId },
include: {
claim: true,
claim: {
include: {
serviceLines: true,
},
},
transactions: true,
servicePayments: true,
},
});
if (!payment) return null;
return {
...payment,
patientName: payment.claim?.patientName ?? "",
paymentDate: payment.createdAt,
paymentMethod: payment.transactions[0]?.method ?? "OTHER",
};
},
async getPaymentsByPatientId(
patientId: number,
userId: number
): Promise<PaymentWithExtras[]> {
return db.payment.findMany({
where: {
patientId,
userId,
},
const payments = await db.payment.findMany({
where: { patientId, userId },
include: {
claim: true,
claim: {
include: {
serviceLines: true,
},
},
transactions: true,
servicePayments: true,
},
});
return payments.map((payment) => ({
...payment,
patientName: payment.claim?.patientName ?? "",
paymentDate: payment.createdAt,
paymentMethod: payment.transactions[0]?.method ?? "OTHER",
}));
},
async getRecentPaymentsByUser(
@@ -988,17 +1024,28 @@ export const storage: IStorage = {
limit: number,
offset: number
): Promise<PaymentWithExtras[]> {
return db.payment.findMany({
const payments = await db.payment.findMany({
where: { userId },
orderBy: { createdAt: "desc" },
skip: offset,
take: limit,
include: {
claim: true,
claim: {
include: {
serviceLines: true,
},
},
transactions: true,
servicePayments: true,
},
});
return payments.map((payment) => ({
...payment,
patientName: payment.claim?.patientName ?? "",
paymentDate: payment.createdAt,
paymentMethod: payment.transactions[0]?.method ?? "OTHER",
}));
},
async getPaymentsByDateRange(
@@ -1006,7 +1053,7 @@ export const storage: IStorage = {
from: Date,
to: Date
): Promise<PaymentWithExtras[]> {
return db.payment.findMany({
const payments = await db.payment.findMany({
where: {
userId,
createdAt: {
@@ -1016,11 +1063,22 @@ export const storage: IStorage = {
},
orderBy: { createdAt: "desc" },
include: {
claim: true,
claim: {
include: {
serviceLines: true,
},
},
transactions: true,
servicePayments: true,
},
});
return payments.map((payment) => ({
...payment,
patientName: payment.claim?.patientName ?? "",
paymentDate: payment.createdAt,
paymentMethod: payment.transactions[0]?.method ?? "OTHER",
}));
},
async getTotalPaymentCountByUser(userId: number): Promise<number> {