search checkpoint 1
This commit is contained in:
@@ -86,6 +86,64 @@ router.get("/recent", async (req: Request, res: Response) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/search", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const {
|
||||
name,
|
||||
phone,
|
||||
insuranceId,
|
||||
gender,
|
||||
dob,
|
||||
limit = "10",
|
||||
offset = "0",
|
||||
} = req.query as Record<string, string>;
|
||||
|
||||
const filters: any = {
|
||||
userId: req.user!.id, // Assumes auth middleware sets this
|
||||
};
|
||||
|
||||
if (name) {
|
||||
filters.OR = [
|
||||
{ firstName: { contains: name, mode: "insensitive" } },
|
||||
{ lastName: { contains: name, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
|
||||
if (phone) {
|
||||
filters.phone = { contains: phone, mode: "insensitive" };
|
||||
}
|
||||
|
||||
if (insuranceId) {
|
||||
filters.insuranceId = { contains: insuranceId, mode: "insensitive" };
|
||||
}
|
||||
|
||||
if (gender) {
|
||||
filters.gender = gender;
|
||||
}
|
||||
|
||||
if (dob) {
|
||||
const parsedDate = new Date(dob);
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
filters.dateOfBirth = parsedDate;
|
||||
}
|
||||
}
|
||||
|
||||
const [patients, totalCount] = await Promise.all([
|
||||
storage.searchPatients({
|
||||
filters,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
}),
|
||||
storage.countPatients(filters),
|
||||
]);
|
||||
|
||||
res.json({ patients, totalCount });
|
||||
} catch (error) {
|
||||
console.error("Search error:", error);
|
||||
res.status(500).json({ message: "Failed to search patients" });
|
||||
}
|
||||
});
|
||||
|
||||
// Get a single patient by ID
|
||||
router.get(
|
||||
"/:id",
|
||||
|
||||
@@ -171,6 +171,24 @@ export interface IStorage {
|
||||
createPatient(patient: InsertPatient): Promise<Patient>;
|
||||
updatePatient(id: number, patient: UpdatePatient): Promise<Patient>;
|
||||
deletePatient(id: number): Promise<void>;
|
||||
searchPatients(args: {
|
||||
filters: any;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}): Promise<
|
||||
{
|
||||
id: number;
|
||||
firstName: string | null;
|
||||
lastName: string | null;
|
||||
phone: string | null;
|
||||
gender: string | null;
|
||||
dateOfBirth: Date;
|
||||
insuranceId: string | null;
|
||||
insuranceProvider: string | null;
|
||||
status: string;
|
||||
}[]
|
||||
>;
|
||||
countPatients(filters: any): Promise<number>; // optional but useful
|
||||
|
||||
// Appointment methods
|
||||
getAppointment(id: number): Promise<Appointment | undefined>;
|
||||
@@ -305,10 +323,42 @@ export const storage: IStorage = {
|
||||
});
|
||||
},
|
||||
|
||||
async searchPatients({
|
||||
filters,
|
||||
limit,
|
||||
offset,
|
||||
}: {
|
||||
filters: any;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}) {
|
||||
return db.patient.findMany({
|
||||
where: filters,
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: limit,
|
||||
skip: offset,
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
phone: true,
|
||||
gender: true,
|
||||
dateOfBirth: true,
|
||||
insuranceId: true,
|
||||
insuranceProvider: true,
|
||||
status: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
async getTotalPatientCount(): Promise<number> {
|
||||
return db.patient.count();
|
||||
},
|
||||
|
||||
async countPatients(filters: any) {
|
||||
return db.patient.count({ where: filters });
|
||||
},
|
||||
|
||||
async createPatient(patient: InsertPatient): Promise<Patient> {
|
||||
return await db.patient.create({ data: patient as Patient });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user