Class: PartnerService::Inventories::CreateService

Inherits:
ApplicationService show all
Defined in:
app/services/partner_service/inventories/create_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) ⇒ CreateService

Returns a new instance of CreateService.



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

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

Instance Method Details

#create_inventory_templateObject



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/partner_service/inventories/create_service.rb', line 35

def create_inventory_template
  ActiveRecord::Base.transaction do
    new_inventory_templates = []
    previous_inventory_template_groups = restaurant.inventory_template_groups
    previous_inventory_template_groups.delete_all if previous_inventory_template_groups.present?
    inventory_template_param.each do |name, inventory_templates|
      itg = InventoryTemplateGroup.new(name: name, restaurant: restaurant)
      itg.save!
      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 = {
            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,
          }
          new_inventory_templates.push it
        end
      end
    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
30
31
32
33
# File 'app/services/partner_service/inventories/create_service.rb', line 11

def execute!
  result = false
  ActiveRecord::Base.transaction do
    create_inventory_template
    # Restaurant.generate_schedule(restaurant.id, inventory_type: :dine_in)
    # Restaurant.generate_schedule(restaurant.id, inventory_type: :delivery)

    HH_LOGGER.debug('create_inventory_service',
                    { restaurant_id: restaurant.id, service_type: :dine_in, remake: true, method: 'sync_perform',
                      source: 'create_service' })
    InventoryV2Worker.new.perform(restaurant.id, :dine_in, true)
    result = true
  end
  if result
    ServiceResult.new success: true,
                      message: 'System is creating the inventories right now, please wait for a minute'
  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