From 615848adf535dacd7e1b6781262e912bf8c0928d Mon Sep 17 00:00:00 2001 From: Sinan Date: Sun, 15 Sep 2024 11:37:41 +0530 Subject: [PATCH] admin use cases validator service added --- .../controllers/admin/AuthenticationController.ts | 7 +------ server/src/presentation/routers/admin/AdminRoutes.ts | 6 ++++-- .../presentation/routers/admin/AuthenticationRoutes.ts | 5 ++++- server/src/use_case/admin/AuthenticationUseCase.ts | 8 +++++++- server/src/use_case/admin/DoctorUseCase.ts | 4 +++- server/src/use_case/admin/PatientUseCase.ts | 6 +++++- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/server/src/presentation/controllers/admin/AuthenticationController.ts b/server/src/presentation/controllers/admin/AuthenticationController.ts index 0d4fc4d5..7d5a3c37 100644 --- a/server/src/presentation/controllers/admin/AuthenticationController.ts +++ b/server/src/presentation/controllers/admin/AuthenticationController.ts @@ -8,9 +8,6 @@ export default class AuthenticationController { async login(req: Request, res: Response, next: NextFunction) { try { const { email, password } = req.body; - if (!email?.trim() || !password?.trim()) - res.status(StatusCode.BadRequest).json({ message: "Email and Password id Required" }); - await this.authUseCase.login(email, password); res.status(StatusCode.Success).json({ message: "Logged in Successfully. Otp has Sended" }); @@ -22,7 +19,7 @@ export default class AuthenticationController { async validateOtp(req: Request, res: Response, next: NextFunction) { try { const { email, otp } = req.body; - if (!email || !email.trim()) return res.status(StatusCode.BadRequest).json({ message: "Email is Required" }); + const { accessToken, refreshToken } = await this.authUseCase.validateOtp(email, otp); res.cookie(Cookie.Admin, refreshToken, { @@ -41,8 +38,6 @@ export default class AuthenticationController { async resendOtp(req: Request, res: Response, next: NextFunction) { try { const { email } = req.body; - if (!email?.trim()) return res.status(StatusCode.BadRequest).json({ message: "Email is Required" }); - await this.authUseCase.resendOtp(email); res.status(StatusCode.Success).json({ message: "Otp has Send to the email" }); diff --git a/server/src/presentation/routers/admin/AdminRoutes.ts b/server/src/presentation/routers/admin/AdminRoutes.ts index 8d925359..2be1855a 100644 --- a/server/src/presentation/routers/admin/AdminRoutes.ts +++ b/server/src/presentation/routers/admin/AdminRoutes.ts @@ -7,16 +7,18 @@ import AdminDoctorController from "../../controllers/admin/DoctorController"; import AdminDoctorUseCase from "../../../use_case/admin/DoctorUseCase"; import DoctorRepository from "../../../infrastructure/repositories/DoctorRepository"; import NodeMailerService from "../../../infrastructure/services/NodeMailerService"; +import JoiService from "../../../infrastructure/services/JoiService"; const router = express.Router(); const patientRepository = new PatientRepository(); const doctorRepository = new DoctorRepository(); const emailService = new NodeMailerService(); -const adminPatientUseCase = new AdminPatientUseCase(patientRepository); +const validatorService = new JoiService() +const adminPatientUseCase = new AdminPatientUseCase(patientRepository,validatorService); const adminPatientController = new AdminPatientController(adminPatientUseCase); -const adminDoctorUseCase = new AdminDoctorUseCase(doctorRepository, emailService); +const adminDoctorUseCase = new AdminDoctorUseCase(doctorRepository, emailService,validatorService); const adminDoctorController = new AdminDoctorController(adminDoctorUseCase); router diff --git a/server/src/presentation/routers/admin/AuthenticationRoutes.ts b/server/src/presentation/routers/admin/AuthenticationRoutes.ts index b0c1295b..686acfe9 100644 --- a/server/src/presentation/routers/admin/AuthenticationRoutes.ts +++ b/server/src/presentation/routers/admin/AuthenticationRoutes.ts @@ -6,6 +6,7 @@ import BcryptService from "../../../infrastructure/services/BcryptService"; import JWTService from "../../../infrastructure/services/JWTService"; import NodeMailerService from "../../../infrastructure/services/NodeMailerService"; import OtpRepository from "../../../infrastructure/repositories/OtpRepository"; +import JoiService from "../../../infrastructure/services/JoiService"; const router = express.Router(); @@ -14,13 +15,15 @@ const otpRepository = new OtpRepository(); const passwordService = new BcryptService(); const tokenService = new JWTService(); const emailService = new NodeMailerService(); +const validatorService = new JoiService() const authUseCase = new AuthenticationUseCase( adminRepository, passwordService, tokenService, emailService, - otpRepository + otpRepository, + validatorService ); const authController = new AuthenticationController(authUseCase); diff --git a/server/src/use_case/admin/AuthenticationUseCase.ts b/server/src/use_case/admin/AuthenticationUseCase.ts index 15400aa2..336ce7cd 100644 --- a/server/src/use_case/admin/AuthenticationUseCase.ts +++ b/server/src/use_case/admin/AuthenticationUseCase.ts @@ -4,16 +4,20 @@ import IEmailService from "../../domain/interface/services/IEmailService"; import ITokenService from "../../domain/interface/services/ITokenService"; import { IPasswordServiceRepository } from "../../domain/interface/services/IPasswordServiceRepository"; import { UserRole } from "../../types"; +import IValidatorService from "../../domain/interface/services/IValidatorService"; export default class AuthenticationUseCase { constructor( private adminRepository: IDoctorRepository, private passwordService: IPasswordServiceRepository, private tokenService: ITokenService, private emailService: IEmailService, - private otpRepository: IOtpRepository + private otpRepository: IOtpRepository, + private validatorService: IValidatorService ) { } async login(email: string, password: string): Promise { + this.validatorService.validateRequiredFields({email,password}) + this.validatorService.validateEmailFormat(email); const doctor = await this.adminRepository.findByEmailWithCredentials(email); if (!doctor) throw new Error("Invalid Credentials"); if (doctor?.role !== "admin") throw new Error("Invalid Credentials"); @@ -35,6 +39,7 @@ export default class AuthenticationUseCase { } async validateOtp(email: string, otp: number): Promise<{ accessToken: string; refreshToken: string }> { + this.validatorService.validateEmailFormat(email) const requestedOtp = await this.otpRepository.findOne(otp, email); if (!requestedOtp) throw new Error("Invalid Credentials"); @@ -52,6 +57,7 @@ export default class AuthenticationUseCase { } async resendOtp(email: string) { + this.validatorService.validateEmailFormat(email) const admin = await this.adminRepository.findByEmail(email); if (!admin) throw new Error("Not Found"); diff --git a/server/src/use_case/admin/DoctorUseCase.ts b/server/src/use_case/admin/DoctorUseCase.ts index 7e0ef3fc..7accfff9 100644 --- a/server/src/use_case/admin/DoctorUseCase.ts +++ b/server/src/use_case/admin/DoctorUseCase.ts @@ -1,12 +1,14 @@ import IDoctor from "../../domain/entities/IDoctor"; import IDoctorRepository from "../../domain/interface/repositories/IDoctorRepository"; import IEmailService from "../../domain/interface/services/IEmailService"; +import IValidatorService from "../../domain/interface/services/IValidatorService"; import { DoctorsFilter, PaginatedResult } from "../../types"; export default class AdminDoctorUseCase { constructor( private doctorRepository: IDoctorRepository, - private emailService: IEmailService + private emailService: IEmailService, + private validatorService: IValidatorService ) { } async getAll(offset: number, limit: number, type: DoctorsFilter): Promise> { diff --git a/server/src/use_case/admin/PatientUseCase.ts b/server/src/use_case/admin/PatientUseCase.ts index 918bbdd8..35e44d77 100644 --- a/server/src/use_case/admin/PatientUseCase.ts +++ b/server/src/use_case/admin/PatientUseCase.ts @@ -1,9 +1,13 @@ import IPatientRepository from "../../domain/interface/repositories/IPatientRepository"; import { IPatient } from "../../domain/entities/IPatient"; import { PaginatedResult } from "../../types"; +import IValidatorService from "../../domain/interface/services/IValidatorService"; export default class AdminPatientUseCase { - constructor(private patientRepository: IPatientRepository) {} + constructor( + private patientRepository: IPatientRepository, + private validatorService:IValidatorService + ) {} async getAll(offset: number, limit: number): Promise> { return await this.patientRepository.findMany(offset, limit);