From f40a3c33d353303da3a9289f64a8dd20b2469533 Mon Sep 17 00:00:00 2001 From: Ali Akkas Date: Fri, 3 May 2024 00:06:00 +0600 Subject: [PATCH] fix: medications and diagnostic reports issue --- .github/workflows/cd.yaml | 9 + README.md | 18 ++ .../migration.sql | 8 + services/EHR/prisma/schema.prisma | 8 +- .../src/controllers/createDiagnosticReport.ts | 33 +++ services/EHR/src/controllers/createEHR.ts | 9 +- .../EHR/src/controllers/createMedication.ts | 28 +++ services/EHR/src/controllers/createPatient.ts | 35 ++++ services/EHR/src/controllers/deleteEHRById.ts | 21 ++ .../src/controllers/getDiagnosticReports.ts | 19 ++ services/EHR/src/controllers/getEHRById.ts | 19 ++ .../EHR/src/controllers/getEHRByPatientId.ts | 24 +++ services/EHR/src/controllers/getEHRs.ts | 2 +- .../EHR/src/controllers/getMedications.ts | 19 ++ services/EHR/src/controllers/index.ts | 9 + services/EHR/src/controllers/updateEHRById.ts | 31 +++ services/EHR/src/lib/EHRService.ts | 198 ++++++++++++++++++ services/EHR/src/routes/index.ts | 29 ++- services/EHR/src/schemas.ts | 22 +- .../appointment/src/lib/AppointmentService.ts | 43 +++- services/auth/src/controllers/register.ts | 2 +- services/auth/src/lib/RegistrationService.ts | 6 +- .../user/src/controllers/deleteUserById.ts | 2 +- 23 files changed, 567 insertions(+), 27 deletions(-) create mode 100644 services/EHR/prisma/migrations/20240502021834_unique_patient_email/migration.sql create mode 100644 services/EHR/src/controllers/createDiagnosticReport.ts create mode 100644 services/EHR/src/controllers/createMedication.ts create mode 100644 services/EHR/src/controllers/createPatient.ts create mode 100644 services/EHR/src/controllers/deleteEHRById.ts create mode 100644 services/EHR/src/controllers/getDiagnosticReports.ts create mode 100644 services/EHR/src/controllers/getEHRById.ts create mode 100644 services/EHR/src/controllers/getEHRByPatientId.ts create mode 100644 services/EHR/src/controllers/getMedications.ts create mode 100644 services/EHR/src/controllers/updateEHRById.ts diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index fc1dbb4..b95921c 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -42,6 +42,15 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/appointment:latest + - name: Build and push EHR service + uses: docker/build-push-action@v5 + with: + context: ./services/EHR + file: ./services/EHR/Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/EHR:latest + - name: Build and push user service uses: docker/build-push-action@v5 with: diff --git a/README.md b/README.md index 78f1a4b..4e51f6d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Healthcare Appointment Scheduling System ## About + Building Microservices Architecture using Docker, implementing CI/CD pipelines using GitHub Actions, incorporating asynchronous communication using RabbitMQ and caching with Redis. Additionally, implementing rate limiting, creating a custom API gateway or reverse proxy, and documenting system architecture. ## [Basic System Architecture](https://ali-akkas.notion.site/Healthcare-Appointment-Scheduling-System-cf67ead3bb1947f58f505c18fb886280?pvs=4) @@ -82,6 +83,22 @@ Healthcare-Appointment-Scheduling-System/ ├── app.ts ├── index.ts ├── config.ts + ├── queue.ts + ├── redis.ts + ├── tests + ├── package.json + ├── Dockerfile + ├── EHR + ├── prisma + ├── src/ + ├── controllers + ├── lib + ├── routes + ├── app.ts + ├── index.ts + ├── config.ts + ├── queue.ts + ├── redis.ts ├── tests ├── package.json ├── Dockerfile @@ -124,6 +141,7 @@ When push the code in the GitHub automatic run the actions ``` ### Postman `API Testing` + ```bash https://api.postman.com/collections/22653708-df253a8d-6b26-4e83-9673-793d1d6c04d5?access_key=PMAT-01HWT700T6H22BASWVF2E3HK9R ``` diff --git a/services/EHR/prisma/migrations/20240502021834_unique_patient_email/migration.sql b/services/EHR/prisma/migrations/20240502021834_unique_patient_email/migration.sql new file mode 100644 index 0000000..3bcb90d --- /dev/null +++ b/services/EHR/prisma/migrations/20240502021834_unique_patient_email/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - A unique constraint covering the columns `[patientEmail]` on the table `EHR` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "EHR_patientEmail_key" ON "EHR"("patientEmail"); diff --git a/services/EHR/prisma/schema.prisma b/services/EHR/prisma/schema.prisma index a7a0cf9..f78f6f8 100644 --- a/services/EHR/prisma/schema.prisma +++ b/services/EHR/prisma/schema.prisma @@ -16,15 +16,17 @@ enum Gender { } model EHR { - id String @id @default(cuid()) + id String @id @default(cuid()) patientId String - patientEmail String + patientEmail String @unique medicalHistories String[] allergies String[] medications Medication[] diagnosticReports DiagnosticReport[] + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt - patient Patient @relation(fields: [patientId], references: [id]) + patient Patient @relation(fields: [patientId], references: [id]) } model Patient { diff --git a/services/EHR/src/controllers/createDiagnosticReport.ts b/services/EHR/src/controllers/createDiagnosticReport.ts new file mode 100644 index 0000000..b411413 --- /dev/null +++ b/services/EHR/src/controllers/createDiagnosticReport.ts @@ -0,0 +1,33 @@ +import { Request, Response, NextFunction } from 'express'; +import { DiagnosticReportCreateSchema } from '@/schemas'; +import ehrService from '@/lib/EHRService'; + +const createDiagnosticReport = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + // Validate the request body + const parsedBody = DiagnosticReportCreateSchema.safeParse(req.body); + if (!parsedBody.success) { + return res.status(400).json({ message: parsedBody.error.errors }); + } + + // Create the DiagnosticReport + const diagnosticReport = await ehrService.createDiagnosticReport( + parsedBody.data + ); + + return res + .status(201) + .json({ + message: 'Diagnostic Report created successfully!', + diagnosticReport, + }); + } catch (err) { + next(err); + } +}; + +export default createDiagnosticReport; diff --git a/services/EHR/src/controllers/createEHR.ts b/services/EHR/src/controllers/createEHR.ts index 5a99fc5..59fa0fd 100644 --- a/services/EHR/src/controllers/createEHR.ts +++ b/services/EHR/src/controllers/createEHR.ts @@ -3,7 +3,6 @@ import { EHRCreateSchema } from '@/schemas'; import ehrService from '@/lib/EHRService'; const createEHR = async (req: Request, res: Response, next: NextFunction) => { - console.log('Creating EHR...'); try { // Validate the request body const parsedBody = EHRCreateSchema.safeParse(req.body); @@ -11,6 +10,14 @@ const createEHR = async (req: Request, res: Response, next: NextFunction) => { return res.status(400).json({ message: parsedBody.error.errors }); } + // Check if the patient already has an EHR by patient email + const existingEHR = await ehrService.checkExistingEHR( + parsedBody.data.patientEmail + ); + if (existingEHR) { + return res.status(400).json({ message: 'Patient already has an EHR' }); + } + // Create the EHR const ehr = await ehrService.createEHR(parsedBody.data); diff --git a/services/EHR/src/controllers/createMedication.ts b/services/EHR/src/controllers/createMedication.ts new file mode 100644 index 0000000..6473860 --- /dev/null +++ b/services/EHR/src/controllers/createMedication.ts @@ -0,0 +1,28 @@ +import { Request, Response, NextFunction } from 'express'; +import { MedicationCreateSchema } from '@/schemas'; +import ehrService from '@/lib/EHRService'; + +const createMedication = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + // Validate the request body + const parsedBody = MedicationCreateSchema.safeParse(req.body); + if (!parsedBody.success) { + return res.status(400).json({ message: parsedBody.error.errors }); + } + + // Create the Medications + const medication = await ehrService.createMedication(parsedBody.data); + + return res + .status(201) + .json({ message: 'Medication created successfully!', medication }); + } catch (err) { + next(err); + } +}; + +export default createMedication; diff --git a/services/EHR/src/controllers/createPatient.ts b/services/EHR/src/controllers/createPatient.ts new file mode 100644 index 0000000..9171ee4 --- /dev/null +++ b/services/EHR/src/controllers/createPatient.ts @@ -0,0 +1,35 @@ +import { Request, Response, NextFunction } from 'express'; +import { PatientCreateSchema } from '@/schemas'; +import ehrService from '@/lib/EHRService'; + +const createPatient = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + // Validate the request body + const parsedBody = PatientCreateSchema.safeParse(req.body); + + if (!parsedBody.success) { + return res.status(400).json({ message: parsedBody.error.errors }); + } + + const { userId } = parsedBody.data; + const existingPatient = await ehrService.checkExistingPatient(userId); + + if (existingPatient) { + return res.status(400).json({ message: 'Patient already exists!' }); + } + + const patient = await ehrService.createPatient(parsedBody.data); + + return res + .status(201) + .json({ message: 'Patient created successfully!', patient }); + } catch (err) { + next(err); + } +}; + +export default createPatient; diff --git a/services/EHR/src/controllers/deleteEHRById.ts b/services/EHR/src/controllers/deleteEHRById.ts new file mode 100644 index 0000000..b695881 --- /dev/null +++ b/services/EHR/src/controllers/deleteEHRById.ts @@ -0,0 +1,21 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; + +const deleteEHRById = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const { id } = req.params; + + // Delete EHR by id + await ehrService.deleteEHRById(id); + + return res.status(204).send(); + } catch (err) { + next(err); + } +}; + +export default deleteEHRById; diff --git a/services/EHR/src/controllers/getDiagnosticReports.ts b/services/EHR/src/controllers/getDiagnosticReports.ts new file mode 100644 index 0000000..1060e37 --- /dev/null +++ b/services/EHR/src/controllers/getDiagnosticReports.ts @@ -0,0 +1,19 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; + +const getDiagnosticReports = async ( + _req: Request, + res: Response, + next: NextFunction +) => { + try { + // Get all Diagnostic Reports + const diagnosticReports = await ehrService.getDiagnosticReports(); + + return res.status(200).json(diagnosticReports); + } catch (err) { + next(err); + } +}; + +export default getDiagnosticReports; diff --git a/services/EHR/src/controllers/getEHRById.ts b/services/EHR/src/controllers/getEHRById.ts new file mode 100644 index 0000000..be15d6c --- /dev/null +++ b/services/EHR/src/controllers/getEHRById.ts @@ -0,0 +1,19 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; + +const getEHRById = async (req: Request, res: Response, next: NextFunction) => { + try { + const { id } = req.params; + + const EHR = await ehrService.getEHRById(id); + if (!EHR) { + return res.status(404).json({ message: 'EHR not found' }); + } + + return res.status(200).json({ message: 'EHR fetched successfully', EHR }); + } catch (err) { + next(err); + } +}; + +export default getEHRById; diff --git a/services/EHR/src/controllers/getEHRByPatientId.ts b/services/EHR/src/controllers/getEHRByPatientId.ts new file mode 100644 index 0000000..f3a7998 --- /dev/null +++ b/services/EHR/src/controllers/getEHRByPatientId.ts @@ -0,0 +1,24 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; + +const getEHRByPatientId = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const { patientId } = req.params; + + const EHR = await ehrService.getEHRByPatientId(patientId); + + if (!EHR) { + return res.status(404).json({ message: 'EHR not found' }); + } + + res.status(200).json({ message: 'EHR fetched successfully', EHR }); + } catch (err) { + next(err); + } +}; + +export default getEHRByPatientId; diff --git a/services/EHR/src/controllers/getEHRs.ts b/services/EHR/src/controllers/getEHRs.ts index c8017f4..ec999bc 100644 --- a/services/EHR/src/controllers/getEHRs.ts +++ b/services/EHR/src/controllers/getEHRs.ts @@ -1,5 +1,5 @@ -import ehrService from '@/lib/EHRService'; import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; const getEHRs = async (_req: Request, res: Response, next: NextFunction) => { try { diff --git a/services/EHR/src/controllers/getMedications.ts b/services/EHR/src/controllers/getMedications.ts new file mode 100644 index 0000000..f2583e7 --- /dev/null +++ b/services/EHR/src/controllers/getMedications.ts @@ -0,0 +1,19 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; + +const getMedications = async ( + _req: Request, + res: Response, + next: NextFunction +) => { + try { + // Get all Medications + const medications = await ehrService.getMedications(); + + return res.status(200).json(medications); + } catch (err) { + next(err); + } +}; + +export default getMedications; diff --git a/services/EHR/src/controllers/index.ts b/services/EHR/src/controllers/index.ts index cdab4ea..9fde450 100644 --- a/services/EHR/src/controllers/index.ts +++ b/services/EHR/src/controllers/index.ts @@ -1,2 +1,11 @@ export { default as createEHR } from './createEHR'; export { default as getEHRs } from './getEHRs'; +export { default as getEHRById } from './getEHRById'; +export { default as getEHRByPatientId } from './getEHRByPatientId'; +export { default as updateEHRById } from './updateEHRById'; +export { default as deleteEHRById } from './deleteEHRById'; +export { default as createPatient } from './createPatient'; +export { default as createMedication } from './createMedication'; +export { default as createDiagnosticReport } from './createDiagnosticReport'; +export { default as getMedications } from './getMedications'; +export { default as getDiagnosticReports } from './getDiagnosticReports'; diff --git a/services/EHR/src/controllers/updateEHRById.ts b/services/EHR/src/controllers/updateEHRById.ts new file mode 100644 index 0000000..e78d901 --- /dev/null +++ b/services/EHR/src/controllers/updateEHRById.ts @@ -0,0 +1,31 @@ +import { Request, Response, NextFunction } from 'express'; +import ehrService from '@/lib/EHRService'; +import { EHRUpdateSchema } from '@/schemas'; + +const updateEHRById = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + // Validate the request body + const parsedBody = EHRUpdateSchema.safeParse(req.body); + if (!parsedBody.success) { + return res.status(400).json({ message: parsedBody.error.errors }); + } + + const { id } = req.params; + + // Update EHR by id + const updatedEHR = await ehrService.updateEHRById(id, parsedBody.data); + if (!updatedEHR) { + return res.status(404).json({ message: 'EHR not found' }); + } + + return res.json({ message: 'EHR updated successfully', updatedEHR }); + } catch (err) { + next(err); + } +}; + +export default updateEHRById; diff --git a/services/EHR/src/lib/EHRService.ts b/services/EHR/src/lib/EHRService.ts index fa63eac..31cb615 100644 --- a/services/EHR/src/lib/EHRService.ts +++ b/services/EHR/src/lib/EHRService.ts @@ -3,6 +3,44 @@ import sendToQueue from '@/queue'; import redis from '@/redis'; class EHRService { + /** + * Check if the patient already has an EHR + * @param patientEmail - Patient email + */ + public async checkExistingEHR(patientEmail: string) { + const existingEHR = await prisma.eHR.findUnique({ + where: { + patientEmail, + }, + }); + + return existingEHR; + } + + /** + * Create a new medication for an EHR + * @param medication - Medication data + */ + public async createMedication(medication: any) { + const createdMedication = await prisma.medication.create({ + data: medication, + }); + + return createdMedication; + } + + /** + * Create a new diagnostic report for an EHR + * @param diagnosticReport - Diagnostic Report data + */ + public async createDiagnosticReport(diagnosticReport: any) { + const createdDiagnosticReport = await prisma.diagnosticReport.create({ + data: diagnosticReport, + }); + + return createdDiagnosticReport; + } + /** * Create a new EHR * Send an email notification to the patient @@ -13,6 +51,13 @@ class EHRService { try { const ehr = await prisma.eHR.create({ data: EHRData, + select: { + id: true, + patientId: true, + patientEmail: true, + medicalHistories: true, + allergies: true, + }, }); await sendToQueue('send-email', JSON.stringify(ehr)); @@ -29,6 +74,7 @@ class EHRService { /** * Get All EHRs from cache or database * Cache EHRs data for future requests + * @returns EHRs data */ public async getEHRs() { try { @@ -50,6 +96,158 @@ class EHRService { throw err; } } + + /** + * Get EHR by patientId and aggregate data from Medications and Diagnostic Reports + * Cache EHR data for future requests + * @param patientId - Patient ID + */ + public async getEHRByPatientId(patientId: string) { + try { + // Check if EHRs data is cached + const cachedEHRs = await redis.get(`EHR:${patientId}`); + if (cachedEHRs) { + return JSON.parse(cachedEHRs); + } + + // If not cached, fetch EHR from the database + const EHR = await prisma.eHR.findFirst({ + where: { + patientId, + }, + }); + + // Cache EHRs data for future requests + await redis.set(`EHR:${patientId}`, JSON.stringify(EHR)); + + return EHR; + } catch (err) { + console.error('Error fetching EHRs:', err); + throw err; + } + } + + /** + * Get EHR by EHR ID and aggregate data from Medications and Diagnostic Reports + * Cache EHR data for future requests + * @param ehrId - EHR ID + * @returns EHR data + */ + public async getEHRById(ehrId: string) { + try { + // Check if EHR data is cached + const cachedEHR = await redis.get(`EHR:${ehrId}`); + if (cachedEHR) { + return JSON.parse(cachedEHR); + } + + // If not cached, fetch EHR from the database + const EHR = await prisma.eHR.findUnique({ + where: { + id: ehrId, + }, + include: { + medications: true, + diagnosticReports: true, + }, + }); + + // Cache EHR data for future requests + await redis.set(`EHR:${ehrId}`, JSON.stringify(EHR)); + + return EHR; + } catch (err) { + console.error('Error fetching EHR:', err); + throw err; + } + } + + /** + * Update EHR by EHR ID + * Invalidate cache for the updated EHR data and all EHRs + * @param ehrId - EHR ID + * @param EHRData - Updated EHR data + * @returns Updated EHR data + */ + public async updateEHRById(ehrId: string, EHRData: any) { + try { + const ehr = await prisma.eHR.update({ + where: { + id: ehrId, + }, + data: EHRData, + }); + + await redis.del(`EHR:${ehrId}`); + await redis.del('EHRs'); + + return ehr; + } catch (err) { + console.error('Error updating EHR:', err); + throw err; + } + } + + /** + * Delete EHR by EHR ID + * Invalidate cache for the deleted EHR data and all EHRs + * @param ehrId - EHR ID + * @returns Deleted EHR data + */ + public async deleteEHRById(ehrId: string) { + try { + const ehr = await prisma.eHR.delete({ + where: { + id: ehrId, + }, + }); + + await redis.del(`EHR:${ehrId}`); + await redis.del('EHRs'); + + return ehr; + } catch (err) { + console.error('Error deleting EHR:', err); + throw err; + } + } + + /** + * Get all medications for an EHR + * @returns Medications data + */ + public async getMedications() { + console.log('Fetching Medications from the database'); + return prisma.medication.findMany(); + } + + /** + * Get all diagnostic reports for an EHR + * @returns Diagnostic Reports data + */ + public async getDiagnosticReports() { + return prisma.diagnosticReport.findMany(); + } + + /** + * Check if the patient already exists + */ + public async checkExistingPatient(userId: string) { + return prisma.patient.findUnique({ + where: { userId }, + }); + } + + /** + * Create a new patient + */ + public async createPatient(patientData: any) { + const patient = await prisma.patient.create({ + data: patientData, + }); + + return patient; + } } const ehrService = new EHRService(); diff --git a/services/EHR/src/routes/index.ts b/services/EHR/src/routes/index.ts index 56d5cc1..4db3d61 100644 --- a/services/EHR/src/routes/index.ts +++ b/services/EHR/src/routes/index.ts @@ -1,8 +1,35 @@ import { Router } from 'express'; -import { createEHR, getEHRs } from '@/controllers'; +import { + createEHR, + getEHRs, + getEHRById, + getEHRByPatientId, + updateEHRById, + deleteEHRById, + createPatient, + createMedication, + createDiagnosticReport, + getMedications, + getDiagnosticReports, +} from '@/controllers'; const router = Router(); router.post('/ehr', createEHR).get('/ehr', getEHRs); +router + .get('/ehr/:id', getEHRById) + .put('/ehr/:id', updateEHRById) + .delete('/ehr/:id', deleteEHRById); + +router.post('/ehr/patient', createPatient); +router.get('/ehr/patient/:patientId', getEHRByPatientId); + +router + .post('/ehr/medications', createMedication) + .get('/ehr/medications', getMedications); + +router + .post('/ehr/diagnostic-reports', createDiagnosticReport) + .get('/ehr/diagnostic-reports', getDiagnosticReports); export default router; diff --git a/services/EHR/src/schemas.ts b/services/EHR/src/schemas.ts index bfbd18e..40306fd 100644 --- a/services/EHR/src/schemas.ts +++ b/services/EHR/src/schemas.ts @@ -1,17 +1,15 @@ import { z } from 'zod'; -const Medication = z.object({ - id: z.string(), +export const MedicationCreateSchema = z.object({ name: z.string(), dosage: z.string(), - start_date: z.date(), - end_date: z.date(), + start_date: z.string(), + end_date: z.string(), }); -const DiagnosticReport = z.object({ - id: z.string(), +export const DiagnosticReportCreateSchema = z.object({ title: z.string(), - date: z.date(), + date: z.string(), findings: z.string(), }); @@ -20,10 +18,16 @@ export const EHRCreateSchema = z.object({ patientEmail: z.string().email(), medicalHistories: z.array(z.string()), allergies: z.array(z.string()), - medications: z.array(Medication), - diagnosticReports: z.array(DiagnosticReport), }); export const EHRUpdateSchema = EHRCreateSchema.omit({ patientId: true, }).partial(); + +export const PatientCreateSchema = z.object({ + userId: z.string(), + patientName: z.string(), + dateOfBirth: z.string(), + medicalHistory: z.string().optional(), + insuranceInformation: z.string().optional(), +}); diff --git a/services/appointment/src/lib/AppointmentService.ts b/services/appointment/src/lib/AppointmentService.ts index 7a8bc38..9119c88 100644 --- a/services/appointment/src/lib/AppointmentService.ts +++ b/services/appointment/src/lib/AppointmentService.ts @@ -14,7 +14,7 @@ class AppointmentService { const appointment = await prisma.appointment.create({ data: appointmentData, }); - + await sendToQueue('send-email', JSON.stringify(appointment)); await redis.del('appointments'); @@ -122,21 +122,48 @@ class AppointmentService { /** * Update an appointment + * Invalidate cache for the updated appointment and all appointments + * @param appointmentId - Appointment ID + * @param appointmentData - Data to update the appointment + * @returns Updated appointment data */ public async updateAppointment(appointmentId: string, appointmentData: any) { - return prisma.appointment.update({ - where: { id: appointmentId }, - data: appointmentData, - }); + try { + const appointment = await prisma.appointment.update({ + where: { id: appointmentId }, + data: appointmentData, + }); + + await redis.del(`appointment:${appointmentId}`); + await redis.del('appointments'); + + return appointment; + } catch (err) { + console.error('Error updating appointment:', err); + throw err; + } } /** * Delete an appointment + * Invalidate cache for the deleted appointment data and all appointments + * @param appointmentId - Appointment ID + * @returns Deleted appointment data */ public async deleteAppointment(appointmentId: string) { - return prisma.appointment.delete({ - where: { id: appointmentId }, - }); + try { + const appointment = await prisma.appointment.delete({ + where: { id: appointmentId }, + }); + + await redis.del(`appointment:${appointmentId}`); + await redis.del('appointments'); + + return appointment; + } catch (err) { + console.error('Error deleting appointment:', err); + throw err; + } } /** diff --git a/services/auth/src/controllers/register.ts b/services/auth/src/controllers/register.ts index 7e5e119..4f059b3 100644 --- a/services/auth/src/controllers/register.ts +++ b/services/auth/src/controllers/register.ts @@ -23,7 +23,7 @@ const register = async (req: Request, res: Response, next: NextFunction) => { const user = await registrationService.createUser(parsedBody.data); await registrationService.createUserProfile(user.id, name, email); - // Generate verification code + // Generate verification code and save it to the database const code = emailService.generateVerificationCode(); await emailService.createVerificationCode(user.id, code); diff --git a/services/auth/src/lib/RegistrationService.ts b/services/auth/src/lib/RegistrationService.ts index 2ef3b74..4354783 100644 --- a/services/auth/src/lib/RegistrationService.ts +++ b/services/auth/src/lib/RegistrationService.ts @@ -28,11 +28,13 @@ class RegistrationService { /** * Create an auth user. */ - public async createUser(userData) { + public async createUser(userData: any) { + const hashedPassword = await this.generateHash(userData.password); + const user = await prisma.user.create({ data: { ...userData, - password: await this.generateHash(userData.password), + password: hashedPassword, }, select: { id: true, diff --git a/services/user/src/controllers/deleteUserById.ts b/services/user/src/controllers/deleteUserById.ts index 43e09dc..9f427f2 100644 --- a/services/user/src/controllers/deleteUserById.ts +++ b/services/user/src/controllers/deleteUserById.ts @@ -1,5 +1,5 @@ -import userService from '@/lib/UserService'; import { Request, Response, NextFunction } from 'express'; +import userService from '@/lib/UserService'; const deleteUserById = async ( req: Request,