Class: Weeloy::Reservations::Create

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, ApiHelpers
Defined in:
app/services/weeloy/reservations/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods included from ApiHelpers

#api_call, #channel, #find_weeloy_vendor_application, #get_purpose, #headers, #parse_phone_number, #parse_user_or_guest_full_name, #parsed_vendor_name, #reservation_start_at, #send_error_availability_notification_to_staff

Constructor Details

#initialize(reservation_id = nil, actor = nil, is_vendor_prepaid_reservation = false, reference_id = nil, reseller_reference_id = nil) ⇒ Create

Returns a new instance of Create.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/weeloy/reservations/create.rb', line 13

def initialize(reservation_id = nil, actor = nil, is_vendor_prepaid_reservation = false, reference_id = nil, reseller_reference_id = nil)
  if reservation_id.present?
    @reservation = Reservation.find(reservation_id).decorate
    BUSINESS_LOGGER.set_business_context({ reservation_id: reservation_id })
  end
  if actor.is_a?(String) && actor.to_sym == :user
    @blockage_id = @reservation&.inventory_weeloy_blockage&.blockage_id
  end
  @is_vendor_prepaid_reservation = is_vendor_prepaid_reservation
  @reference_id = reference_id
  @reseller_reference_id = reseller_reference_id
end

Instance Attribute Details

#blockage_idObject (readonly)

Returns the value of attribute blockage_id.



11
12
13
# File 'app/services/weeloy/reservations/create.rb', line 11

def blockage_id
  @blockage_id
end

#reservationObject (readonly)

Returns the value of attribute reservation.



11
12
13
# File 'app/services/weeloy/reservations/create.rb', line 11

def reservation
  @reservation
end

Instance Method Details

#executeObject



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
# File 'app/services/weeloy/reservations/create.rb', line 26

def execute
  errors.clear

  error_message = Weeloy::ErrorMessages::DEFAULT_ERROR
  # Check if reservation is blank
  if reservation.blank?
    error_message = 'Reservation is blank'
    APMErrorHandler.report("#{self.class} #{error_message}") unless Weeloy::ErrorMessages::KNOWN_ERRORS.include?(error_message)
    errors.add(:base, error_message)
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  # Check if restaurant is using Weeloy inventory source
  inv_source = reservation.restaurant.inventory_source&.inv_source
  if inv_source.blank? || inv_source != ApiVendorV1::Constants::WEELOY_INV_SOURCE_NAME
    error_message = "Restaurant #{reservation.restaurant_id} is not using Weeloy inventory source"
    unless Weeloy::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("#{self.class} #{error_message}", reservation_id: reservation.id,
                                                               restaurant_id: reservation.restaurant_id)
    end
    errors.add(:base, error_message)
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  shop_id = reservation.restaurant&.weeloy_restaurant&.shop_id
  shop_slug = reservation.restaurant&.weeloy_restaurant&.shop_slug
  if shop_id.blank? || shop_slug.blank?
    error_message = "Invalid shop id: #{shop_id} or shop slug: #{shop_slug} of restaurant #{reservation.restaurant_id}"
    unless Weeloy::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("#{self.class} #{error_message}", reservation_id: reservation.id,
                                                               restaurant_id: reservation.restaurant_id)
    end
    errors.add(:base, error_message)
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  if blockage_id.present?
    BUSINESS_LOGGER.info('WEELOY: blockage id is present', blockage_id: blockage_id)
    blockage_is_valid = validate_blockage
    unless blockage_is_valid.success?
      error_message = blockage_is_valid.message
      errors.add(:base, error_message)
      BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
      return ServiceResult.new errors: errors, message: error_message
    end
  else
    BUSINESS_LOGGER.info('WEELOY: blockage id is NOT present', blockage_id: blockage_id)
  end

  # skip create reservation if payment is not confirmed (for prepaid reservation)
  # commented out because we want to create reservation in Weeloy even if payment is not confirmed
  # return ServiceResult.new data: nil unless reservation.payment_is_confirmed?

  # Create reservation in Weeloy
  weeloy_reservation = create_reservation(shop_id, reservation_params)
  unless weeloy_reservation.success?
    error_message = weeloy_reservation.message
    errors.add(:base, error_message)

    # send notif to ops team if error related to inventory
    send_notification_to_error_staff(error_message) if reservation.created_by.to_sym == :user
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  # Create InventoryWeeloyBlockage
  inventory_weeloy_blockage = create_inventory_weeloy_blockage
  if inventory_weeloy_blockage.blank?
    error_message = "Error creating InventoryWeeloyBlockage reservation #{weeloy_reservation.data['reservation_id']} for reservation #{reservation.id}"
    unless Weeloy::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("#{self.class} #{error_message}", reservation_id: reservation.id,
                                                               restaurant_id: reservation.restaurant_id)
    end
    errors.add(:base, error_message)
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  # Create VendorReservation
  result = create_weeloy_reservation(weeloy_reservation.data)
  if result.blank?
    error_message = "Error creating Weeloy reservation #{weeloy_reservation.data['reservation_id']} for reservation #{reservation.id}"
    unless Weeloy::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("#{self.class} #{error_message}", reservation_id: reservation.id,
                                                               restaurant_id: reservation.restaurant_id)
    end
    errors.add(:base, error_message)
    BUSINESS_LOGGER.error('WEELOY:', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end
  # Create VendorPayment

  VendorsService::VendorPaymentService.new(reservation).save_vendor_payment if [:admin,
                                                                                :owner].include?(reservation.created_by.to_sym)

  ServiceResult.new data: result
end