Skip to content

Commit

Permalink
get doctor appointments server side added
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 17, 2024
1 parent fc81a9c commit b8af633
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import IAppointment, { AppointmentStatus } from "../../entities/IAppointment";
export default interface IAppointmentRepository {
create(appointment: IAppointment): Promise<string>;
update(appointment: IAppointment): Promise<void>;
updateStatusMany(appointmentIds: string[], status: AppointmentStatus): Promise<void>;
updateManyBySlotIds(slotIds: string[], fields: IAppointment): Promise<void>;
findOneBySlotId(slotId: string): Promise<IAppointment | null>;
findByDateAndSlot(appointmentDate: string, slotId:string): Promise<IAppointment | null>;
findManyByDateAndDoctorId(appointmentDate:string,doctorId:string):Promise<IAppointment[] | null>;
updateAppointmentStatusToConfirmed(appointmentId:string):Promise<void>
findByDateAndSlot(appointmentDate: string, slotId: string): Promise<IAppointment | null>;
findManyByDateAndDoctorId(appointmentDate: string, doctorId: string): Promise<IAppointment[] | null>;
updateAppointmentStatusToConfirmed(appointmentId: string): Promise<void>;
findManyByDoctorId(doctorId: string, status:AppointmentStatus): Promise<IAppointment[] | null>
}
10 changes: 4 additions & 6 deletions server/src/infrastructure/repositories/AppointmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import AppointmentModel from "../database/AppointmentModel";

export default class AppointmentRepository implements IAppointmentRepository {
model = AppointmentModel

async create(appointment: IAppointment): Promise<string> {
return (await this.model.create(appointment))._id
}
async update(appointment: IAppointment): Promise<void> {
await this.model.findByIdAndUpdate(appointment._id, appointment, { new: true })
}

async findOneBySlotId(slotId: string): Promise<IAppointment | null> {
return await this.model.findOne({ slotId });
async findManyByDoctorId(doctorId: string, status: AppointmentStatus): Promise<IAppointment[] | null> {
return await this.model.find({ doctorId, status });
}

async findManyByDateAndDoctorId(appointmentDate: string, doctorId: string): Promise<IAppointment[] | null> {
const dateWithoutTime = appointmentDate.split('T')[0];
const dateWithoutTime = appointmentDate.split('T')[0];
return await this.model.find({
doctorId,
appointmentDate: { $regex: new RegExp(`^${dateWithoutTime}`) }
Expand All @@ -26,9 +27,6 @@ export default class AppointmentRepository implements IAppointmentRepository {
return await this.model.findOne({ appointmentDate, slotId })
}

async updateStatusMany(appointmentIds: string[], status: AppointmentStatus): Promise<void> {
await this.model.updateMany({ _id: { $in: appointmentIds } }, { status })
}

async updateManyBySlotIds(slotIds: string[], fields: Partial<IAppointment>): Promise<void> {
await this.model.updateMany({ slotId: { $in: slotIds } }, fields);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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)
}
}

}
16 changes: 11 additions & 5 deletions server/src/presentation/routers/appointment/AppointmentRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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';
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();

Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 }> {
Expand Down
19 changes: 19 additions & 0 deletions server/src/use_case/appointment/GetAppointmentUseCase.ts
Original file line number Diff line number Diff line change
@@ -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<IAppointment[] | null> {
this.validatorService.validateIdFormat(doctorId);
if(status){
this.validatorService.validateEnum(status,Object.values(AppointmentStatus))
}
return await this.appointmentRepository.findManyByDoctorId(doctorId, status ?? AppointmentStatus.CONFIRMED)
}

}

1 comment on commit b8af633

@vercel
Copy link

@vercel vercel bot commented on b8af633 Sep 17, 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.