diff --git a/server/src/domain/interface/repositories/IAppointmentRepository.ts b/server/src/domain/interface/repositories/IAppointmentRepository.ts index f3bd0749..1abccf35 100644 --- a/server/src/domain/interface/repositories/IAppointmentRepository.ts +++ b/server/src/domain/interface/repositories/IAppointmentRepository.ts @@ -3,10 +3,9 @@ import IAppointment, { AppointmentStatus } from "../../entities/IAppointment"; export default interface IAppointmentRepository { create(appointment: IAppointment): Promise; update(appointment: IAppointment): Promise; - updateStatusMany(appointmentIds: string[], status: AppointmentStatus): Promise; updateManyBySlotIds(slotIds: string[], fields: IAppointment): Promise; - findOneBySlotId(slotId: string): Promise; - findByDateAndSlot(appointmentDate: string, slotId:string): Promise; - findManyByDateAndDoctorId(appointmentDate:string,doctorId:string):Promise; - updateAppointmentStatusToConfirmed(appointmentId:string):Promise + findByDateAndSlot(appointmentDate: string, slotId: string): Promise; + findManyByDateAndDoctorId(appointmentDate: string, doctorId: string): Promise; + updateAppointmentStatusToConfirmed(appointmentId: string): Promise; + findManyByDoctorId(doctorId: string, status:AppointmentStatus): Promise } diff --git a/server/src/infrastructure/repositories/AppointmentRepository.ts b/server/src/infrastructure/repositories/AppointmentRepository.ts index 371c78be..712ab900 100644 --- a/server/src/infrastructure/repositories/AppointmentRepository.ts +++ b/server/src/infrastructure/repositories/AppointmentRepository.ts @@ -4,6 +4,7 @@ import AppointmentModel from "../database/AppointmentModel"; export default class AppointmentRepository implements IAppointmentRepository { model = AppointmentModel + async create(appointment: IAppointment): Promise { return (await this.model.create(appointment))._id } @@ -11,12 +12,12 @@ export default class AppointmentRepository implements IAppointmentRepository { await this.model.findByIdAndUpdate(appointment._id, appointment, { new: true }) } - async findOneBySlotId(slotId: string): Promise { - return await this.model.findOne({ slotId }); + async findManyByDoctorId(doctorId: string, status: AppointmentStatus): Promise { + return await this.model.find({ doctorId, status }); } async findManyByDateAndDoctorId(appointmentDate: string, doctorId: string): Promise { - const dateWithoutTime = appointmentDate.split('T')[0]; + const dateWithoutTime = appointmentDate.split('T')[0]; return await this.model.find({ doctorId, appointmentDate: { $regex: new RegExp(`^${dateWithoutTime}`) } @@ -26,9 +27,6 @@ export default class AppointmentRepository implements IAppointmentRepository { return await this.model.findOne({ appointmentDate, slotId }) } - async updateStatusMany(appointmentIds: string[], status: AppointmentStatus): Promise { - await this.model.updateMany({ _id: { $in: appointmentIds } }, { status }) - } async updateManyBySlotIds(slotIds: string[], fields: Partial): Promise { await this.model.updateMany({ slotId: { $in: slotIds } }, fields); diff --git a/server/src/presentation/controllers/appointment/AppointmentControllers.ts b/server/src/presentation/controllers/appointment/AppointmentControllers.ts index accbb5a0..519d8f8b 100644 --- a/server/src/presentation/controllers/appointment/AppointmentControllers.ts +++ b/server/src/presentation/controllers/appointment/AppointmentControllers.ts @@ -1,17 +1,20 @@ import { NextFunction, Response } from "express"; import { CustomRequest, StatusCode } from "../../../types"; -import AppointmentUseCase from "../../../use_case/appointment/AppointmentUseCase"; +import CreateAppointmentUseCase from "../../../use_case/appointment/CreateAppointmentUseCase"; +import GetAppointmentUseCase from "../../../use_case/appointment/GetAppointmentUseCase"; +import { AppointmentStatus } from "../../../domain/entities/IAppointment"; export default class AppointmentController { constructor( - private appointmentUseCase: AppointmentUseCase, + private createAppointmentUseCase: CreateAppointmentUseCase, + private getAppointmentUseCase: GetAppointmentUseCase ) { } async create(req: CustomRequest, res: Response, next: NextFunction) { try { const { appointment } = req.body; const patientId = req.patient?.id; - const { appointmentId, orderId, patient } = await this.appointmentUseCase.createAppointment(appointment, patientId!); + const { appointmentId, orderId, patient } = await this.createAppointmentUseCase.exec(appointment, patientId!); res.status(StatusCode.Success).json({ orderId, appointmentId, patient }); } catch (error: any) { next(error); @@ -21,11 +24,22 @@ export default class AppointmentController { async completePayment(req: CustomRequest, res: Response, next: NextFunction) { try { const { appointmentId,paymentData } = req.body; - await this.appointmentUseCase.verifyPayment(paymentData, appointmentId) + await this.createAppointmentUseCase.verifyPayment(paymentData, appointmentId) res.status(StatusCode.Success).json({ message: "Payment Verification Completed" }); } catch (error) { next(error) } } + async getAppointmentsDoctor(req:CustomRequest,res:Response,next:NextFunction){ + try { + const doctorId = req.doctor?.id; + const status = req.query.status as AppointmentStatus; + const appointments = await this.getAppointmentUseCase.getAppointmentsByDoctorId(doctorId!,status); + res.status(StatusCode.Success).json({appointments}) + } catch (error) { + next(error) + } + } + } diff --git a/server/src/presentation/routers/appointment/AppointmentRoutes.ts b/server/src/presentation/routers/appointment/AppointmentRoutes.ts index b76a3ae5..a3b15dae 100644 --- a/server/src/presentation/routers/appointment/AppointmentRoutes.ts +++ b/server/src/presentation/routers/appointment/AppointmentRoutes.ts @@ -1,7 +1,7 @@ import express from 'express' import AppointmentRepository from '../../../infrastructure/repositories/AppointmentRepository'; import SlotRepository from '../../../infrastructure/repositories/SlotRepository'; -import AppointmentUseCase from '../../../use_case/appointment/AppointmentUseCase'; +import CreateAppointmentUseCase from '../../../use_case/appointment/CreateAppointmentUseCase'; import AppointmentController from '../../controllers/appointment/AppointmentControllers'; import PatientAuthMiddleware from '../../middlewares/PatientAuthMiddleware'; import JWTService from '../../../infrastructure/services/JWTService'; @@ -9,6 +9,8 @@ import JoiService from '../../../infrastructure/services/JoiService'; import RazorPayService from '../../../infrastructure/services/RazorPayService'; import PaymentRepository from '../../../infrastructure/repositories/PaymentRepository'; import PatientRepository from '../../../infrastructure/repositories/PatientRepository'; +import GetAppointmentUseCase from '../../../use_case/appointment/GetAppointmentUseCase'; +import DoctorAuthMiddleware from '../../middlewares/DoctorAuthMiddleware'; const router = express.Router(); @@ -17,17 +19,21 @@ const appointmentRepository = new AppointmentRepository(); const slotRepository = new SlotRepository(); const tokenService = new JWTService(); const validatorService = new JoiService(); + const paymentService = new RazorPayService(); const paymentRepository = new PaymentRepository(); const patientRepository = new PatientRepository() -const appointmentUseCase = new AppointmentUseCase(appointmentRepository, slotRepository, validatorService, paymentService, paymentRepository, patientRepository); - -const appointmentController = new AppointmentController(appointmentUseCase); +const createAppointmentUseCase = new CreateAppointmentUseCase(appointmentRepository, slotRepository, validatorService, paymentService, paymentRepository, patientRepository); +const getAppointmentUseCase = new GetAppointmentUseCase(appointmentRepository, validatorService); +const appointmentController = new AppointmentController(createAppointmentUseCase, getAppointmentUseCase); const authorizePatient = new PatientAuthMiddleware(tokenService); +const authorizeDoctor = new DoctorAuthMiddleware(tokenService); + -router.post('/', authorizePatient.exec.bind(authorizePatient), appointmentController.create.bind(appointmentController)); router.post('/verify-payment', authorizePatient.exec.bind(authorizePatient), appointmentController.completePayment.bind(appointmentController)) +router.post('/', authorizePatient.exec.bind(authorizePatient), appointmentController.create.bind(appointmentController)); +router.get('/doctor', authorizeDoctor.exec.bind(authorizeDoctor), appointmentController.getAppointmentsDoctor.bind(appointmentController)) export default router; \ No newline at end of file diff --git a/server/src/use_case/appointment/AppointmentUseCase.ts b/server/src/use_case/appointment/CreateAppointmentUseCase.ts similarity index 99% rename from server/src/use_case/appointment/AppointmentUseCase.ts rename to server/src/use_case/appointment/CreateAppointmentUseCase.ts index 15f99eae..402276a5 100644 --- a/server/src/use_case/appointment/AppointmentUseCase.ts +++ b/server/src/use_case/appointment/CreateAppointmentUseCase.ts @@ -24,7 +24,7 @@ export default class AppointmentUseCase { this.bookingAmount = 300; } - async createAppointment( + async exec( appointmentData: IAppointment, patientId: string ): Promise<{ appointmentId: string, orderId: string, patient: IPatient }> { diff --git a/server/src/use_case/appointment/GetAppointmentUseCase.ts b/server/src/use_case/appointment/GetAppointmentUseCase.ts new file mode 100644 index 00000000..a4282376 --- /dev/null +++ b/server/src/use_case/appointment/GetAppointmentUseCase.ts @@ -0,0 +1,19 @@ +import IAppointmentRepository from "../../domain/interface/repositories/IAppointmentRepository"; +import IAppointment, { AppointmentStatus } from "../../domain/entities/IAppointment"; +import IValidatorService from "../../domain/interface/services/IValidatorService"; + +export default class GetAppointmentUseCase { + constructor( + private appointmentRepository: IAppointmentRepository, + private validatorService: IValidatorService + ) { } + + async getAppointmentsByDoctorId(doctorId: string, status?: AppointmentStatus): Promise { + this.validatorService.validateIdFormat(doctorId); + if(status){ + this.validatorService.validateEnum(status,Object.values(AppointmentStatus)) + } + return await this.appointmentRepository.findManyByDoctorId(doctorId, status ?? AppointmentStatus.CONFIRMED) + } + +} \ No newline at end of file