src/controllers/requestController.js
/**
   * @description This helps a manager to approve request created by his direct report
   * @param  {object} req - The request object
   * @param  {object} res - The response object
   * @returns  {object} The response object
   */
@description This helps a manager to approve request created by his direct report
@param  {object} req - The request object
@param  {object} res - The response object
@returns  {object} The response object
@description This helps a manager to approve request created by his direct report @param { object } req - The request object @param { object } res - The response object @returns { object } The response object
static async approve(req, res) {
    try {
      const { userData } = req;
      const id = parseInt(req.params.id, 10);
      let response;
      const directReportIds = await RequestService.retrieveDirectReports(userData);
      const { user_id: userId, status } = await RequestRepository.findById(id);
      if (directReportIds.length === 0 || !userId || !directReportIds.includes(userId)) {
        response = new Response(res, 404, 'Request not found in your direct reports');
        return response.sendErrorMessage();
      }
      if (status === 'Approved') {
        response = new Response(res, 400, 'Request is already approved');
        return response.sendErrorMessage();
      }
      const request = await RequestService.approveRequest(id);
      await request[1].dataValues.destination.forEach((obj) => {
        sequelize.query(
          'UPDATE "Locations" SET visit_count = visit_count + 1 WHERE destination = :obj',
          {
            replacements: { obj: obj.toLowerCase() }
          }
        );
      });
      const notification = {
        eventType: 'approved_request',
        requestId: id
      };
      eventEmitter.emit('notification', notification);
      response = new Response(res, 200, 'Request is sucessfully approved', request[1]);
      return response.sendSuccessResponse();
    } catch (error) {
      return DbErrorHandler.handleSignupError(res, error);
    }
  }
static async approve ( req , res ) { try { const { userData } = req ; const id = parseInt ( req . params . id , 10 ) ; let response ; const directReportIds = await RequestService . retrieveDirectReports ( userData ) ; const { user_id : userId , status } = await RequestRepository . findById ( id ) ; if ( directReportIds . length === 0 || ! userId || ! directReportIds . includes ( userId ) ) { response = new Response ( res , 404 , 'Request not found in your direct reports' ) ; return response . sendErrorMessage ( ) ; } if ( status === 'Approved' ) { response = new Response ( res , 400 , 'Request is already approved' ) ; return response . sendErrorMessage ( ) ; } const request = await RequestService . approveRequest ( id ) ; await request [ 1 ] . dataValues . destination . forEach ( ( obj ) => { sequelize . query ( 'UPDATE "Locations" SET visit_count = visit_count + 1 WHERE destination = :obj' , { replacements : { obj : obj . toLowerCase ( ) } } ) ; } ) ; const notification = { eventType : 'approved_request' , requestId : id } ; eventEmitter . emit ( 'notification' , notification ) ; response = new Response ( res , 200 , 'Request is sucessfully approved' , request [ 1 ] ) ; return response . sendSuccessResponse ( ) ; } catch ( error ) { return DbErrorHandler . handleSignupError ( res , error ) ; } }
@description This helps a manager to approve request created by his direct report
 @param  {object} req - The request object
 @param  {object} res - The response object
 @returns  {object} The response object
@description This helps a manager to approve request created by his direct report @param { object } req - The request object @param { object } res - The response object @returns { object } The response object