Class: RestaurantService::UpdateInventory

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer
Defined in:
app/services/restaurant_service/update_inventory.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(inventory_template_param, restaurant) ⇒ UpdateInventory

Returns a new instance of UpdateInventory.

Parameters:

  • inventory_template_param (Hash)

    mon: [{start_Time, end_time, quantity_available, start_time, end_time, quantity_available],
    tue: [end_time, quantity_available, start_time, end_time, quantity_available]
    

    }



12
13
14
15
# File 'app/services/restaurant_service/update_inventory.rb', line 12

def initialize(inventory_template_param, restaurant)
  self.restaurant = restaurant
  self.inventory_template_param = inventory_template_param
end

Instance Method Details

#create_inventory_templateObject

create inventory template based on provided params



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/restaurant_service/update_inventory.rb', line 44

def create_inventory_template
  inventory_template_param.each do |name, inventory_templates|
    itg = InventoryTemplateGroup.new(name: name, restaurant: restaurant)
    unless itg.save
      merge_errors(itg.errors)
      raise ActiveRecord::Rollback
    end
    restaurant.update!("#{name}": itg.id)
    inventory_templates.each do |inventory_template|
      start_time = Time.zone.parse(inventory_template[:start_time])
      end_time = Time.zone.parse(inventory_template[:end_time])
      while start_time <= end_time
        it = InventoryTemplate.new(
          start_time: start_time.strftime('%H:%M'),
          end_time: (start_time += 15.minutes).strftime('%H:%M'),
          quantity_available: inventory_template[:quantity_available],
          inventory_template_group_id: itg.id,
        )
        HH_LOGGER.info('restaurant:update_inventory', it.attributes)

        if it.save
          send_updated_inventory_event(it.attributes, restaurant)
        else
          merge_errors(it.errors)
          raise ActiveRecord::Rollback
        end
      end
    end
  end
end

#execute!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/restaurant_service/update_inventory.rb', line 17

def execute!
  result = false
  ActiveRecord::Base.transaction do
    # create inventory template and generate schedule for dine_in and delivery
    create_inventory_template
    Restaurant.generate_schedule(restaurant.id, inventory_type: :dine_in)
    Restaurant.generate_schedule(restaurant.id, inventory_type: :delivery)
    result = true
  end
  if result
    # schedule a worker to notify GetYourGuide about the inventory update
    ::Vendors::Getyourguide::NotifyWorker.perform_async([restaurant.id])

    # return success message
    ServiceResult.new success: true,
                      message: 'System is recreating the inventories right now, please wait for a minute'
  else
    # return error message
    ServiceResult.new message: 'Sorry, an error happened', errors: [error_message_simple]
  end
rescue StandardError => e
  # catch error and return error message
  APMErrorHandler.report(e)
  ServiceResult.new message: 'Something went wrong', errors: [e.message]
end