From 131534bb234e5ed72d09bb5eb07a02fd798fa250 Mon Sep 17 00:00:00 2001 From: Sinan Date: Thu, 12 Sep 2024 08:19:43 +0530 Subject: [PATCH] uaAuthenticated route doctors server side added --- .../controllers/UnauthenticatedControllers.ts | 20 +++++++++++++++++++ server/src/presentation/routers/index.ts | 9 +++++++++ .../src/use_case/UnauthenticatedUseCases.ts | 18 +++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 server/src/presentation/controllers/UnauthenticatedControllers.ts create mode 100644 server/src/use_case/UnauthenticatedUseCases.ts diff --git a/server/src/presentation/controllers/UnauthenticatedControllers.ts b/server/src/presentation/controllers/UnauthenticatedControllers.ts new file mode 100644 index 00000000..26319221 --- /dev/null +++ b/server/src/presentation/controllers/UnauthenticatedControllers.ts @@ -0,0 +1,20 @@ +import { StatusCode } from "../../types"; +import UnauthenticatedUseCases from "../../use_case/UnauthenticatedUseCases"; +import { NextFunction, Request, Response } from "express"; + +export default class UnathenicatedControllers { + constructor(private unauthenticatedUseCase: UnauthenticatedUseCases) { } + + async getDoctors(req: Request, res: Response, next: NextFunction) { + try { + let offset = parseInt(req.query.offset as string); + let limit = parseInt(req.query.limit as string); + + offset = isNaN(offset) || offset < 0 ? 0 : offset; + limit = isNaN(limit) || limit < 0 ? 10 : Math.min(limit, 100); + res.status(StatusCode.Success).json(await this.unauthenticatedUseCase.getDoctors(offset, limit)) + } catch (error) { + next(error) + } + } +} \ No newline at end of file diff --git a/server/src/presentation/routers/index.ts b/server/src/presentation/routers/index.ts index d567bf51..e693471d 100644 --- a/server/src/presentation/routers/index.ts +++ b/server/src/presentation/routers/index.ts @@ -8,6 +8,9 @@ import PatientAuthMiddleware from "../middlewares/PatientAuthMiddleware"; import AdminAuthMiddleware from "../middlewares/AdminAuthMiddleware"; import protectedAdminRoutes from "./admin/AdminRoutes"; import doctorAuthentication from "./doctor/AuthenticationRoutes"; +import UnathenicatedControllers from "../controllers/UnauthenticatedControllers"; +import UnauthenticatedUseCases from "../../use_case/UnauthenticatedUseCases"; +import DoctorRepository from "../../infrastructure/repositories/DoctorRepository"; const app = express(); const tokenService = new TokenService(); @@ -15,6 +18,10 @@ const tokenService = new TokenService(); const authorizePatient = new PatientAuthMiddleware(tokenService); const authorizeAdmin = new AdminAuthMiddleware(tokenService); +const doctorRepository = new DoctorRepository() +const unauthenticatedUseCase = new UnauthenticatedUseCases(doctorRepository) +const unauthenticatedController = new UnathenicatedControllers(unauthenticatedUseCase) + const errorHandler = new ErrorHandler(); app.use("/patient/auth", patientAuthentication); @@ -25,6 +32,8 @@ app.use("/admin", authorizeAdmin.exec.bind(authorizeAdmin), protectedAdminRoutes app.use("/doctor/auth", doctorAuthentication); +app.get('/doctors', unauthenticatedController.getDoctors.bind(unauthenticatedController)) + app.use(errorHandler.exec.bind(errorHandler)); export default app; diff --git a/server/src/use_case/UnauthenticatedUseCases.ts b/server/src/use_case/UnauthenticatedUseCases.ts new file mode 100644 index 00000000..fd839b34 --- /dev/null +++ b/server/src/use_case/UnauthenticatedUseCases.ts @@ -0,0 +1,18 @@ +import IDoctor from "../domain/entities/IDoctor"; +import IDoctorRepository from "../domain/interface/repositories/IDoctorRepository"; +import { PaginatedResult } from "../types"; + + +export default class UnauthenticatedUseCases { + constructor(private doctorRepository: IDoctorRepository) { } + + + + async getDoctors(offset: number, limit: number): Promise> { + const isVerified = true; + const isBlocked = false + const data = await this.doctorRepository.findMany(offset, limit, isVerified, isBlocked); + data.items = data.items.filter(item=>item.role!=='admin') + return data; + } +} \ No newline at end of file