Class: PartnerService::Inventories::SyncBookedSeatService

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

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(restaurant) ⇒ SyncBookedSeatService

Returns a new instance of SyncBookedSeatService.



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

def initialize(restaurant)
  @restaurant = restaurant
end

Instance Method Details

#regenerate_total_booked_seat(restaurant_id, date) ⇒ Object



19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/partner_service/inventories/sync_booked_seat_service.rb', line 19

def regenerate_total_booked_seat(restaurant_id, date)
  dine_in_reservations = []
  delivery_reservations = []
  Reservation.where('date >= ?', date).where(restaurant_id: restaurant_id).find_each do |reservation|
    inv_model = reservation.inventory_klass.to_s
    invs = inv_model.constantize.where(date: reservation.date, restaurant_id: reservation.restaurant_id).where(
      'start_time >= ? AND start_time <= ?  AND end_time >= ? AND end_time <= ?', reservation.start_time_format,
      reservation.end_time_format, reservation.start_time_format, reservation.end_time_format
    )

    invs.each do |inv|
      booked_seat = reservation.active? && reservation.ack? ? reservation.adult : 0
      ir_attributes = {
        reservation_id: reservation.id,
        booked_seat: booked_seat,
      }
      inventory_relation = if reservation.restaurant.use_third_party_inventory?
                             'inventory_id'
                           else
                             "#{reservation.inventory_klass.to_s.underscore}_id"
                           end
      # For inventory we using table partition so we need to use id[0] to get the id, because the id is an array [id, restaurant_id]
      if inv_model != 'InventoryTakeAway'
        ir_attributes[inventory_relation] = inv.id[0]
        ir_attributes[:restaurant_id] = reservation.restaurant_id
        ir_attributes[:inventory_type] = inv_model
        dine_in_reservations.push ir_attributes
      else
        ir_attributes[inventory_relation] = inv.id
        delivery_reservations.push ir_attributes
      end
    end
  end

  if dine_in_reservations.present?
    InventoryReservation.import! dine_in_reservations, on_duplicate_key_update: [:booked_seat], batch_size: 1000,
                                                       raise_error: true
    pending_dine_in = restaurant.inventories.where('date >= ?', date).map do |inv|
      inv.calc_total_booked_seat
      inv.attributes
    end
    Inventory.import! pending_dine_in, on_duplicate_key_update: [:total_booked_seat], batch_size: 1000,
                                       raise_error: true
  end

  if delivery_reservations.present?
    InventoryTakeAwayReservation.import! delivery_reservations, on_duplicate_key_update: [:booked_seat], raise_error: true,
                                                                batch_size: 1000

    pending_delivery = restaurant.inventory_take_aways.where('date >= ?', date).map do |inv|
      inv.calc_total_booked_seat
      inv.attributes
    end
    InventoryTakeAway.import! pending_delivery, on_duplicate_key_update: [:total_booked_seat], batch_size: 1000,
                                                raise_error: true
  end
  true
end

#sync_booked_seatObject



10
11
12
13
14
15
16
17
# File 'app/services/partner_service/inventories/sync_booked_seat_service.rb', line 10

def sync_booked_seat
  start_date = Date.current_date
  end_date = Date.current_date + restaurant.days_in_advance.days
  dates = (start_date..end_date).to_a
  dates.each do |date|
    regenerate_total_booked_seat(restaurant.id, date)
  end
end