Class: ReportService::Dashboard

Inherits:
ApplicationService show all
Defined in:
app/services/report_service/dashboard.rb

Overview

typed: ignore frozen_string_literal: true

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(restaurants, params) ⇒ Dashboard

Returns a new instance of Dashboard.



7
8
9
10
11
# File 'app/services/report_service/dashboard.rb', line 7

def initialize(restaurants, params)
  @restaurants = restaurants
  @reservations = Reservation.joins(:restaurant).where(restaurant_id: restaurants.ids)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'app/services/report_service/dashboard.rb', line 5

def params
  @params
end

#reservationsObject (readonly)

Returns the value of attribute reservations.



5
6
7
# File 'app/services/report_service/dashboard.rb', line 5

def reservations
  @reservations
end

#restaurantsObject (readonly)

Returns the value of attribute restaurants.



5
6
7
# File 'app/services/report_service/dashboard.rb', line 5

def restaurants
  @restaurants
end

Instance Method Details

#build_reports_by_dayObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/report_service/dashboard.rb', line 49

def build_reports_by_day
  reservations.where('date between ? and ?', report_params[:start_date],
                     report_params[:end_date]).order('date ASC').map do |r|
    {
      date: r.date,
      reservation_time: r.reservation_time.iso8601,
      arrived: r.reached_goal? && r.is_past?,
      pending: r.pending? || r.upcoming?,
      rejected: r.rejected?,
      no_show: r.no_show?,
      cancel: !r.active,
      adult: r.adult,
      kids: r.kids,
    }
  end
end

#build_reports_by_hourObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/report_service/dashboard.rb', line 17

def build_reports_by_hour
  date = report_params[:start_date].is_a?(Date) ? report_params[:start_date] : report_params[:start_date].to_date
  @reservations = reservations.exclude_temporary.where(date: date, active: true,
                                                       no_show: false).group_by(&:start_time_format)
  hours = reservations.keys.sort

  hours.map do |hour|
    data = {
      count: reservations[hour].size,
      adult: reservations[hour].sum(&:adult),
      kids: reservations[hour].sum(&:kids),
      seat_available: calculate_seat_available(date, hour),
    }
    [
      hour,
      data,
    ]
  end
end

#calculate_seat_available(date, start_time) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/report_service/dashboard.rb', line 37

def calculate_seat_available(date, start_time)
  seat_left = 0
  restaurants.each do |restaurant|
    inv_checker = InvCheckerFactory.new(restaurant.id, restaurant.time_zone).create_inv_checker_service
    today = Time.now_in_tz(restaurant.time_zone).to_date
    return '-' if date < today

    seat_left += inv_checker.seat_left(date, start_time)
  end
  seat_left
end

#to_json(*_args) ⇒ Object



13
14
15
# File 'app/services/report_service/dashboard.rb', line 13

def to_json(*_args)
  report_params[:is_by_hour] == 'true' ? build_reports_by_hour : build_reports_by_day
end