2022-08-29 17:34:09 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Generate statistics indicators about reservations
|
|
|
|
class Statistics::Builders::ReservationsBuilderService
|
|
|
|
include Statistics::Concerns::HelpersConcern
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def build(options = default_options)
|
|
|
|
# machine/space/training list
|
2022-10-24 12:12:49 +02:00
|
|
|
%w[machine space training event].each do |category|
|
2023-02-02 15:43:05 +01:00
|
|
|
Statistics::FetcherService.send("each_#{category}_reservation", options) do |r|
|
2022-08-29 17:34:09 +02:00
|
|
|
%w[booking hour].each do |type|
|
2022-10-24 12:12:49 +02:00
|
|
|
stat = "Stats::#{category.capitalize}"
|
|
|
|
.constantize
|
|
|
|
.new({ date: format_date(r[:date]),
|
|
|
|
type: type,
|
|
|
|
subType: r["#{category}_type".to_sym],
|
|
|
|
ca: r[:ca],
|
|
|
|
name: r["#{category}_name".to_sym],
|
|
|
|
reservationId: r[:reservation_id] }.merge(user_info_stat(r)))
|
|
|
|
stat[:stat] = (type == 'booking' ? 1 : r[:nb_hours])
|
|
|
|
stat["#{category}Id".to_sym] = r["#{category}_id".to_sym]
|
|
|
|
|
2022-12-01 13:03:19 +01:00
|
|
|
stat = add_custom_attributes(category, stat, r)
|
2022-08-29 17:34:09 +02:00
|
|
|
stat.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
|
|
|
|
def add_custom_attributes(category, stat, reservation_data)
|
|
|
|
stat = add_event_attributes(category, stat, reservation_data)
|
2023-01-31 17:10:07 +01:00
|
|
|
stat = add_machine_attributes(category, stat, reservation_data)
|
|
|
|
stat = add_space_attributes(category, stat, reservation_data)
|
2022-12-01 13:03:19 +01:00
|
|
|
add_training_attributes(category, stat, reservation_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_event_attributes(category, stat, reservation_data)
|
|
|
|
return stat unless category == 'event'
|
|
|
|
|
|
|
|
stat[:eventDate] = reservation_data[:event_date]
|
|
|
|
stat[:eventTheme] = reservation_data[:event_theme]
|
|
|
|
stat[:ageRange] = reservation_data[:age_range]
|
|
|
|
|
|
|
|
stat
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_training_attributes(category, stat, reservation_data)
|
|
|
|
return stat unless category == 'training'
|
|
|
|
|
|
|
|
stat[:trainingDate] = reservation_data[:training_date]
|
|
|
|
|
|
|
|
stat
|
|
|
|
end
|
2023-01-31 17:10:07 +01:00
|
|
|
|
|
|
|
def add_machine_attributes(category, stat, reservation_data)
|
|
|
|
return stat unless category == 'machine'
|
|
|
|
|
2023-02-01 17:56:14 +01:00
|
|
|
stat[:machineDates] = reservation_data[:slot_dates].map { |date| { name: date } }
|
2023-01-31 17:10:07 +01:00
|
|
|
|
|
|
|
stat
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_space_attributes(category, stat, reservation_data)
|
|
|
|
return stat unless category == 'space'
|
|
|
|
|
2023-02-01 17:56:14 +01:00
|
|
|
stat[:spaceDates] = reservation_data[:slot_dates].map { |date| { name: date } }
|
2023-01-31 17:10:07 +01:00
|
|
|
|
|
|
|
stat
|
|
|
|
end
|
2022-08-29 17:34:09 +02:00
|
|
|
end
|
|
|
|
end
|