Class: HhPackage::ReservationPackages::Validator

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/my_lib/hh_package/reservation_packages/validator.rb

Overview

Validate given packages is it enough to cover the party size

Defined Under Namespace

Classes: InvalidPackage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initializeValidator

Returns a new instance of Validator.



13
14
15
16
17
18
19
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 13

def initialize
  self.use_validation = true
  self.adult = 0
  self.kids = 0
  self.group_booking = false
  self.data = []
end

Instance Attribute Details

#use_validationObject

Returns the value of attribute use_validation.



11
12
13
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 11

def use_validation
  @use_validation
end

Class Method Details

.validate_minimum_spending(packages, total_package_price, adult = 0) ⇒ String?

Validates minimum spending requirements for DIY packages Can be called directly without setting up the full validator

Parameters:

  • packages (Array)

    Array of package items

  • total_package_price (Numeric)

    Primary total package price

  • total_package_price_v2 (Numeric)

    Secondary total package price

  • adult (Integer) (defaults to: 0)

    Number of adults in the reservation

Returns:

  • (String, nil)

    Returns error message if validation fails, nil if valid



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
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 89

def self.validate_minimum_spending(packages, total_package_price, adult = 0)
  packages.each do |item|
    # Handle both package and restaurant_package formats
    package = if item.key?(:package)
                item[:package]
              elsif item.key?(:restaurant_package)
                item[:restaurant_package].fetch_package
              elsif item.key?(:restaurant_package_id)
                rp = HhPackage::RestaurantPackage.find_by(id: item[:restaurant_package_id])
                next if rp.nil?

                rp.fetch_package
              else
                next
              end

    next unless package.type_short == HhPackage::Package::Diy::TYPE_SHORT

    # Get the pricing configuration for DIY package
    pricing = package.pricings&.order(updated_at: :desc)&.first
    next unless pricing.require_min_spending?

    min_spending_type = pricing.min_spending_type
    min_spending_amount = pricing.min_spending.amount
    currency = pricing.min_spending_currency || 'THB'

    case min_spending_type
    when HhPackage::Pricing::MIN_SPENDING_PER_PERSON_TYPE
      next if adult < 1 # Skip validation if no adults

      spend_amount_per_person = total_package_price / adult
      next if spend_amount_per_person >= min_spending_amount

      return I18n.t(
        'errors.package.diy.min_spending_per_person_not_met',
        min_amount: HhMoney.new(pricing.min_spending_cents, currency).default_format,
        package_name: package.name,
        current_amount: HhMoney.from_amount(spend_amount_per_person, currency).default_format,
      )
    when HhPackage::Pricing::MIN_SPENDING_PER_BOOKING_TYPE
      # Do nothing, we don't have min spending per booking validation yet
      next
    else
      APMErrorHandler.report("Invalid minimum spending type #{min_spending_type}")
      next
    end
  end

  nil # All minimum spending requirements met
end

Instance Method Details

#is_group_booking(group_booking) ⇒ Object



52
53
54
55
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 52

def is_group_booking(group_booking)
  self.group_booking = group_booking
  self
end

#primary_package_present?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 67

def primary_package_present?
  primary_packages = restaurant_packages.select do |rp|
    package = rp.fetch_package
    is_add_on = package.respond_to?(:is_add_on?) ? package.is_add_on? : false
    is_add_on == false
  end
  return true if primary_packages.present?

  errors.add(:base, I18n.t('errors.package.primary_package_missing'))
  false
end

#select(data) ⇒ Object

Examples:

[
  {
    restaurant_package: restaurant_package_a,
    id: 1,
    quantity: 1
  },
  {
    restaurant_package: restaurant_package_b,
    id: 2,
    quantity: 2
]

it's better to pass :id instead of :restaurant_package attribute
in the future it should read :id attribute only

Parameters:

  • data (Array)


37
38
39
40
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 37

def select(data)
  self.data = data
  self
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 57

def valid?
  use_validation && party_covered?
  use_validation && primary_package_present?
  errors.full_messages.blank?
rescue StandardError => e
  APMErrorHandler.report('package validator error', e: e)
  errors.add(:base, 'Something went wrong') if errors.full_messages.blank?
  false
end

#with_adult(adult) ⇒ Object



42
43
44
45
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 42

def with_adult(adult)
  self.adult = adult
  self
end

#with_kids(kids) ⇒ Object



47
48
49
50
# File 'app/my_lib/hh_package/reservation_packages/validator.rb', line 47

def with_kids(kids)
  self.kids = kids
  self
end