Module: SevenRooms::ApiHelpers

Includes:
MoneyRails::ActionViewExtension
Included in:
Authentication, SevenRooms::Availabilities::Availability, Blockages::Create, Reservations::Cancel, Reservations::Create, Reservations::Detail, Reservations::Update, Restaurants::Detail
Defined in:
app/services/seven_rooms/api_helpers.rb

Overview

This module contains helper methods for interacting with the SevenRooms API.

Instance Method Summary collapse

Instance Method Details

#api_uriObject



13
14
15
# File 'app/services/seven_rooms/api_helpers.rb', line 13

def api_uri
  AdminSetting.seven_rooms_api_host.to_s
end

#auth_headersHash

Returns the authentication headers for API requests.

Returns:

  • (Hash)

    The authentication headers.



20
21
22
23
24
25
# File 'app/services/seven_rooms/api_helpers.rb', line 20

def auth_headers
  {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/json',
  }
end

#common_headers(auth_token) ⇒ Hash

Generates common headers for API requests.

Parameters:

  • auth_token (String)

    The authentication token.

Returns:

  • (Hash)

    The common headers.



31
32
33
34
35
36
# File 'app/services/seven_rooms/api_helpers.rb', line 31

def common_headers(auth_token)
  {
    'Accept' => 'application/json',
    'Authorization' => auth_token,
  }
end

#find_seven_rooms_vendor_applicationObject



71
72
73
74
75
76
77
78
# File 'app/services/seven_rooms/api_helpers.rb', line 71

def find_seven_rooms_vendor_application
  inv_source_name = ApiVendorV1::Constants::SEVEN_ROOMS_INV_SOURCE_NAME
  if reservation.third_party_indicator.present?
    Doorkeeper::Application.find_by(name: reservation.booking_channel.name)
  else
    Doorkeeper::Application.find_by(name: inv_source_name)
  end
end

#parse_user_or_guest_full_name(reservation = nil) ⇒ Hash

Parses the full name of a user or guest and returns a hash containing the first name and last name.

Parameters:

  • full_name (String)

    The full name to be parsed.

Returns:

  • (Hash)

    A hash containing the first name and last name.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/seven_rooms/api_helpers.rb', line 42

def parse_user_or_guest_full_name(reservation = nil)
  reservation_name = {
    first_name: 'HungryHub',
    last_name: 'Customer',
  }
  return reservation_name if reservation.blank?

  parsed_name = reservation.name.split
  reservation_name[:first_name] = parsed_name.first
  reservation_name[:last_name] = parsed_name.length > 1 ? parsed_name[1..].join(' ') : ''

  reservation_name
end

#parsed_vendor_name(reservation) ⇒ Object



129
130
131
132
133
134
# File 'app/services/seven_rooms/api_helpers.rb', line 129

def parsed_vendor_name(reservation)
  vendor_name = reservation&.vendor_reservation&.oauth_application&.name
  return '' if vendor_name.blank?

  vendor_name.to_s.split('HH x ').last
end

#send_error_availability_notification_to_staff(reservation, error_message, date, start_time, party_size, operation_type = :update) ⇒ Object



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
126
127
# File 'app/services/seven_rooms/api_helpers.rb', line 93

def send_error_availability_notification_to_staff(
  reservation, error_message, date, start_time, party_size, operation_type = :update
)
  is_credit_card_error = sevenrooms_credit_card_error?(error_message)
  # Send notifications for inventory errors and credit card requirement errors
  return unless SevenRooms::ErrorMessages::INVENTORY_ERRORS.include?(error_message) ||
    is_credit_card_error

  # Use different subject for credit card errors
  subject = if is_credit_card_error
              "Booking Rejected – Credit Card Required (SevenRooms #{reservation.restaurant_id})"
            else
              "SevenRooms #{operation_type.capitalize}: #{error_message}"
            end

  body = <<~BODY
    #{if reservation.old_reservation_id.present?
        "Old Reservation ID: #{reservation.old_reservation_id}<br>New Reservation ID: #{reservation.id}<br>"
      else
        "Reservation ID: #{reservation.id}<br>"
      end}
    Restaurant: #{reservation.restaurant&.name_en}<br>
    Date: #{date}<br>
    Time: #{start_time}<br>
    Party Size: #{party_size}<br>
    Message: #{error_message}
  BODY

  # Get account manager email if available
   = reservation.restaurant&.user&.email
  receivers = [::MERCHANT_EMAIL, ::VENDOR_TEAM_EMAIL]
  receivers <<  if .present?

  StaffMailer.notify_error_staff(subject, body, { receivers: receivers }).deliver_later!
end

#sevenrooms_credit_card_error?(error_message) ⇒ Boolean

Checks if an error message is related to credit card requirements

Parameters:

  • error_message (String)

    The error message to check

Returns:

  • (Boolean)

    true if the error is credit card related, false otherwise



84
85
86
87
88
89
90
91
# File 'app/services/seven_rooms/api_helpers.rb', line 84

def sevenrooms_credit_card_error?(error_message)
  return false if error_message.blank?

  error_message_downcased = error_message.downcase
  SevenRooms::ErrorMessages::CREDIT_CARD_ERRORS.any? do |error|
    error_message == error || error_message_downcased.include?(error.downcase)
  end
end

#standardize_or_default_phone(phone_number = nil) ⇒ String

Standardizes a given phone number to E.164 format or returns a default support phone number in E.164 format.

Parameters:

  • phone_number (String, nil) (defaults to: nil)

    the phone no. to be standardized. If nil, default support phone no. is returned.

Returns:

  • (String)

    the standardized phone no. in E.164 format, or default support phone no. in E.164 format if the given phone number is invalid or nil.



61
62
63
64
65
66
67
68
69
# File 'app/services/seven_rooms/api_helpers.rb', line 61

def standardize_or_default_phone(phone_number = nil)
  parsed_phone = Phonelib.parse(phone_number)

  if parsed_phone.valid?
    parsed_phone.full_e164
  else
    Phonelib.parse(AdminSetting.support_phone).full_e164
  end
end