Skip to content

Commit

Permalink
Register error handled
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Aug 26, 2024
1 parent f1c9ae4 commit 563536c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import PatientModel from "../models/PatientModel";

export default class PatientRepository implements IPatientRepository {
model = PatientModel;
async create(patient: IPatient): Promise<IPatient> {
const patientModel = new this.model(patient);
return await patientModel.save();
async create(patient: IPatient): Promise<IPatient | never> {
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<IPatient | null> {
return await this.model.findByIdAndUpdate(patient._id, patient, { new: true });
Expand Down
2 changes: 1 addition & 1 deletion server/src/interface/repositories/IPatientRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IPatient } from "../../domain/entities/Patient";

export default interface IPatientRepository {
create(patient: IPatient): Promise<IPatient>;
create(patient: IPatient): Promise<IPatient|never>;
update(patient: IPatient): Promise<IPatient | null>;
changeStatus(id: string, status: boolean): Promise<IPatient | null>;
findByEmail(email: string): Promise<IPatient | null>;
Expand Down
15 changes: 6 additions & 9 deletions server/src/presentation/controllers/PatientController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
19 changes: 19 additions & 0 deletions server/src/presentation/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -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 }),
});
}
2 changes: 1 addition & 1 deletion server/src/presentation/routers/PatientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
3 changes: 3 additions & 0 deletions server/src/presentation/routers/index.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 10 additions & 1 deletion server/src/use_case/patient/RegisterPatientUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}

1 comment on commit 563536c

@vercel
Copy link

@vercel vercel bot commented on 563536c Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.