Skip to content

Commit

Permalink
admin use cases validator service added
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 15, 2024
1 parent effa75c commit 615848a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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, {
Expand All @@ -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" });
Expand Down
6 changes: 4 additions & 2 deletions server/src/presentation/routers/admin/AdminRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion server/src/use_case/admin/AuthenticationUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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");
Expand All @@ -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");

Expand All @@ -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");

Expand Down
4 changes: 3 additions & 1 deletion server/src/use_case/admin/DoctorUseCase.ts
Original file line number Diff line number Diff line change
@@ -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<PaginatedResult<IDoctor>> {
Expand Down
6 changes: 5 additions & 1 deletion server/src/use_case/admin/PatientUseCase.ts
Original file line number Diff line number Diff line change
@@ -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<PaginatedResult<IPatient>> {
return await this.patientRepository.findMany(offset, limit);
Expand Down

1 comment on commit 615848a

@vercel
Copy link

@vercel vercel bot commented on 615848a Sep 15, 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.