Class: PartnerService::Inventories::EditService

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

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(restaurant) ⇒ EditService

Returns a new instance of EditService.



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

def initialize(restaurant)
  @restaurant = restaurant
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
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/partner_service/inventories/edit_service.rb', line 10

def call
  data = {}
  day_in_week = %w[mon tue wed thu fri sat sun]

  day_in_week.each do |day|
    array = []
    start_time = nil
    end_time = nil
    itg = InventoryTemplateGroup.find_by(id: @restaurant.send(day))
    next if itg.blank?

    its = itg.inventory_templates.order('start_time asc').group_by(&:quantity_available)
    its.transform_values do |items|
      items.each_with_index do |it, index|
        if end_time.blank?
          start_time = it.start_time.to_formatted_s(:time)
          end_time = it.end_time.to_formatted_s(:time)
        end

        if it.end_time == items[index + 1]&.start_time
          end_time = items[index + 1].end_time.to_formatted_s(:time)
        else
          end_time = '24:00' if end_time == '00:00'
          array << [start_time, end_time, it.quantity_available]
          end_time = nil
        end
      end
    end
    data.merge!({ convert_day_in_week(day) => array })
    end
  data
end