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

84.68% Statements 94/111
67.69% Branches 44/65
93.1% Functions 27/29
90% Lines 90/100

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159  7x 63x 63x   63x   7x 189x   7x 17x 17x 17x   34x 17x     7x 7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x     7x     4x 4x 4x 4x 4x 4x               4x 4x 4x 4x 4x 4x               1x 1x 1x 1x 1x               3x 3x 3x 3x 3x 2x     1x         1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x               7x          
import { checkPermission } from '@/middlewares/permission.middleware';
import { PatientRepository } from '@/repositories/patient.repo';
import { PatientZODSchema } from '@/schemas/patient/patient.schema';
import { PermissionModel, PermissionVerb } from '@/services/permissions.service';
import HtEtpStatusCode from '@/utils/HTTPStatusCodes';
import { NextFunction, Request, Response } from 'express';
import {
  AuthGuard,
  DeElete,
  Get,
  Middlewares,
  Post,
  Put,
  RequestBody,
} from '@/decorators/router.decorator';
import { MainRouter } from '../router';
import { parseAPIVersion } from '@/config/app.config';
 
class PatientsRouter extends MainRouter {
  constructor(prefix: string) {
    super(prefix, 'Patients');
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.CREATE)])
  @RequestBody(PatientZODSchema.createPatientSchema, 'createPatientSchema')
  @Post('/')
  async createPatient(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TCreatePatientSchema = req.body;
      const resBody = await PatientRepository.createPatient(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.READ)])
  @RequestBody(PatientZODSchema.searchPatientSchema, 'searchPatientSchema')
  @Get('/')
  async getPatients(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TSearchPatientSchema = req.body;
      const resBody = await PatientRepository.getPatients(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.READ)])
  @Get('/me')
  async getPatientProfile(req: Request, res: Response, next: NextFunction) {
    try {
      const resBody = await PatientRepository.getPatient();
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.READ)])
  @Get('/:patientId')
  async getPatient(req: Request, res: Response, next: NextFunction) {
    try {
      const patientId: string = req.params.patientId;
      const resBody = await PatientRepository.getPatient(patientId);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.UPDATE)])
  @RequestBody(PatientZODSchema.updatePatientSchema, 'updatePatientSchema')
  @Put('/')
  async updatePatient(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TUpdatePatientSchema = req.body;
      const resBody = await PatientRepository.updatePatient(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.DELETE)])
  @RequestBody(PatientZODSchema.deletePatientSchema, 'deletePatientSchema')
  @Delete('/')
  async deletePatient(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TDeletePatientSchema = req.body;
      const resBody = await PatientRepository.deletePatient(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }

  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.DELETE)])
  @RequestBody(PatientZODSchema.validateDeleteSchema, 'validateDeleteSchema')
  @Post('/confirm-delete')
  async confirmDeletePatient(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TValidateDeleteSchema = req.body;
      const resBody = await PatientRepository.confirmDeletePatient(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.UPDATE)])
  @RequestBody(PatientZODSchema.addDoctorSchema, 'addDoctorSchema')
  @Post('/add-doctor')
  async addDoctor(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TAddDoctorSchema = req.body;
      const resBody = await PatientRepository.addDoctor(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
 
  @AuthGuard()
  @Middlewares([checkPermission(PermissionModel.PATIENT, PermissionVerb.UPDATE)])
  @RequestBody(PatientZODSchema.addDoctorSchema, 'addDoctorSchema')
  @Post('/remove-doctor')
  async removeDoctor(req: Request, res: Response, next: NextFunction) {
    try {
      const body: TAddDoctorSchema = req.body;
      const resBody = await PatientRepository.removeDoctor(body);
      res.status(resBody.error ? resBody.error.code : HttpStatusCode.OK).json(resBody);
      next();
    } catch (err) {
      next(err);
    }
  }
}
 
const patientsRoute = new PatientsRouter(parseAPIVersion(1) + '/patients');
export const patientsRoutes = patientsRoute.getRoute(patientsRoute);