Module: RefundGuaranteeHelper

Included in:
Agents::ModifyBookingForUser, ModelExt::Reservations::InstanceMethods
Defined in:
app/my_lib/refund_guarantee_helper.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#calculate_min_refund_hours(reservation) ⇒ Integer

Calculates the minimum refund hours required based on reservation's packages and add-ons

Parameters:

  • reservation (Reservation)

    The reservation to evaluate

Returns:

  • (Integer)

    The maximum refund hours required



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
# File 'app/my_lib/refund_guarantee_helper.rb', line 10

def calculate_min_refund_hours(reservation)
  max_hours = nil

  # Check packages
  if reservation.package[:package_data].present?
    reservation.package[:package_data].each do |pkg_data|
      restaurant_package_id = pkg_data[:restaurant_package_id]
      next if restaurant_package_id.blank?

      restaurant_package = HhPackage::RestaurantPackage.find_by(id: restaurant_package_id)
      next if restaurant_package.blank?

      package = restaurant_package.package
      refund_hours = package&.package_attr&.refund_hours_in_advance || 0
      max_hours = refund_hours if max_hours.nil? || refund_hours > max_hours
    end
  end

  # Check add-ons
  if reservation.add_on[:add_on_data].present?
    reservation.add_on[:add_on_data].each do |addon_data|
      restaurant_add_on_id = addon_data[:restaurant_add_on_id]
      next if restaurant_add_on_id.blank?

      restaurant_add_on = AddOns::Restaurant.find_by(id: restaurant_add_on_id)
      next if restaurant_add_on.blank?

      add_on = restaurant_add_on.add_on
      refund_hours = add_on&.refund_hours_in_advance || 0
      max_hours = refund_hours if max_hours.nil? || refund_hours > max_hours
    end
  end

  max_hours || 0
end

#calculate_refundable_until_time(reservation) ⇒ Time

Calculates the max refundable_until time for a reservation

Parameters:

  • reservation (Reservation)

    The reservation to evaluate

Returns:

  • (Time)

    The calculated refundable_until time in UTC



51
52
53
54
55
56
57
58
59
60
# File 'app/my_lib/refund_guarantee_helper.rb', line 51

def calculate_refundable_until_time(reservation)
  restaurant_tz = reservation.restaurant.time_zone || HungryHub::Time::DEFAULT_ZONE
  min_refund_hours = calculate_min_refund_hours(reservation)

  reservation_date_time = Time.use_zone(restaurant_tz) do
    Time.zone.parse("#{reservation.date} #{reservation.start_time_format}")
  end

  (reservation_date_time - min_refund_hours.hours).utc
end