All files / platform/modules/booking/src/Services TargetSystemService.js

92.59% Statements 25/27
78.57% Branches 11/14
100% Functions 4/4
100% Lines 25/25

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    1x   1x 1x       129x       129x 129x       3x 3x   3x 3x 3x     1x                   2x                       2x             6x 6x 6x   6x 6x 6x     3x   3x                                 3x 3x               1x  
'use strict';
 
const _ = require('lodash');
 
const CE = use('C2C/Exceptions');
const Logger = use('Logger');
 
class TargetSystemService {
  static get inject() {
    return ['C2C/Services/SystemLinkService', 'C2C/Services/RequestService'];
  }
 
  constructor(systemLinkService, RequestService) {
    this.systemLinkService = systemLinkService;
    this.requestService = RequestService;
  }
 
  async requestChangeBookingDate({ bookingId, systemId, body }) {
    const systemLink = await this.systemLinkService.getHookForChangeBookingDate(systemId);
    Iif (!systemLink) return {};
 
    const link = systemLink.link.replace('{bookingId}', bookingId);
    try {
      const result = await this.requestService.put(link, body, {
        systemId: systemLink.systemId,
      });
      Logger.info('TargetSystemService -> requestChangeBookingDate: Success', {
        level: 'info',
        method: 'PUT',
        url: link,
        data: body,
        response: {
          data: result,
        },
      });
    } catch (err) {
      Logger.error('TargetSystemService -> requestChangeBookingDate: Failed', {
        level: 'error',
        method: 'PUT',
        url: link,
        data: body,
        response: {
          data: {
            errorMessage: err.message,
            errorData: err.response && err.response.data,
          },
        },
      });
      throw CE.BadRequestException.raise(
        _.get(err, 'response.data.data.message') || 'errors.somethingWrong',
      );
    }
  }
 
  async requestToUpdateBooking({ systemId, bookingId, body }) {
    const systemLink = await this.systemLinkService.getHookBeforeUpdateBooking(systemId);
    Iif (!systemLink) return {};
    const requestStartDate = new Date();
 
    const link = systemLink.link.replace('{bookingId}', bookingId);
    try {
      const result = await this.requestService.put(link, body, {
        systemId: systemId,
      });
      return _.get(result, 'data');
    } catch (err) {
      Logger.error('BookingService -> sendBookingInfoToTargetSystem: Failed', {
        level: 'error',
        method: 'PUT',
        url: link,
        bookingId: bookingId,
        systemId: systemId,
        requestStartDate: requestStartDate,
        data: body,
        response: {
          err: err,
          data: {
            errorMessage: err.message,
            errorData: err.response && err.response.data,
          },
        },
      });
      // MEMO: timeoutはnailie-api側で処理が60000ms以上かかっているが処理は完了しているためエラーを出さず後続処理へ続ける
      Eif (!err.message?.includes('timeout')) {
        throw CE.BadRequestException.raise(
          _.get(err, 'response.data.data.message') || 'errors.somethingWrong',
        );
      }
    }
  }
}
 
module.exports = TargetSystemService;