Class: BranchListService

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

Overview

Service class to handle branch listing logic, extracting complex logic from controller Provides pagination, filtering, and caching capabilities for branch data

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ BranchListService

Returns a new instance of BranchListService.



6
7
8
# File 'app/services/branch_list_service.rb', line 6

def initialize(params)
  @params = params
end

Instance Method Details

#cache_keyString

Generate cache key for caching the response

Returns:

  • (String)

    Cache key based on parameters and data freshness



12
13
14
15
16
17
18
19
20
# File 'app/services/branch_list_service.rb', line 12

def cache_key
  [
    self.class.to_s,
    CityHash.hash32(@params),
    'branches',
    I18n.locale,
    "updated_at:#{Branch.maximum(:updated_at).to_i}",
  ].join('|')
end

#callHash

Execute the branch listing logic

Returns:

  • (Hash)

    Response hash with success status, data, and pagination info



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/branch_list_service.rb', line 24

def call
  ElasticAPM.with_span('BranchListService#call', 'app') do
    branches = filter_active_branches
    branches = apply_master_menu_filter(branches)
    branches = apply_name_filter(branches) if @params[:name_like].present?

    paginated_result = paginate_branches(branches)
    branches_data = format_branches_data(paginated_result[:branches])

    build_response(branches_data, paginated_result[:pagination])
  rescue StandardError => e
    HH_LOGGER.error('Error in BranchListService#call', { error: e.message, params: @params })
    APMErrorHandler.report(e, context: { params: @params })
    { success: false, message: 'Failed to fetch branches' }
  end
end