From 092201971b0309b1cba333a4b87d064fd541eb60 Mon Sep 17 00:00:00 2001 From: Potenz Date: Mon, 11 Aug 2025 01:38:13 +0530 Subject: [PATCH] updated bcz of payment --- apps/Backend/src/routes/claims.ts | 56 +++++++++++-------------------- packages/db/types/claim-types.ts | 8 +++++ 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/apps/Backend/src/routes/claims.ts b/apps/Backend/src/routes/claims.ts index 183d019..84cc242 100644 --- a/apps/Backend/src/routes/claims.ts +++ b/apps/Backend/src/routes/claims.ts @@ -2,46 +2,20 @@ import { Router } from "express"; import { Request, Response } from "express"; import { storage } from "../storage"; import { z } from "zod"; -import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas"; import multer from "multer"; import { forwardToSeleniumClaimAgent } from "../services/seleniumClaimClient"; import path from "path"; import axios from "axios"; import { Prisma } from "@repo/db/generated/prisma"; import { Decimal } from "@prisma/client/runtime/library"; +import { + ExtendedClaimSchema, + InputServiceLine, + updateClaimSchema, +} from "@repo/db/types"; const router = Router(); -// Define Zod schemas -const ClaimSchema = ( - ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject -).omit({ - id: true, - createdAt: true, - updatedAt: true, -}); - -type InsertClaim = z.infer; - -const updateClaimSchema = ( - ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject -) - .omit({ - id: true, - createdAt: true, - updatedAt: true, - }) - .partial(); - -type UpdateClaim = z.infer; - -// Extend the schema to inject `userId` manually (since it's not passed by the client) -const ExtendedClaimSchema = ( - ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject -).extend({ - userId: z.number(), -}); - // Routes const multerStorage = multer.memoryStorage(); // NO DISK const upload = multer({ @@ -277,6 +251,14 @@ router.get("/:id", async (req: Request, res: Response): Promise => { router.post("/", async (req: Request, res: Response): Promise => { try { if (Array.isArray(req.body.serviceLines)) { + req.body.serviceLines = req.body.serviceLines.map( + (line: InputServiceLine) => ({ + ...line, + totalBilled: Number(line.totalBilled), + totalAdjusted: Number(line.totalAdjusted), + totalPaid: Number(line.totalPaid), + }) + ); req.body.serviceLines = { create: req.body.serviceLines }; } @@ -290,10 +272,12 @@ router.post("/", async (req: Request, res: Response): Promise => { parsedClaim.serviceLines as Prisma.ServiceLineCreateNestedManyWithoutClaimInput )?.create; const lines = Array.isArray(serviceLinesCreateInput) - ? (serviceLinesCreateInput as unknown as { amount: number }[]) + ? (serviceLinesCreateInput as unknown as { + totalBilled: number | string; + }[]) : []; const totalBilled = lines.reduce( - (sum, line) => sum + (line.amount ?? 0), + (sum, line) => sum + Number(line.totalBilled ?? 0), 0 ); @@ -302,16 +286,14 @@ router.post("/", async (req: Request, res: Response): Promise => { // Step 3: Create empty payment await storage.createPayment({ + claimId: claim.id, 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, + notes: "", }); res.status(201).json(claim); diff --git a/packages/db/types/claim-types.ts b/packages/db/types/claim-types.ts index 5225a6c..28c2ee1 100644 --- a/packages/db/types/claim-types.ts +++ b/packages/db/types/claim-types.ts @@ -21,8 +21,16 @@ export const updateClaimSchema = ( updatedAt: true, }) .partial(); + export type UpdateClaim = z.infer; +// Extend the schema to inject `userId` manually (since it's not passed by the client) +export const ExtendedClaimSchema = ( + ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject +).extend({ + userId: z.number(), +}); + export type Claim = z.infer; export type ClaimStatus = z.infer;