Class: PartnerService::Inventories::UpdateService

Inherits:
ApplicationService show all
Defined in:
app/services/partner_service/inventories/update_service.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(inventory_template_param, restaurant) ⇒ UpdateService

Returns a new instance of UpdateService.



6
7
8
9
# File 'app/services/partner_service/inventories/update_service.rb', line 6

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

Instance Method Details

#create_inventory_templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/partner_service/inventories/update_service.rb', line 31

def create_inventory_template
  ActiveRecord::Base.transaction do
    new_inventory_templates = []
    inventory_template_param.each do |name, inventory_templates|
      itg = InventoryTemplateGroup.find_by(id: @restaurant.send(name))
      next unless itg

      itg.inventory_templates.delete_all if inventory_templates.blank?
      previous_inventory_templates = itg.inventory_templates
      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 = {
            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('partner app update inventory template', it)
          new_inventory_templates.push it
        end
      end
      previous_inventory_templates.delete_all
    end
    InventoryTemplate.import! new_inventory_templates, batch_size: 2000, raise_error: true
  end
end

#execute!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/partner_service/inventories/update_service.rb', line 11

def execute!
  result = false
  ActiveRecord::Base.transaction do
    create_inventory_template
    HH_LOGGER.debug('update_inventory_service',
                    { restaurant_id: restaurant.id, service_type: :dine_in, remake: true, delay: '1s',
                      source: 'update_service' })
    InventoryV2Worker.perform_in(1.second, restaurant.id, :dine_in, true)
    result = true
  end
  if result
    ServiceResult.new success: true, message: 'System is recreating the inventories right now, please wait'
  else
    ServiceResult.new message: 'Sorry, an error happened', errors: [error_message_simple]
  end
rescue StandardError => e
  APMErrorHandler.report(e)
  ServiceResult.new message: 'Something went wrong', errors: [e.message]
end