validation added in route
This commit is contained in:
@@ -74,10 +74,7 @@ router.get(
|
|||||||
|
|
||||||
const parsedClaimId = parseIntOrError(req.params.claimId, "Claim ID");
|
const parsedClaimId = parseIntOrError(req.params.claimId, "Claim ID");
|
||||||
|
|
||||||
const payments = await storage.getPaymentsByClaimId(
|
const payments = await storage.getPaymentsByClaimId(parsedClaimId);
|
||||||
parsedClaimId,
|
|
||||||
userId
|
|
||||||
);
|
|
||||||
if (!payments)
|
if (!payments)
|
||||||
return res.status(404).json({ message: "No payments found for claim" });
|
return res.status(404).json({ message: "No payments found for claim" });
|
||||||
|
|
||||||
@@ -138,7 +135,6 @@ router.get("/filter", async (req: Request, res: Response): Promise<any> => {
|
|||||||
|
|
||||||
const { from, to } = validated.data;
|
const { from, to } = validated.data;
|
||||||
const payments = await storage.getPaymentsByDateRange(
|
const payments = await storage.getPaymentsByDateRange(
|
||||||
userId,
|
|
||||||
new Date(from),
|
new Date(from),
|
||||||
new Date(to)
|
new Date(to)
|
||||||
);
|
);
|
||||||
@@ -157,7 +153,7 @@ router.get("/:id", async (req: Request, res: Response): Promise<any> => {
|
|||||||
|
|
||||||
const id = parseIntOrError(req.params.id, "Payment ID");
|
const id = parseIntOrError(req.params.id, "Payment ID");
|
||||||
|
|
||||||
const payment = await storage.getPaymentById(id, userId);
|
const payment = await storage.getPaymentById(id);
|
||||||
if (!payment) return res.status(404).json({ message: "Payment not found" });
|
if (!payment) return res.status(404).json({ message: "Payment not found" });
|
||||||
|
|
||||||
res.status(200).json(payment);
|
res.status(200).json(payment);
|
||||||
@@ -205,6 +201,9 @@ router.put("/:id", async (req: Request, res: Response): Promise<any> => {
|
|||||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||||
|
|
||||||
const paymentId = parseIntOrError(req.params.id, "Payment ID");
|
const paymentId = parseIntOrError(req.params.id, "Payment ID");
|
||||||
|
const paymentRecord = await storage.getPaymentById(paymentId);
|
||||||
|
if (!paymentRecord)
|
||||||
|
return res.status(404).json({ message: "Payment not found" });
|
||||||
|
|
||||||
const validated = newTransactionPayloadSchema.safeParse(
|
const validated = newTransactionPayloadSchema.safeParse(
|
||||||
req.body.data as NewTransactionPayload
|
req.body.data as NewTransactionPayload
|
||||||
@@ -218,6 +217,33 @@ router.put("/:id", async (req: Request, res: Response): Promise<any> => {
|
|||||||
|
|
||||||
const { status, serviceLineTransactions } = validated.data;
|
const { status, serviceLineTransactions } = validated.data;
|
||||||
|
|
||||||
|
// validation if req is valid
|
||||||
|
for (const txn of serviceLineTransactions) {
|
||||||
|
const line = paymentRecord.claim.serviceLines.find(
|
||||||
|
(sl) => sl.id === txn.serviceLineId
|
||||||
|
);
|
||||||
|
if (!line)
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: `Invalid service line: ${txn.serviceLineId}` });
|
||||||
|
|
||||||
|
const paidAmount = new Decimal(txn.paidAmount ?? 0);
|
||||||
|
const adjustedAmount = new Decimal(txn.adjustedAmount ?? 0);
|
||||||
|
if (paidAmount.lt(0) || adjustedAmount.lt(0)) {
|
||||||
|
return res.status(400).json({ message: "Amounts cannot be negative" });
|
||||||
|
}
|
||||||
|
if (paidAmount.eq(0) && adjustedAmount.eq(0)) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Must provide a payment or adjustment" });
|
||||||
|
}
|
||||||
|
if (paidAmount.gt(line.totalDue)) {
|
||||||
|
return res.status(400).json({
|
||||||
|
message: `Paid amount exceeds due for service line ${txn.serviceLineId}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Wrap everything in a transaction
|
// Wrap everything in a transaction
|
||||||
const result = await prisma.$transaction(async (tx) => {
|
const result = await prisma.$transaction(async (tx) => {
|
||||||
// 1. Create all new service line transactions
|
// 1. Create all new service line transactions
|
||||||
|
|||||||
@@ -179,10 +179,9 @@ export interface IStorage {
|
|||||||
updatePayment(
|
updatePayment(
|
||||||
id: number,
|
id: number,
|
||||||
updates: UpdatePayment,
|
updates: UpdatePayment,
|
||||||
userId: number
|
|
||||||
): Promise<Payment>;
|
): Promise<Payment>;
|
||||||
deletePayment(id: number, userId: number): Promise<void>;
|
deletePayment(id: number, userId: number): Promise<void>;
|
||||||
getPaymentById(id: number, userId: number): Promise<PaymentWithExtras | null>;
|
getPaymentById(id: number): Promise<PaymentWithExtras | null>;
|
||||||
getRecentPaymentsByPatientId(
|
getRecentPaymentsByPatientId(
|
||||||
patientId: number,
|
patientId: number,
|
||||||
limit: number,
|
limit: number,
|
||||||
@@ -191,7 +190,6 @@ export interface IStorage {
|
|||||||
getTotalPaymentCountByPatient(patientId: number): Promise<number>;
|
getTotalPaymentCountByPatient(patientId: number): Promise<number>;
|
||||||
getPaymentsByClaimId(
|
getPaymentsByClaimId(
|
||||||
claimId: number,
|
claimId: number,
|
||||||
userId: number
|
|
||||||
): Promise<PaymentWithExtras | null>;
|
): Promise<PaymentWithExtras | null>;
|
||||||
getRecentPaymentsByUser(
|
getRecentPaymentsByUser(
|
||||||
userId: number,
|
userId: number,
|
||||||
@@ -199,7 +197,6 @@ export interface IStorage {
|
|||||||
offset: number
|
offset: number
|
||||||
): Promise<PaymentWithExtras[]>;
|
): Promise<PaymentWithExtras[]>;
|
||||||
getPaymentsByDateRange(
|
getPaymentsByDateRange(
|
||||||
userId: number,
|
|
||||||
from: Date,
|
from: Date,
|
||||||
to: Date
|
to: Date
|
||||||
): Promise<PaymentWithExtras[]>;
|
): Promise<PaymentWithExtras[]>;
|
||||||
@@ -742,11 +739,10 @@ export const storage: IStorage = {
|
|||||||
async updatePayment(
|
async updatePayment(
|
||||||
id: number,
|
id: number,
|
||||||
updates: UpdatePayment,
|
updates: UpdatePayment,
|
||||||
userId: number
|
|
||||||
): Promise<Payment> {
|
): Promise<Payment> {
|
||||||
const existing = await db.payment.findFirst({ where: { id, userId } });
|
const existing = await db.payment.findFirst({ where: { id } });
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
throw new Error("Not authorized or payment not found");
|
throw new Error("Payment not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.payment.update({
|
return db.payment.update({
|
||||||
@@ -805,10 +801,9 @@ export const storage: IStorage = {
|
|||||||
|
|
||||||
async getPaymentById(
|
async getPaymentById(
|
||||||
id: number,
|
id: number,
|
||||||
userId: number
|
|
||||||
): Promise<PaymentWithExtras | null> {
|
): Promise<PaymentWithExtras | null> {
|
||||||
const payment = await db.payment.findFirst({
|
const payment = await db.payment.findFirst({
|
||||||
where: { id, userId },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
claim: {
|
claim: {
|
||||||
include: {
|
include: {
|
||||||
@@ -836,10 +831,9 @@ export const storage: IStorage = {
|
|||||||
|
|
||||||
async getPaymentsByClaimId(
|
async getPaymentsByClaimId(
|
||||||
claimId: number,
|
claimId: number,
|
||||||
userId: number
|
|
||||||
): Promise<PaymentWithExtras | null> {
|
): Promise<PaymentWithExtras | null> {
|
||||||
const payment = await db.payment.findFirst({
|
const payment = await db.payment.findFirst({
|
||||||
where: { claimId, userId },
|
where: { claimId },
|
||||||
include: {
|
include: {
|
||||||
claim: {
|
claim: {
|
||||||
include: {
|
include: {
|
||||||
@@ -899,13 +893,11 @@ export const storage: IStorage = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getPaymentsByDateRange(
|
async getPaymentsByDateRange(
|
||||||
userId: number,
|
|
||||||
from: Date,
|
from: Date,
|
||||||
to: Date
|
to: Date
|
||||||
): Promise<PaymentWithExtras[]> {
|
): Promise<PaymentWithExtras[]> {
|
||||||
const payments = await db.payment.findMany({
|
const payments = await db.payment.findMany({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
|
||||||
createdAt: {
|
createdAt: {
|
||||||
gte: from,
|
gte: from,
|
||||||
lte: to,
|
lte: to,
|
||||||
|
|||||||
Reference in New Issue
Block a user