1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/services/availabilities/availabilities_service.rb

144 lines
5.7 KiB
Ruby
Raw Normal View History

2019-01-17 16:26:03 +01:00
# frozen_string_literal: true
# Provides helper methods for Availability resources and properties
class Availabilities::AvailabilitiesService
def initialize(current_user, maximum_visibility = {})
@current_user = current_user
@maximum_visibility = maximum_visibility
@service = Availabilities::StatusService.new(current_user.role)
2019-01-17 16:26:03 +01:00
end
# list all slots for the given machine, with reservations info, relatives to the given user
def machines(machine_id, user)
machine = Machine.friendly.find(machine_id)
reservations = reservations(machine, user)
2019-01-17 16:26:03 +01:00
availabilities = availabilities(machine, 'machines', user)
slots = []
availabilities.each do |a|
2020-06-10 11:52:54 +02:00
slot_duration = a.slot_duration || Setting.get('slot_duration').to_i
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin?
2019-01-17 16:26:03 +01:00
slot = Slot.new(
start_at: a.start_at + (i * slot_duration).minutes,
end_at: a.start_at + (i * slot_duration).minutes + slot_duration.minutes,
2019-01-17 16:26:03 +01:00
availability_id: a.id,
availability: a,
machine: machine,
title: ''
)
slot = @service.machine_reserved_status(slot, reservations, @current_user)
slots << slot
end
end
slots
end
# list all slots for the given space, with reservations info, relatives to the given user
def spaces(space_id, user)
space = Space.friendly.find(space_id)
reservations = reservations(space, user)
2019-01-17 16:55:25 +01:00
availabilities = availabilities(space, 'space', user)
2019-01-17 16:26:03 +01:00
slots = []
availabilities.each do |a|
2020-06-10 11:52:54 +02:00
slot_duration = a.slot_duration || Setting.get('slot_duration').to_i
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin?
2019-01-17 16:26:03 +01:00
slot = Slot.new(
start_at: a.start_at + (i * slot_duration).minutes,
end_at: a.start_at + (i * slot_duration).minutes + slot_duration.minutes,
2019-01-17 16:26:03 +01:00
availability_id: a.id,
availability: a,
space: space,
title: ''
)
slot = @service.space_reserved_status(slot, reservations, user)
slots << slot
end
end
slots.each do |s|
s.title = I18n.t('availabilities.not_available') if s.complete? && !s.is_reserved
2019-01-17 16:26:03 +01:00
end
slots
end
# list all slots for the given training, with reservations info, relatives to the given user
def trainings(training_id, user)
# first, we get the already-made reservations
reservations = user.reservations.where("reservable_type = 'Training'")
reservations = reservations.where('reservable_id = :id', id: training_id.to_i) if training_id.is_number?
reservations = reservations.joins(:slots).where('slots.start_at > ?', @current_user.admin? ? 1.month.ago : DateTime.current)
2019-01-17 16:26:03 +01:00
# visible availabilities depends on multiple parameters
2019-01-17 16:55:25 +01:00
availabilities = training_availabilities(training_id, user)
2019-01-17 16:26:03 +01:00
# finally, we merge the availabilities with the reservations
availabilities.each do |a|
2019-01-17 16:55:25 +01:00
a = @service.training_event_reserved_status(a, reservations, user)
2019-01-17 16:26:03 +01:00
end
end
private
def subscription_year?(user)
user.subscription && user.subscription.plan.interval == 'year' && user.subscription.expired_at >= DateTime.current
2019-01-17 16:26:03 +01:00
end
# member must have validated at least 1 training and must have a valid yearly subscription.
def show_extended_slots?(user)
user.trainings.size.positive? && subscription_year?(user)
end
def reservations(reservable, user)
2019-01-17 16:26:03 +01:00
Reservation.where('reservable_type = ? and reservable_id = ?', reservable.class.name, reservable.id)
.includes(:slots, statistic_profile: [user: [:profile]])
2019-01-17 16:26:03 +01:00
.references(:slots, :user)
.where('slots.start_at > ?', user.admin? ? 1.month.ago : DateTime.current)
2019-01-17 16:26:03 +01:00
end
def availabilities(reservable, type, user)
2020-05-06 12:56:12 +02:00
if user.admin? || user.manager?
2019-01-17 16:26:03 +01:00
reservable.availabilities
.includes(:tags)
.where('end_at > ? AND available_type = ?', 1.month.ago, type)
2019-01-17 16:26:03 +01:00
.where(lock: false)
else
end_at = @maximum_visibility[:other]
end_at = @maximum_visibility[:year] if subscription_year?(user)
reservable.availabilities
.includes(:tags)
.where('end_at > ? AND end_at < ? AND available_type = ?', DateTime.current, end_at, type)
2019-01-17 16:26:03 +01:00
.where('availability_tags.tag_id' => user.tag_ids.concat([nil]))
.where(lock: false)
end
end
def training_availabilities(training_id, user)
availabilities = if training_id.is_number? || (training_id.length.positive? && training_id != 'all')
2019-01-17 16:55:25 +01:00
Training.friendly.find(training_id).availabilities
2019-01-17 16:26:03 +01:00
else
Availability.trainings
end
# who made the request?
2020-01-21 11:16:49 +01:00
# 1) an admin (he can see all availabilities of 1 month ago and future)
2019-01-17 16:26:03 +01:00
if @current_user.admin?
availabilities.includes(:tags, :slots, trainings: [:machines])
.where('availabilities.start_at > ?', 1.month.ago)
2019-01-17 16:26:03 +01:00
.where(lock: false)
# 2) an user (he cannot see availabilities further than 1 (or 3) months)
else
end_at = @maximum_visibility[:other]
end_at = @maximum_visibility[:year] if show_extended_slots?(user)
availabilities.includes(:tags, :slots, :availability_tags, trainings: [:machines])
.where('availabilities.start_at > ? AND availabilities.start_at < ?', DateTime.current, end_at)
2019-01-17 16:26:03 +01:00
.where('availability_tags.tag_id' => user.tag_ids.concat([nil]))
.where(lock: false)
end
end
end