From 563536c49043d626dca5f70283a3201cc0cd427b Mon Sep 17 00:00:00 2001 From: Sinan Date: Mon, 26 Aug 2024 15:02:31 +0530 Subject: [PATCH] Register error handled --- .../repositories/PatientRepository.ts | 13 ++++++++++--- .../repositories/IPatientRepository.ts | 2 +- .../controllers/PatientController.ts | 15 ++++++--------- .../presentation/middlewares/errorHandler.ts | 19 +++++++++++++++++++ .../src/presentation/routers/PatientRoutes.ts | 2 +- server/src/presentation/routers/index.ts | 3 +++ .../patient/RegisterPatientUseCase.ts | 11 ++++++++++- 7 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 server/src/presentation/middlewares/errorHandler.ts diff --git a/server/src/infrastructure/database/repositories/PatientRepository.ts b/server/src/infrastructure/database/repositories/PatientRepository.ts index bb82207c..8ab7e804 100644 --- a/server/src/infrastructure/database/repositories/PatientRepository.ts +++ b/server/src/infrastructure/database/repositories/PatientRepository.ts @@ -5,9 +5,16 @@ import PatientModel from "../models/PatientModel"; export default class PatientRepository implements IPatientRepository { model = PatientModel; - async create(patient: IPatient): Promise { - const patientModel = new this.model(patient); - return await patientModel.save(); + async create(patient: IPatient): Promise { + try { + const patientModel = new this.model(patient); + return await patientModel.save(); + } catch (error: any) { + if (error.code === 11000) { + throw new Error("Patient With Email Already Exists!."); + } + throw error; + } } async update(patient: IPatient): Promise { return await this.model.findByIdAndUpdate(patient._id, patient, { new: true }); diff --git a/server/src/interface/repositories/IPatientRepository.ts b/server/src/interface/repositories/IPatientRepository.ts index 26ebb25d..03ab2e13 100644 --- a/server/src/interface/repositories/IPatientRepository.ts +++ b/server/src/interface/repositories/IPatientRepository.ts @@ -1,7 +1,7 @@ import { IPatient } from "../../domain/entities/Patient"; export default interface IPatientRepository { - create(patient: IPatient): Promise; + create(patient: IPatient): Promise; update(patient: IPatient): Promise; changeStatus(id: string, status: boolean): Promise; findByEmail(email: string): Promise; diff --git a/server/src/presentation/controllers/PatientController.ts b/server/src/presentation/controllers/PatientController.ts index 14c8a99d..9d131b24 100644 --- a/server/src/presentation/controllers/PatientController.ts +++ b/server/src/presentation/controllers/PatientController.ts @@ -16,25 +16,23 @@ export default class PatientController { try { const patient = req.body.patient as IPatient; - // * email validation + // email validation if (!patient.email?.trim()) return res.status(400).json({ message: "Email is Required" }); if (!isValidEmail(patient.email)) return res.status(422).json({ message: "Invalid Email Format" }); - // * password validation + // password validation if (!patient.password?.trim()) return res.status(400).json({ message: "Password is required" }); if (!isValidatePassword(patient.password)) return res.status(422).json({ message: "Password is too week" }); - // * name validation + // name validation if (!patient.name?.trim()) return res.status(400).json({ message: "Name is required" }); - // * phone validation + // phone validation if (!patient.phone?.toString().trim()) return res.status(400).json({ message: "Phone number is required" }); - const newPatient = await this.registerPatientUseCase.execute(patient); - - res.status(200).json({ patient: newPatient }); + res.status(200).json({ patient: await this.registerPatientUseCase.execute(patient) }); + } catch (error) { - console.error("Error registering patient:", error); next(error); } } @@ -43,7 +41,6 @@ export default class PatientController { try { this.loginPatientUseCase.execute(req.body.patient); } catch (error) { - console.log("Error in Patient login : ", error); next(error); } } diff --git a/server/src/presentation/middlewares/errorHandler.ts b/server/src/presentation/middlewares/errorHandler.ts new file mode 100644 index 00000000..a50ca9ff --- /dev/null +++ b/server/src/presentation/middlewares/errorHandler.ts @@ -0,0 +1,19 @@ +import { Request, Response, NextFunction } from 'express'; + +export const errorHandler = (err: any, req: Request, res: Response, next: NextFunction)=> { + console.error(err); + + const statusCode = err.statusCode || 500; + + if (err.code && err.code === 11000) { + return res.status(409).json({ + message: "Duplicate key error. The resource already exists.", + error: err.message, + }); + } + + res.status(statusCode).json({ + message: err.message || "Internal Server Error", + ...(process.env.NODE_ENV !== 'production' && { stack: err.stack }), + }); +} diff --git a/server/src/presentation/routers/PatientRoutes.ts b/server/src/presentation/routers/PatientRoutes.ts index b82f06ed..4fec7f68 100644 --- a/server/src/presentation/routers/PatientRoutes.ts +++ b/server/src/presentation/routers/PatientRoutes.ts @@ -15,7 +15,7 @@ const loginPatientUseCase = new LoginPatientUseCase(patientRepository, passwordS const registerPatientUseCase = new RegisterPatientUseCase(patientRepository, passwordService); const patientController = new PatientController(patientUseCase, registerPatientUseCase, loginPatientUseCase); -route.post("/register", (req, res, next) => { +route.post("/", (req, res, next) => { patientController.registerPatient(req, res, next); }); diff --git a/server/src/presentation/routers/index.ts b/server/src/presentation/routers/index.ts index 3a4d5383..c6a7e162 100644 --- a/server/src/presentation/routers/index.ts +++ b/server/src/presentation/routers/index.ts @@ -1,8 +1,11 @@ import express from "express"; import patientRoutes from "./PatientRoutes"; +import { errorHandler } from "../middlewares/errorHandler"; const app = express(); app.use("/patient", patientRoutes); +app.use(errorHandler); + export default app; diff --git a/server/src/use_case/patient/RegisterPatientUseCase.ts b/server/src/use_case/patient/RegisterPatientUseCase.ts index 060b82ea..3c937f0d 100644 --- a/server/src/use_case/patient/RegisterPatientUseCase.ts +++ b/server/src/use_case/patient/RegisterPatientUseCase.ts @@ -10,6 +10,15 @@ export default class RegisterPatientUseCase { async execute(patient: IPatient) { patient.password = await this.passwordService.hash(patient.password!); - return await this.patientRepository.create(patient); + const { isSubscribed, _id, email, name, password, phone, isBlocked } = await this.patientRepository.create(patient); + return { + _id, + name, + phone, + email, + password, + isBlocked, + isSubscribed, + }; } }