claim routes connection done

This commit is contained in:
2025-05-31 23:13:04 +05:30
parent 3faef34917
commit aa38ddc00c
3 changed files with 152 additions and 116 deletions

View File

@@ -2,9 +2,7 @@ import { Router } from "express";
import { Request, Response } from "express";
import { storage } from "../storage";
import { z } from "zod";
import {
ClaimUncheckedCreateInputObjectSchema,
} from "@repo/db/usedSchemas";
import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
const router = Router();
@@ -31,6 +29,13 @@ const updateClaimSchema = (
type UpdateClaim = z.infer<typeof updateClaimSchema>;
// Extend the schema to inject `userId` manually (since it's not passed by the client)
const ExtendedClaimSchema = (
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
).extend({
userId: z.number(),
});
// Routes
// Get all claims for the logged-in user
@@ -73,12 +78,16 @@ router.get("/:id", async (req: Request, res: Response): Promise<any> => {
// Create a new claim
router.post("/", async (req: Request, res: Response): Promise<any> => {
try {
const claimData = ClaimSchema.parse({
if (Array.isArray(req.body.serviceLines)) {
req.body.serviceLines = { create: req.body.serviceLines };
}
const parsedClaim = ExtendedClaimSchema.parse({
...req.body,
userId: req.user!.id,
});
const newClaim = await storage.createClaim(claimData);
const newClaim = await storage.createClaim(parsedClaim);
res.status(201).json(newClaim);
} catch (error) {
if (error instanceof z.ZodError) {
@@ -87,7 +96,14 @@ router.post("/", async (req: Request, res: Response): Promise<any> => {
errors: error.format(),
});
}
res.status(500).json({ message: "Failed to create claim" });
console.error("❌ Failed to create claim:", error); // logs full error to server
// Send more detailed info to the client (for dev only)
return res.status(500).json({
message: "Failed to create claim",
error: error instanceof Error ? error.message : String(error),
});
}
});

View File

@@ -268,7 +268,6 @@ export function ClaimForm({
});
}, [serviceDate]);
// Handle patient field changes (to make inputs controlled and editable)
const updatePatientField = (field: keyof Patient, value: any) => {
setPatient((prev) => (prev ? { ...prev, [field]: value } : null));
@@ -305,7 +304,6 @@ export function ClaimForm({
setForm({ ...form, serviceLines: updatedLines });
};
// FILE UPLOAD ZONE
const [uploadedFiles, setUploadedFiles] = useState<File[]>([]);
const [isUploading, setIsUploading] = useState(false);
@@ -335,7 +333,7 @@ export function ClaimForm({
};
// Delta MA Button Handler
const handleDeltaMASubmit = () => {
const handleDeltaMASubmit = async () => {
const appointmentData = {
patientId: patientId,
date: convertToISODate(serviceDate),
@@ -343,7 +341,7 @@ export function ClaimForm({
};
// 1. Create or update appointment
onHandleAppointmentSubmit(appointmentData);
const appointmentId = await onHandleAppointmentSubmit(appointmentData);
// 2. Update patient
if (patient && typeof patient.id === "number") {
@@ -368,6 +366,7 @@ export function ClaimForm({
staffId: Number(staff?.id),
patientId: patient?.id,
insuranceProvider: "Delta MA",
appointmentId: appointmentId!,
});
// 4. Close form

View File

@@ -209,9 +209,9 @@ export default function ClaimsPage() {
},
});
const handleAppointmentSubmit = (
const handleAppointmentSubmit = async (
appointmentData: InsertAppointment | UpdateAppointment
) => {
): Promise<number> => {
// Converts local date to exact UTC date with no offset issues
function parseLocalDate(dateInput: Date | string): Date {
if (dateInput instanceof Date) return dateInput;
@@ -250,23 +250,39 @@ export default function ClaimsPage() {
new Date(a.date).toLocaleDateString("en-CA") === formattedDate
);
if (existingAppointment && typeof existingAppointment.id === 'number') {
if (existingAppointment && typeof existingAppointment.id === "number") {
// Update appointment with only date
updateAppointmentMutation.mutate({
id: existingAppointment.id,
appointment: minimalData,
});
} else {
// Create new appointment with required fields + defaults
createAppointmentMutation.mutate({
return existingAppointment.id;
}
return new Promise<number>((resolve, reject) => {
createAppointmentMutation.mutate(
{
...minimalData,
patientId: appointmentData.patientId,
userId: user?.id,
title: "Scheduled Appointment", // default title
type: "checkup", // default type
} as InsertAppointment);
title: "Scheduled Appointment",
type: "checkup",
},
{
onSuccess: (newAppointment) => {
resolve(newAppointment.id);
},
onError: (error) => {
toast({
title: "Error",
description: "Could not create appointment",
variant: "destructive",
});
reject(error);
},
}
);
});
};
const createClaimMutation = useMutation({
@@ -322,6 +338,15 @@ export default function ClaimsPage() {
patientId: null,
serviceDate: "",
});
// Remove query parameters without reload
const url = new URL(window.location.href);
url.searchParams.delete("memberId");
url.searchParams.delete("dob");
url.searchParams.delete("name");
// Use history.replaceState to update the URL without reloading
window.history.replaceState({}, document.title, url.toString());
};
const prefillClaimForm = (patient: Patient) => {
@@ -376,7 +401,7 @@ export default function ClaimsPage() {
});
}
}
}, [memberId, dob, patients]);
}, [memberId, dob]);
function handleClaimSubmit(claimData: any) {
createClaimMutation.mutate(claimData);
@@ -467,9 +492,7 @@ export default function ClaimsPage() {
onClick={() => {
if (patientsWithAppointments.length > 0) {
const firstPatient = patientsWithAppointments[0];
handleNewClaim(
Number(firstPatient?.patientId),
);
handleNewClaim(Number(firstPatient?.patientId));
} else {
toast({
title: "No patients available",
@@ -503,9 +526,7 @@ export default function ClaimsPage() {
<div
key={item.patientId}
className="py-4 flex items-center justify-between cursor-pointer hover:bg-gray-50"
onClick={() =>
handleNewClaim(item.patientId)
}
onClick={() => handleNewClaim(item.patientId)}
>
<div>
<h3 className="font-medium">{item.patientName}</h3>