insuranceCred half
This commit is contained in:
@@ -5,7 +5,7 @@ import userRoutes from './users'
|
||||
import staffRoutes from './staffs'
|
||||
import pdfExtractionRoutes from './pdfExtraction';
|
||||
import claimsRoutes from './claims';
|
||||
|
||||
import insuranceCredsRoutes from './insuranceCreds';
|
||||
const router = Router();
|
||||
|
||||
router.use('/patients', patientRoutes);
|
||||
@@ -14,5 +14,6 @@ router.use('/users', userRoutes);
|
||||
router.use('/staffs', staffRoutes);
|
||||
router.use('/pdfExtraction', pdfExtractionRoutes);
|
||||
router.use('/claims', claimsRoutes);
|
||||
router.use('/insuranceCreds', insuranceCredsRoutes);
|
||||
|
||||
export default router;
|
||||
79
apps/Backend/src/routes/insuranceCreds.ts
Normal file
79
apps/Backend/src/routes/insuranceCreds.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { InsuranceCredentialUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
import { z } from "zod";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// ✅ Types
|
||||
type InsuranceCredential = z.infer<typeof InsuranceCredentialUncheckedCreateInputObjectSchema>;
|
||||
|
||||
const insertInsuranceCredentialSchema = (
|
||||
InsuranceCredentialUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({ id: true });
|
||||
|
||||
type InsertInsuranceCredential = z.infer<typeof insertInsuranceCredentialSchema>;
|
||||
|
||||
// ✅ Get all credentials for a user
|
||||
router.get("/", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
if (!req.user || !req.user.id) {
|
||||
return res.status(401).json({ message: "Unauthorized: user info missing" });
|
||||
}
|
||||
const userId = req.user.id;
|
||||
|
||||
const credentials = await storage.getInsuranceCredentialsByUser(userId);
|
||||
return res.status(200).json(credentials);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to fetch credentials", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ Create credential for a user
|
||||
router.post("/", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
if (!req.user || !req.user.id) {
|
||||
return res.status(401).json({ message: "Unauthorized: user info missing" });
|
||||
}
|
||||
const userId = req.user.id;
|
||||
|
||||
const parseResult = insertInsuranceCredentialSchema.safeParse({ ...req.body, userId });
|
||||
if (!parseResult.success) {
|
||||
return res.status(400).json({ error: parseResult.error.flatten() });
|
||||
}
|
||||
|
||||
const credential = await storage.createInsuranceCredential(parseResult.data);
|
||||
return res.status(201).json(credential);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to create credential", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ Update credential
|
||||
router.put("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
const id = Number(req.params.id);
|
||||
if (isNaN(id)) return res.status(400).send("Invalid credential ID");
|
||||
|
||||
const updates = req.body as Partial<InsuranceCredential>;
|
||||
const credential = await storage.updateInsuranceCredential(id, updates);
|
||||
return res.status(200).json(credential);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to update credential", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ Delete a credential
|
||||
router.delete("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
const id = Number(req.params.id);
|
||||
if (isNaN(id)) return res.status(400).send("Invalid ID");
|
||||
|
||||
await storage.deleteInsuranceCredential(id);
|
||||
return res.status(204).send();
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to delete credential", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -3,6 +3,9 @@ import type { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { z } from "zod";
|
||||
import { UserUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
import jwt from 'jsonwebtoken';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -64,6 +67,13 @@ router.post("/", async (req: Request, res: Response) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Function to hash password using bcrypt
|
||||
async function hashPassword(password: string) {
|
||||
const saltRounds = 10; // Salt rounds for bcrypt
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
return hashedPassword;
|
||||
}
|
||||
|
||||
// PUT: Update user
|
||||
router.put("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
@@ -75,6 +85,15 @@ router.put("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
|
||||
|
||||
const updates = userUpdateSchema.parse(req.body);
|
||||
|
||||
// If password is provided and non-empty, hash it
|
||||
if (updates.password && updates.password.trim() !== "") {
|
||||
updates.password = await hashPassword(updates.password);
|
||||
} else {
|
||||
// Remove password field if empty, so it won't overwrite existing password with blank
|
||||
delete updates.password;
|
||||
}
|
||||
|
||||
const updatedUser = await storage.updateUser(id, updates);
|
||||
if (!updatedUser) return res.status(404).send("User not found");
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ import {
|
||||
PatientUncheckedCreateInputObjectSchema,
|
||||
UserUncheckedCreateInputObjectSchema,
|
||||
StaffUncheckedCreateInputObjectSchema,
|
||||
ClaimUncheckedCreateInputObjectSchema,
|
||||
ClaimUncheckedCreateInputObjectSchema,
|
||||
InsuranceCredentialUncheckedCreateInputObjectSchema,
|
||||
} from "@repo/db/usedSchemas";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -95,7 +96,7 @@ type RegisterFormValues = z.infer<typeof registerSchema>;
|
||||
// staff types:
|
||||
type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>;
|
||||
|
||||
// Claim typse:
|
||||
// Claim typse:
|
||||
const insertClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({
|
||||
@@ -118,6 +119,19 @@ type UpdateClaim = z.infer<typeof updateClaimSchema>;
|
||||
|
||||
type Claim = z.infer<typeof ClaimUncheckedCreateInputObjectSchema>;
|
||||
|
||||
// InsuraneCreds types:
|
||||
type InsuranceCredential = z.infer<
|
||||
typeof InsuranceCredentialUncheckedCreateInputObjectSchema
|
||||
>;
|
||||
|
||||
const insertInsuranceCredentialSchema = (
|
||||
InsuranceCredentialUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
).omit({ id: true });
|
||||
|
||||
type InsertInsuranceCredential = z.infer<
|
||||
typeof insertInsuranceCredentialSchema
|
||||
>;
|
||||
|
||||
export interface IStorage {
|
||||
// User methods
|
||||
getUser(id: number): Promise<User | undefined>;
|
||||
@@ -135,7 +149,7 @@ export interface IStorage {
|
||||
|
||||
// Appointment methods
|
||||
getAppointment(id: number): Promise<Appointment | undefined>;
|
||||
getAllAppointments(): Promise<Appointment[]>;
|
||||
getAllAppointments(): Promise<Appointment[]>;
|
||||
getAppointmentsByUserId(userId: number): Promise<Appointment[]>;
|
||||
getAppointmentsByPatientId(patientId: number): Promise<Appointment[]>;
|
||||
createAppointment(appointment: InsertAppointment): Promise<Appointment>;
|
||||
@@ -160,6 +174,17 @@ export interface IStorage {
|
||||
createClaim(claim: InsertClaim): Promise<Claim>;
|
||||
updateClaim(id: number, updates: UpdateClaim): Promise<Claim>;
|
||||
deleteClaim(id: number): Promise<void>;
|
||||
|
||||
// InsuranceCredential methods
|
||||
getInsuranceCredentialsByUser(userId: number): Promise<InsuranceCredential[]>;
|
||||
createInsuranceCredential(
|
||||
data: InsertInsuranceCredential
|
||||
): Promise<InsuranceCredential>;
|
||||
updateInsuranceCredential(
|
||||
id: number,
|
||||
updates: Partial<InsuranceCredential>
|
||||
): Promise<InsuranceCredential>;
|
||||
deleteInsuranceCredential(id: number): Promise<void>;
|
||||
}
|
||||
|
||||
export const storage: IStorage = {
|
||||
@@ -356,4 +381,27 @@ export const storage: IStorage = {
|
||||
throw new Error(`Claim with ID ${id} not found`);
|
||||
}
|
||||
},
|
||||
|
||||
// Insurance Creds
|
||||
async getInsuranceCredentialsByUser(userId: number) {
|
||||
return await db.insuranceCredential.findMany({ where: { userId } });
|
||||
},
|
||||
|
||||
async createInsuranceCredential(data: InsertInsuranceCredential) {
|
||||
return await db.insuranceCredential.create({ data: data as InsuranceCredential });
|
||||
},
|
||||
|
||||
async updateInsuranceCredential(
|
||||
id: number,
|
||||
updates: Partial<InsuranceCredential>
|
||||
) {
|
||||
return await db.insuranceCredential.update({
|
||||
where: { id },
|
||||
data: updates,
|
||||
});
|
||||
},
|
||||
|
||||
async deleteInsuranceCredential(id: number) {
|
||||
await db.insuranceCredential.delete({ where: { id } });
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user