Class: Agents::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Rescuable, SharedJobs
Defined in:
app/my_lib/agents/base.rb

Direct Known Subclasses

CreateBase, Update

Constant Summary collapse

WORKER_DELAY_TIME =
2.minutes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SharedJobs

#give_campaign_reward, #send_rating_email

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (protected)



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/my_lib/agents/base.rb', line 239

def method_missing(method_sym, *arguments, &block)
  if reservation.is_a?(::Reservation)
    if reservation.respond_to?(method_sym)
      begin
        reservation.send(method_sym, *arguments, &block)
      rescue ArgumentError
        reservation.send(method_sym)
      end

    elsif reservation.has_attribute?(method_sym)
      reservation.send(method_sym)
    else
      super
    end

  else
    super
  end
end

Instance Attribute Details

#audit_commentObject

Returns the value of attribute audit_comment.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def audit_comment
  @audit_comment
end

#errorsObject

Returns the value of attribute errors.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def errors
  @errors
end

#executorObject

Returns the value of attribute executor.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def executor
  @executor
end

#force_updateObject

Returns the value of attribute force_update.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def force_update
  @force_update
end

#ownerObject

Returns the value of attribute owner.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def owner
  @owner
end

#reservationObject

Returns the value of attribute reservation.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def reservation
  @reservation
end

#reservation_paramsObject

Returns the value of attribute reservation_params.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def reservation_params
  @reservation_params
end

#restaurantObject

Returns the value of attribute restaurant.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def restaurant
  @restaurant
end

#userObject

Returns the value of attribute user.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def user
  @user
end

#vendor_booking_idObject

Returns the value of attribute vendor_booking_id.



12
13
14
# File 'app/my_lib/agents/base.rb', line 12

def vendor_booking_id
  @vendor_booking_id
end

Instance Method Details

#error_messageObject



27
28
29
# File 'app/my_lib/agents/base.rb', line 27

def error_message
  errors.full_messages.uniq.to_sentence
end

#hotlineObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/my_lib/agents/base.rb', line 119

def hotline
  return AdminSetting.hotline_channel if reservation.blank?

  booking_channel = reservation.booking_channel
  return '' if booking_channel.blank?

  if booking_channel.using_restaurant_phone?
    return restaurant.phone if restaurant

    AdminSetting.hotline_channel
  elsif booking_channel.using_custom_phone?
    reservation.booking_channel.custom_phone
  elsif booking_channel.using_default_phone?
    AdminSetting.hotline_channel
  else
    APMErrorHandler.report('Undefined phone type', channel: reservation.channel)
    AdminSetting.hotline_channel
  end
end

#inventory_available?(adult = reservation.adult, kids = reservation.kids) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/my_lib/agents/base.rb', line 139

def inventory_available?(adult = reservation.adult, kids = reservation.kids)
  inv_checker = InvCheckerFactory.new(restaurant.id, restaurant.time_zone).create_inv_checker_service

  # For reservation updates, try to get the original adult count
  original_adult = if instance_variable_defined?(:@old_reservation_attributes) && @old_reservation_attributes.present?
                     @old_reservation_attributes['adult']
                   end

  BUSINESS_LOGGER.info(
    'Checking inventory availability',
    {
      reservation_id: reservation.id,
      adult: adult,
      kids: kids,
      original_adult: original_adult,
      has_packages: reservation.package.present?,
      is_update_scenario: original_adult.present?,
    },
  )

  if reservation.package.present?
    slugs = reservation.package[:package_data].map do |pd|
      HhPackage::RestaurantPackage.fetch(pd[:restaurant_package_id]).slug
    end

    is_available = slugs.select do |slug|
      # Check if the inventory service supports the adult_from parameter
      if original_adult.present?
        inv_checker.package_bookable?(date: reservation.date,
                                      start_time: reservation.start_time_format,
                                      adult: adult,
                                      kids: kids,
                                      slug: slug,
                                      adult_from: original_adult)
      else
        inv_checker.package_bookable?(date: reservation.date,
                                      start_time: reservation.start_time_format,
                                      adult: adult,
                                      kids: kids,
                                      slug: slug)
      end
    end

    if is_available.present?
      BUSINESS_LOGGER.info(
        'Package inventory check passed',
        {
          reservation_id: reservation.id,
          available_packages: is_available.size,
          total_packages: slugs.size,
        },
      )
      return true
    end
  elsif inv_checker.inventory_bookable?(date: reservation.date, start_time: reservation.start_time_format,
                                        adult: adult, kids: kids)
    BUSINESS_LOGGER.info(
      'Non-package inventory check passed',
      {
        reservation_id: reservation.id,
        adult: adult,
        kids: kids,
      },
    )
    return true
  end

  BUSINESS_LOGGER.warn(
    'Inventory availability check failed',
    {
      reservation_id: reservation.id,
      adult: adult,
      kids: kids,
      original_adult: original_adult,
      error_message: inv_checker.error_message,
    },
  )

  reservation.errors.add :base, inv_checker.error_message
  inventory_error!

  false
end

#save_reservation!(*args) ⇒ Object



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
# File 'app/my_lib/agents/base.rb', line 31

def save_reservation!(*args)
  reservation.audit_comment = [reservation.audit_comment, audit_comment].to_sentence if audit_comment
  reservation.build_property if reservation.property.nil?

  if Channel.manual.include?(reservation.channel)
    raise InvalidReservation, 'Maximum party size is 100' if reservation.party_size > 100

    args = args[0]

    if args && (args == :sneaky || (args.key?(:sneaky) && args[:sneaky]))
      if reservation.created_at.blank?
        reservation.created_at = reservation.updated_at = Time.zone.now
      else
        reservation.updated_at = Time.zone.now
      end

      reservation.save(:sneaky)
      reservation.property.save if reservation.property.changed?
      return true
    elsif reservation.save(validate: false)
      reservation.property.save if reservation.property.changed?
      return true
    end

    raise InvalidReservation, error_message
  elsif !args.nil? && args[0] && (args[0] == :sneaky || (args[0].key?(:sneaky) && args[0][:sneaky]))
    if reservation.created_at.blank?
      reservation.created_at = reservation.updated_at = Time.zone.now
    else
      reservation.updated_at = Time.zone.now
    end

    reservation.save(:sneaky)
    reservation.property.save if reservation.property.changed?
    true
  else
    raise InvalidReservation, error_message unless reservation.save(*args)

    reservation.property.save if reservation.property.changed?
    true
  end
end

#status=(status) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/my_lib/agents/base.rb', line 74

def status=(status)
  case status
  when 'pending'
    reservation.mark_as_valid_reservation!
  when 'no_show'
    reservation.mark_as_no_show!
  when 'arrive'
    reservation.mark_as_arrived!
    reservation.mark_voucher_as_active!
  when 'cancel'
    reservation.mark_voucher_as_inactive!
    reservation.mark_as_canceled!
  else
    APMErrorHandler.report('undetected status', status: status)
  end
end