All files / src/controllers consultant-details.controller.ts

74.41% Statements 32/43
20% Branches 2/10
100% Functions 4/4
73.17% Lines 30/41

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 771x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x         1x   2x 2x 2x 2x           1x 1x 1x 1x 1x 1x                     1x             1x 1x 1x 1x     1x 1x                               1x      
import {authenticate, AuthenticationBindings} from '@loopback/authentication';
import {UserRepository} from '@loopback/authentication-jwt';
import {inject} from '@loopback/core';
import {repository} from '@loopback/repository';
import {del, get, param, post, requestBody, response, Response, RestBindings} from '@loopback/rest';
import {roles} from '../config';
import {ConsultantDetailsRepository} from '../repositories';
import errorMessages from '../utils/errorMessages';
import {log} from "../utils/logMethod";
import successMessages from '../utils/successMessages';
import {failureHandler, getUID, isAdmin, successHandler, validatePayload} from './components';
import {baseResponse, consultantDetails} from "./requestSpecs";
import {currentUserType} from './types';
import {CreateType} from './types/consultant-details.controller.types';
 
@authenticate("jwt")
export class ConsultantDetailsController {
  constructor(
    @repository(ConsultantDetailsRepository) public consultantDetailsRepository: ConsultantDetailsRepository,
    @inject(RestBindings.Http.RESPONSE) protected res: Response,
    @inject(AuthenticationBindings.CURRENT_USER) public currentUser: currentUserType,
    @repository(UserRepository) public userRepository: UserRepository,
  ) { }
 
  @post('/consultant')
  @log()
  @response(200, consultantDetails)
  async create(@requestBody(consultantDetails) consultant: CreateType,): Promise<Response> {
    let methodName = "create"
    try {
      validatePayload(consultant, consultantDetails)
      Eif (!isAdmin(this.currentUser))
        throw {code: 403, message: errorMessages.permissionDenied, methodName}
      const foundConsultant = await this.userRepository.findOne({where: {UID: consultant.consultantUID}, include: ['role']})
      if (!foundConsultant || foundConsultant.role.roleType !== roles.consultant)
        throw {code: 422, message: errorMessages.dataNotFound, methodName}
      let ifBadgeExist = await this.consultantDetailsRepository.findOne({where: {consultantUID: consultant.consultantUID, badgeName: consultant.badgeName, }});
      if (ifBadgeExist)
        throw {code: 422, message: errorMessages.dataAlreadyExist, methodName}
      consultant.UID = getUID()
      const newBadge = await this.consultantDetailsRepository.create(consultant);
      return successHandler(this.res, 200, newBadge)
    } catch (error) {
      return this.failureHandler(this.res, 403, error)
    }
  }
 
  @get('/getConsultantBadges/{UID}')
  @response(200, baseResponse)
  @log()
  async getConsultantBadges(@param.path.string('UID') UID: string): Promise<any> {
    let methodName = "getConsultantBadges"
    try {
      Iif (!UID) {
        throw {code: 400, message: errorMessages.requiredFieldIsMissing, methodName}
      }
      const badges = await this.consultantDetailsRepository.find({where: {consultantUID: UID}})
      return successHandler(this.res, 200, badges)
    } catch (error) {
      return this.failureHandler(this.res, 422, error)
    }
  }
  // @del('/consultant/{UID}')
  // @log()
  // async deleteById(@param.path.string('UID') UID: string): Promise<Response> {
  //   try {
  //     await this.consultantDetailsRepository.deleteAll({UID});
  //     return successHandler(this.res, 200, successMessages.deletedSuccessfully)
  //   } catch (error) {
  //     return this.failureHandler(this.res, 403, error)
  //   }
  // }
  private failureHandler(res: Response, code: number, error: any): Promise<Response> {
    return failureHandler(res, code, error, ConsultantDetailsController.name)
  }
}