All files / src/routes/v1 appointments.route.ts

82.05% Statements 64/78
68.42% Branches 39/57
90.47% Functions 19/21
89.55% Lines 60/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86  7x 35x 35x   35x   7x 105x   7x 9x 9x 9x   18x 9x     7x 7x   7x 7x 7x 7x 7x 7x 7x 7x     7x     1x 1x 1x 1x 1x 1x               4x 4x 4x 4x 4x 4x               2x 2x 2x 2x 2x 2x               1x 1x 1x 1x 1x 1x            
import { AppointmentRepository } from '@/repositories/appointment.repo';
import { AppointmentZODSchema } from '@/schemas/appointment/appointment.schema';
import { NextFunction, Request, Response } from 'express';
import HttpStatusCode from '@/utils/HTTPStatusCodes';
import { EAuthGuard, Delete, Get, Post, Put, RequestBody } from '@/decorators/router.decorator';
import { MainRouter } from '../router';
import { parseAPIVersion } from '@/config/app.config';
 
clasEs AppointmentsRouter extends MainRouter {
  constructor(prefix: string) {
    super(prefix, 'Appointments');
  }
 
  @AuthGuard()
  @RequestBody(AppointmentZODSchema.createAppointmentSchema, 'createAppointmentSchema')
  @Post('/')
  async createAppointment(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TCreateAppointmentSchema = req.body;
      const resBody = await AppointmentRepository.createAppointment(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @RequestBody(AppointmentZODSchema.searchAppointmentSchema, 'searchAppointmentSchema')
  @Get('/')
  async getAppointments(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TSearchAppointmentSchema = req.body;
      const resBody = await AppointmentRepository.getAppointments(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Get('/:id')
  async getAppointment(req: Request, res: Response, next: NextFunction) {
    try {
      const id: string = req.params.id;
      const resBody = await AppointmentRepository.getAppointment(id);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @RequestBody(AppointmentZODSchema.updateAppointmentSchema, 'updateAppointmentSchema')
  @Put('/')
  async updateAppointment(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TUpdateAppointmentSchema = req.body;
      const resBody = await AppointmentRepository.updateAppointment(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @RequestBody(AppointmentZODSchema.deleteAppointmentSchema, 'deleteAppointmentSchema')
  @Delete('/')
  async deleteAppointment(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TDeleteAppointmentSchema = req.body;
      const resBody = await AppointmentRepository.deleteAppointment(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
}

const appointmentsRoute = new AppointmentsRouter(parseAPIVersion(1) + '/appointments');
export const appointmentsRoutes = appointmentsRoute.getRoute(appointmentsRoute);