Module: Api::Vendor::V1::Dianping::Concerns::ReservationErrorResponses

Extended by:
ActiveSupport::Concern
Included in:
ReservationsController
Defined in:
app/controllers/api/vendor/v1/dianping/concerns/reservation_error_responses.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#render_error(data = {}) ⇒ Object



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
# File 'app/controllers/api/vendor/v1/dianping/concerns/reservation_error_responses.rb', line 7

def render_error(data = {})
  ota_order_status = if [:occupy, :query, nil, ''].include?(data[:method])
                       ApiVendorV1::Constants::DIANPING_OCCUPY_FAILED_STATUS
                     elsif data[:method] == :release
                       ApiVendorV1::Constants::DIANPING_RELEASE_FAILED_STATUS
                     elsif data[:method] == :confirm
                       ApiVendorV1::Constants::DIANPING_CONFIRM_FAILED_STATUS
                     elsif data[:method] == :cancel
                       ApiVendorV1::Constants::DIANPING_CANCEL_FAILED_STATUS
                     else
                       raise NotImplementedError
                     end

  order_data = {
    msg: data[:msg],
    code: data[:code],
    vendor_reference_id: vendor_reference_id,
    is_success: false,
    vendor_reservation_id: vendor_reservation&.id,
    ota_order_status: ota_order_status,
    refund_id: data[:refund_id],
  }

  report_error(order_data.merge(params: parsed_data, method: data[:method]))

  if %i[confirm cancel].include?(data[:method])
    worker_data = order_data.slice(:vendor_reference_id, :vendor_reservation_id, :ota_order_status, :refund_id)
    worker_data[:method] = data[:method]
    worker_data[:reservation_id] = reservation&.id
    Vendors::Dianping::ReservationWebhookWorker.perform_async(worker_data)
  end

  serialized_data = Api::Vendor::V1::Dianping::ReservationSerializer.new(OpenStruct.new(order_data))
  VendorLogger.log_event(:response, params[:route], payload: serialized_data)
  render json: serialized_data, status: :ok
end

#report_error(data) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'app/controllers/api/vendor/v1/dianping/concerns/reservation_error_responses.rb', line 44

def report_error(data)
  error_heading = "Api:Dianping:#{data[:method]}: #{data[:msg]}"
  BUSINESS_LOGGER.set_business_context({ vendor_reservation_id: data[:vendor_reservation_id] })
  BUSINESS_LOGGER.error(error_heading, data: data)
  return if hh_reservation_inventory_errors.include?(data[:msg])

  APMErrorHandler.report(error_heading, data: data, vendor_reservation_id: data[:vendor_reservation_id])
  send_notify_email(data)
end

#send_notify_email(data) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/controllers/api/vendor/v1/dianping/concerns/reservation_error_responses.rb', line 54

def send_notify_email(data)
  receivers = [::ERROR_RECIPIENT_EMAIL, ::SUPPORT_EMAIL, ::ZUL_EMAIL]
  subject = "#{data[:error_from]}: #{data[:msg]}"
  body = <<~BODY
    #{data}
  BODY
  StaffMailer.notify(subject, body, receivers).deliver_later!
end