import { prisma as db } from "@repo/db/client"; import { AppointmentUncheckedCreateInputObjectSchema, PatientUncheckedCreateInputObjectSchema, UserUncheckedCreateInputObjectSchema, StaffUncheckedCreateInputObjectSchema, } from "@repo/db/usedSchemas"; import { z } from "zod"; //creating types out of schema auto generated. type Appointment = z.infer; const insertAppointmentSchema = ( AppointmentUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).omit({ id: true, createdAt: true, }); type InsertAppointment = z.infer; const updateAppointmentSchema = ( AppointmentUncheckedCreateInputObjectSchema as unknown as z.ZodObject ) .omit({ id: true, createdAt: true, }) .partial(); type UpdateAppointment = z.infer; //patient types const PatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).omit({ appointments: true, }); type Patient = z.infer; type Patient2 = z.infer; const insertPatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).omit({ id: true, createdAt: true, }); type InsertPatient = z.infer; const updatePatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ) .omit({ id: true, createdAt: true, userId: true, }) .partial(); type UpdatePatient = z.infer; //user types type User = z.infer; const insertUserSchema = ( UserUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).pick({ username: true, password: true, }); const loginSchema = (insertUserSchema as unknown as z.ZodObject).extend({ rememberMe: z.boolean().optional(), }); const registerSchema = (insertUserSchema as unknown as z.ZodObject) .extend({ confirmPassword: z.string().min(6, { message: "Password must be at least 6 characters long", }), agreeTerms: z.literal(true, { errorMap: () => ({ message: "You must agree to the terms and conditions", }), }), }) .refine((data: any) => data.password === data.confirmPassword, { message: "Passwords don't match", path: ["confirmPassword"], }); type InsertUser = z.infer; type LoginFormValues = z.infer; type RegisterFormValues = z.infer; // staff types: type Staff = z.infer; export interface IStorage { // User methods getUser(id: number): Promise; getUserByUsername(username: string): Promise; createUser(user: InsertUser): Promise; updateUser(id: number, updates: Partial): Promise; deleteUser(id: number): Promise; // Patient methods getPatient(id: number): Promise; getPatientsByUserId(userId: number): Promise; createPatient(patient: InsertPatient): Promise; updatePatient(id: number, patient: UpdatePatient): Promise; deletePatient(id: number): Promise; // Appointment methods getAppointment(id: number): Promise; getAllAppointments(): Promise; getAppointmentsByUserId(userId: number): Promise; getAppointmentsByPatientId(patientId: number): Promise; createAppointment(appointment: InsertAppointment): Promise; updateAppointment( id: number, appointment: UpdateAppointment ): Promise; deleteAppointment(id: number): Promise; // Staff methods getStaff(id: number): Promise; getAllStaff(): Promise; createStaff(staff: Staff): Promise; updateStaff(id: number, updates: Partial): Promise; deleteStaff(id: number): Promise; } export const storage: IStorage = { // User methods async getUser(id: number): Promise { const user = await db.user.findUnique({ where: { id } }); return user ?? undefined; }, async getUserByUsername(username: string): Promise { const user = await db.user.findUnique({ where: { username } }); return user ?? undefined; }, async createUser(user: InsertUser): Promise { return await db.user.create({ data: user as User }); }, async updateUser( id: number, updates: Partial ): Promise { try { return await db.user.update({ where: { id }, data: updates }); } catch { return undefined; } }, async deleteUser(id: number): Promise { try { await db.user.delete({ where: { id } }); return true; } catch { return false; } }, // Patient methods async getPatient(id: number): Promise { const patient = await db.patient.findUnique({ where: { id } }); return patient ?? undefined; }, async getPatientsByUserId(userId: number): Promise { return await db.patient.findMany({ where: { userId } }); }, async createPatient(patient: InsertPatient): Promise { return await db.patient.create({ data: patient as Patient }); }, async updatePatient(id: number, updateData: UpdatePatient): Promise { try { return await db.patient.update({ where: { id }, data: updateData, }); } catch (err) { throw new Error(`Patient with ID ${id} not found`); } }, async deletePatient(id: number): Promise { try { await db.patient.delete({ where: { id } }); } catch (err) { throw new Error(`Patient with ID ${id} not found`); } }, // Appointment methods async getAppointment(id: number): Promise { const appointment = await db.appointment.findUnique({ where: { id } }); return appointment ?? undefined; }, async getAllAppointments(): Promise { return await db.appointment.findMany(); }, async getAppointmentsByUserId(userId: number): Promise { return await db.appointment.findMany({ where: { userId } }); }, async getAppointmentsByPatientId(patientId: number): Promise { return await db.appointment.findMany({ where: { patientId } }); }, async createAppointment( appointment: InsertAppointment ): Promise { return await db.appointment.create({ data: appointment as Appointment }); }, async updateAppointment( id: number, updateData: UpdateAppointment ): Promise { try { return await db.appointment.update({ where: { id }, data: updateData, }); } catch (err) { throw new Error(`Appointment with ID ${id} not found`); } }, async deleteAppointment(id: number): Promise { try { await db.appointment.delete({ where: { id } }); } catch (err) { throw new Error(`Appointment with ID ${id} not found`); } }, async getStaff(id: number): Promise { const staff = await db.staff.findUnique({ where: { id } }); return staff ?? undefined; }, async getAllStaff(): Promise { const staff = await db.staff.findMany(); return staff; }, async createStaff(staff: Staff): Promise { const createdStaff = await db.staff.create({ data: staff, }); return createdStaff; }, async updateStaff( id: number, updates: Partial ): Promise { const updatedStaff = await db.staff.update({ where: { id }, data: updates, }); return updatedStaff ?? undefined; }, async deleteStaff(id: number): Promise { try { await db.staff.delete({ where: { id } }); return true; } catch (error) { console.error("Error deleting staff:", error); return false; } }, };