Class: PartnerService::Inventories::DetailUpdaterService

Inherits:
Object
  • Object
show all
Defined in:
app/services/partner_service/inventories/detail_updater_service.rb

Overview

Service class to update inventory details for one or more restaurants. Handles validation, processing inventory updates, and error handling.

Instance Method Summary collapse

Constructor Details

#initialize(current_staff, params) ⇒ DetailUpdaterService

Initializes the service with the current staff and parameters for inventory updates.

Parameters:

  • current_staff (Staff)

    The staff making the update request.

  • params (Hash)

    Validated parameters for the update operation.



10
11
12
13
14
# File 'app/services/partner_service/inventories/detail_updater_service.rb', line 10

def initialize(current_staff, params)
  @current_staff = current_staff
  @params = params
  @change_tracker = PartnerService::ChangeTracker.new(current_staff)
end

Instance Method Details

#callHash

Executes the inventory update process.

Returns:

  • (Hash)

    A summary of the update results, including success status and messages.

Raises:

  • (StandardError)

    If any validation or processing errors occur.



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
45
46
47
48
49
50
51
52
53
# File 'app/services/partner_service/inventories/detail_updater_service.rb', line 20

def call
  validate_time!
  restaurants = find_restaurants

  results = restaurants.map do |restaurant|
    next third_party_inventory_response(restaurant) if restaurant.use_third_party_inventory?

    @change_tracker.track_update_inventory(@params, restaurant.id)
    operation = InventoryUpdater.call(
      restaurant_id: restaurant.id,
      start_date: start_date,
      end_date: end_date,
      start_time: inventory_params[:start_time],
      end_time: inventory_params[:end_time],
      quantity_available: inventory_params[:quantity_available],
    )

    if operation.success?
      @change_tracker.notify_managers restaurant
    end

    build_response(restaurant, operation)
  end

  format_response(results)
rescue ActiveRecord::RecordNotFound => e
  { success: false, message: "Restaurant not found: #{e.message}" }
rescue ArgumentError => e
  { success: false, message: "Invalid parameter: #{e.message}" }
rescue NotAuthorized
  { success: false, message: 'Not authorized to update inventory' }
rescue StandardError => e
  { success: false, message: e.message }
end