Class: HhPackage::PricingMenu

Inherits:
ApplicationRecord show all
Includes:
IdentityCache
Defined in:
app/models/hh_package/pricing_menu.rb

Overview

Schema Information

Table name: hh_package_pricing_menus

id                      :bigint           not null, primary key
name                    :string(191)
original_price_cents    :integer
original_price_currency :string(191)      default("THB")
package_type            :string(191)      not null
priority                :integer
section_type            :string(191)
selling_price_cents     :integer
selling_price_currency  :string(191)      default("THB")
created_at              :datetime         not null
updated_at              :datetime         not null
menu_group_section_id   :bigint
menu_section_id         :bigint
package_id              :bigint           not null

Indexes

index_pricing_menus_on_package_id_type  (package_id,package_type)

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Instance Method Details



52
53
54
# File 'app/models/hh_package/pricing_menu.rb', line 52

def menu_group_section_id
  read_attribute(:menu_group_section_id)
end

Explicitly define attribute readers to prevent Globalize from interfering when locale is 'id' (Indonesian), which would conflict with the _id suffix See: github.com/globalize/globalize/issues/XXX



48
49
50
# File 'app/models/hh_package/pricing_menu.rb', line 48

def menu_section_id
  read_attribute(:menu_section_id)
end

#pricing_as_json(keys) ⇒ Hash

Note:

Price values are returned in cents for precision

Note:

Formatted price strings include currency symbols and proper formatting

Note:

This method provides backward compatibility while offering improved structure

Converts pricing menu data to JSON format with structured price objects

This method transforms pricing menu attributes into a JSON-friendly format, grouping price-related fields into structured objects for better API consistency. Price information is returned as nested objects containing price, currency, and formatted display values.

Examples:

Basic usage with standard keys

pricing_menu.pricing_as_json(['id', 'name', 'original_price'])
# => {
#      "id" => 1,
#      "name" => "Appetizer",
#      "original_price" => {
#        "price" => 10000,
#        "currency" => "THB",
#        "format" => "฿100"
#      }
#    }

With both original and selling price

pricing_menu.pricing_as_json(['name', 'original_price', 'selling_price'])
# => {
#      "name" => "Main Course",
#      "original_price" => {
#        "price" => 15000,
#        "currency" => "THB",
#        "format" => "฿150"
#      },
#      "selling_price" => {
#        "price" => 12000,
#        "currency" => "THB",
#        "format" => "฿120"
#      }
#    }

Parameters:

  • keys (Array<String>)

    Array of attribute keys to include in the JSON output Supported keys:

    • 'id': Returns the pricing menu ID

    • 'name': Returns the pricing menu name

    • 'original_price': Returns structured original price object with price, currency, and format

    • 'selling_price': Returns structured selling price object with price, currency, and format

    • Any other valid attribute name will be dynamically included if the model responds to it

Returns:

  • (Hash)

    JSON-formatted hash containing the requested attributes



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
# File 'app/models/hh_package/pricing_menu.rb', line 163

def pricing_as_json(keys)
  original_price_format = HhMoney.format_rounded(original_price_cents, original_price_currency)
  selling_price_format = HhMoney.format_rounded(selling_price_cents, selling_price_currency)
  json = {}

  keys.each do |key|
    case key
    when 'id'
      json[key] = id
    when 'name'
      json[key] = name
    when 'original_price'
      # Direct request for original_price object
      json['original_price'] = {
        'price' => original_price_cents,
        'currency' => original_price_currency,
        'format' => original_price_format,
      }
    when 'selling_price'
      # Direct request for selling_price object
      json['selling_price'] = {
        'price' => selling_price_cents,
        'currency' => selling_price_currency,
        'format' => selling_price_format,
      }
    else
      # Handle any other keys that might be added in the future
      json[key] = send(key) if respond_to?(key)
    end
  end

  json
end

#send_pricing_menu_update_eventsvoid

This method returns an undefined value.

Sends update events to all restaurants associated with this pricing menu's package when pricing menu data changes. This ensures search indexes and dependent services are notified of pricing changes that may affect restaurant display and filtering.

The method iterates through all restaurant packages associated with the polymorphic package and sends update events for each active restaurant with pricing menu changes.

Examples:

When a pricing menu is updated

pricing_menu.update(selling_price_cents: 15000)
# Automatically triggers send_pricing_menu_update_events
# Sends UPDATE_EVENT to RESTAURANTS_TOPIC for each associated restaurant


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/hh_package/pricing_menu.rb', line 86

def send_pricing_menu_update_events
  return unless package&.restaurant_packages&.exists?

  package.restaurant_packages.active_scope.includes(:restaurant).find_each do |restaurant_package|
    restaurant = restaurant_package.restaurant

    # Only send event for bookable and not expired restaurant
    next unless restaurant&.bookable_and_not_expired?

    changed_attributes = { price_per_person: {}, price_per_pack: {} }
    send_update_pricing_event(restaurant.id, changed_attributes)
  end
end

#send_update_pricing_event(restaurant_id, changed_attributes) ⇒ void

This method returns an undefined value.

Sends update pricing event to the event-driven system for a specific restaurant

Parameters:

  • restaurant_id (Integer)

    The ID of the restaurant to send the event for

  • changed_attributes (Hash)

    Hash containing the changed pricing menu attributes



105
106
107
108
109
110
111
112
113
# File 'app/models/hh_package/pricing_menu.rb', line 105

def send_update_pricing_event(restaurant_id, changed_attributes)
  EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
    EventDrivenClient::Constants::RESTAURANTS_TOPIC,
    EventDrivenClient::Constants::UPDATE_EVENT,
    restaurant_id,
    changed_attributes,
    { source: 'HhPackage::PricingMenu#send_update_pricing_event' },
  )
end