Class: ComparingPackageService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/comparing_package_service.rb

Overview

Service class to handle package comparison logic for both PartyPack and Ayce packages Extracts complex business logic from the controller and provides better separation of concerns

Constant Summary collapse

PARTY_PACK_TYPE =
'HhPackage::Package::PartyPack'.freeze
AYCE_TYPE =
'HhPackage::Package::Ayce'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(params, package_type) ⇒ ComparingPackageService

Returns a new instance of ComparingPackageService.



9
10
11
12
13
14
15
# File 'app/services/comparing_package_service.rb', line 9

def initialize(params, package_type)
  @params = params
  @package_type = package_type
  @menu_group_id = params[:menu_group_id]
  @restaurant_id = params[:restaurant_id]
  @restaurant_package_id = params[:restaurant_package_id]
end

Instance Method Details

#cache_keyString

Generate cache key for caching the response

Returns:

  • (String)

    Cache key based on parameters and data freshness



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/comparing_package_service.rb', line 19

def cache_key
  [
    self.class.to_s,
    'v5', # version bump: sections sorted by priority
    CityHash.hash32(@params),
    'comparing_package_lists',
    I18n.locale,
    @package_type,
    @menu_group_id,
    @restaurant_id,
    freshness_token,
  ].join('|')
end

#callHash

Execute the package comparison logic

Returns:

  • (Hash)

    Response hash with success status and package data



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
# File 'app/services/comparing_package_service.rb', line 35

def call
  ElasticAPM.with_span('ComparingPackageService#call', 'app') do
    ElasticAPM.set_label(:package_type, @package_type)
    ElasticAPM.set_label(:menu_group_id, @menu_group_id)
    ElasticAPM.set_label(:restaurant_id, @restaurant_id)

    package_lists = fetch_package_lists
    return no_packages_response if package_lists.blank?

    packages_data = process_packages(package_lists)
    { success: true, data: packages_data }
  rescue StandardError => e
    HH_LOGGER.error('Error in ComparingPackageService#call', {
                      error: e.message,
                      package_type: @package_type,
                      menu_group_id: @menu_group_id,
                      restaurant_id: @restaurant_id,
                    })
    APMErrorHandler.report(e, context: {
                             package_type: @package_type,
                             menu_group_id: @menu_group_id,
                             restaurant_id: @restaurant_id,
                           })
    { success: false, message: 'Failed to fetch package comparison data' }
  end
end