claim routes work being done
This commit is contained in:
159
apps/Backend/src/routes/claims.ts
Normal file
159
apps/Backend/src/routes/claims.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { Router } from "express";
|
||||
import { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
ClaimUncheckedCreateInputObjectSchema,
|
||||
} from "@repo/db/usedSchemas";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Define Zod schemas
|
||||
const ClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
});
|
||||
|
||||
type InsertClaim = z.infer<typeof ClaimSchema>;
|
||||
|
||||
const updateClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
)
|
||||
.omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
})
|
||||
.partial();
|
||||
|
||||
type UpdateClaim = z.infer<typeof updateClaimSchema>;
|
||||
|
||||
// Routes
|
||||
|
||||
// Get all claims for the logged-in user
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const claims = await storage.getClaimsByUserId(req.user!.id);
|
||||
res.json(claims);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to retrieve claims" });
|
||||
}
|
||||
});
|
||||
|
||||
// Get a single claim by ID
|
||||
router.get("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
if (!idParam) {
|
||||
return res.status(400).json({ error: "Missing claim ID" });
|
||||
}
|
||||
const claimId = parseInt(idParam, 10);
|
||||
if (isNaN(claimId)) {
|
||||
return res.status(400).json({ error: "Invalid claim ID" });
|
||||
}
|
||||
|
||||
const claim = await storage.getClaim(claimId);
|
||||
if (!claim) {
|
||||
return res.status(404).json({ message: "Claim not found" });
|
||||
}
|
||||
|
||||
if (claim.userId !== req.user!.id) {
|
||||
return res.status(403).json({ message: "Forbidden" });
|
||||
}
|
||||
|
||||
res.json(claim);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to retrieve claim" });
|
||||
}
|
||||
});
|
||||
|
||||
// Create a new claim
|
||||
router.post("/", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const claimData = ClaimSchema.parse({
|
||||
...req.body,
|
||||
userId: req.user!.id,
|
||||
});
|
||||
|
||||
const newClaim = await storage.createClaim(claimData);
|
||||
res.status(201).json(newClaim);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return res.status(400).json({
|
||||
message: "Validation error",
|
||||
errors: error.format(),
|
||||
});
|
||||
}
|
||||
res.status(500).json({ message: "Failed to create claim" });
|
||||
}
|
||||
});
|
||||
|
||||
// Update a claim
|
||||
router.put("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
if (!idParam) {
|
||||
return res.status(400).json({ error: "Missing claim ID" });
|
||||
}
|
||||
|
||||
const claimId = parseInt(idParam, 10);
|
||||
if (isNaN(claimId)) {
|
||||
return res.status(400).json({ error: "Invalid claim ID" });
|
||||
}
|
||||
|
||||
const existingClaim = await storage.getClaim(claimId);
|
||||
if (!existingClaim) {
|
||||
return res.status(404).json({ message: "Claim not found" });
|
||||
}
|
||||
|
||||
if (existingClaim.userId !== req.user!.id) {
|
||||
return res.status(403).json({ message: "Forbidden" });
|
||||
}
|
||||
|
||||
const claimData = updateClaimSchema.parse(req.body);
|
||||
const updatedClaim = await storage.updateClaim(claimId, claimData);
|
||||
res.json(updatedClaim);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return res.status(400).json({
|
||||
message: "Validation error",
|
||||
errors: error.format(),
|
||||
});
|
||||
}
|
||||
res.status(500).json({ message: "Failed to update claim" });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete a claim
|
||||
router.delete("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
if (!idParam) {
|
||||
return res.status(400).json({ error: "Missing claim ID" });
|
||||
}
|
||||
|
||||
const claimId = parseInt(idParam, 10);
|
||||
if (isNaN(claimId)) {
|
||||
return res.status(400).json({ error: "Invalid claim ID" });
|
||||
}
|
||||
|
||||
const existingClaim = await storage.getClaim(claimId);
|
||||
if (!existingClaim) {
|
||||
return res.status(404).json({ message: "Claim not found" });
|
||||
}
|
||||
|
||||
if (existingClaim.userId !== req.user!.id) {
|
||||
return res.status(403).json({ message: "Forbidden" });
|
||||
}
|
||||
|
||||
await storage.deleteClaim(claimId);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to delete claim" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -4,6 +4,7 @@ import appointmentRoutes from './appointements'
|
||||
import userRoutes from './users'
|
||||
import staffRoutes from './staffs'
|
||||
import pdfExtractionRoutes from './pdfExtraction';
|
||||
import claimsRoutes from './claims';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -11,6 +12,7 @@ router.use('/patients', patientRoutes);
|
||||
router.use('/appointments', appointmentRoutes);
|
||||
router.use('/users', userRoutes);
|
||||
router.use('/staffs', staffRoutes);
|
||||
router.use('/pdfExtraction/', pdfExtractionRoutes);
|
||||
router.use('/pdfExtraction', pdfExtractionRoutes);
|
||||
router.use('/claims', claimsRoutes);
|
||||
|
||||
export default router;
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
PatientUncheckedCreateInputObjectSchema,
|
||||
UserUncheckedCreateInputObjectSchema,
|
||||
StaffUncheckedCreateInputObjectSchema,
|
||||
ClaimUncheckedCreateInputObjectSchema,
|
||||
} from "@repo/db/usedSchemas";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -94,6 +95,29 @@ type RegisterFormValues = z.infer<typeof registerSchema>;
|
||||
// staff types:
|
||||
type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>;
|
||||
|
||||
// Claim typse:
|
||||
const insertClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
});
|
||||
type InsertClaim = z.infer<typeof insertClaimSchema>;
|
||||
|
||||
const updateClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
)
|
||||
.omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
})
|
||||
.partial();
|
||||
type UpdateClaim = z.infer<typeof updateClaimSchema>;
|
||||
|
||||
type Claim = z.infer<typeof ClaimUncheckedCreateInputObjectSchema>;
|
||||
|
||||
export interface IStorage {
|
||||
// User methods
|
||||
getUser(id: number): Promise<User | undefined>;
|
||||
@@ -127,6 +151,15 @@ export interface IStorage {
|
||||
createStaff(staff: Staff): Promise<Staff>;
|
||||
updateStaff(id: number, updates: Partial<Staff>): Promise<Staff | undefined>;
|
||||
deleteStaff(id: number): Promise<boolean>;
|
||||
|
||||
// Claim methods
|
||||
getClaim(id: number): Promise<Claim | undefined>;
|
||||
getClaimsByUserId(userId: number): Promise<Claim[]>;
|
||||
getClaimsByPatientId(patientId: number): Promise<Claim[]>;
|
||||
getClaimsByAppointmentId(appointmentId: number): Promise<Claim[]>;
|
||||
createClaim(claim: InsertClaim): Promise<Claim>;
|
||||
updateClaim(id: number, updates: UpdateClaim): Promise<Claim>;
|
||||
deleteClaim(id: number): Promise<void>;
|
||||
}
|
||||
|
||||
export const storage: IStorage = {
|
||||
@@ -281,4 +314,45 @@ export const storage: IStorage = {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// Claim methods implementation
|
||||
async getClaim(id: number): Promise<Claim | undefined> {
|
||||
const claim = await db.claim.findUnique({ where: { id } });
|
||||
return claim ?? undefined;
|
||||
},
|
||||
|
||||
async getClaimsByUserId(userId: number): Promise<Claim[]> {
|
||||
return await db.claim.findMany({ where: { userId } });
|
||||
},
|
||||
|
||||
async getClaimsByPatientId(patientId: number): Promise<Claim[]> {
|
||||
return await db.claim.findMany({ where: { patientId } });
|
||||
},
|
||||
|
||||
async getClaimsByAppointmentId(appointmentId: number): Promise<Claim[]> {
|
||||
return await db.claim.findMany({ where: { appointmentId } });
|
||||
},
|
||||
|
||||
async createClaim(claim: InsertClaim): Promise<Claim> {
|
||||
return await db.claim.create({ data: claim as Claim });
|
||||
},
|
||||
|
||||
async updateClaim(id: number, updates: UpdateClaim): Promise<Claim> {
|
||||
try {
|
||||
return await db.claim.update({
|
||||
where: { id },
|
||||
data: updates,
|
||||
});
|
||||
} catch (err) {
|
||||
throw new Error(`Claim with ID ${id} not found`);
|
||||
}
|
||||
},
|
||||
|
||||
async deleteClaim(id: number): Promise<void> {
|
||||
try {
|
||||
await db.claim.delete({ where: { id } });
|
||||
} catch (err) {
|
||||
throw new Error(`Claim with ID ${id} not found`);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user