2022-05-09 17:18:49 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for Trainings
|
|
|
|
class TrainingService
|
2023-01-24 14:03:01 +01:00
|
|
|
class << self
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def list(filters)
|
|
|
|
trainings = Training.includes(:training_image, :plans, :machines)
|
2022-05-09 17:18:49 +02:00
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
trainings = filter_by_disabled(trainings, filters)
|
|
|
|
trainings = filter_by_public_page(trainings, filters)
|
|
|
|
|
|
|
|
if filters[:requested_attributes].try(:include?, 'availabilities')
|
|
|
|
trainings = trainings.includes(availabilities: [slots: [reservation: [user: %i[profile trainings]]]])
|
|
|
|
.order('availabilities.start_at DESC')
|
|
|
|
end
|
|
|
|
|
|
|
|
trainings
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
2023-01-24 14:03:01 +01:00
|
|
|
|
|
|
|
# @param training [Training]
|
|
|
|
def auto_cancel_reservation(training)
|
|
|
|
return unless training.auto_cancel
|
|
|
|
|
|
|
|
training.availabilities
|
|
|
|
.includes(slots: :slots_reservations)
|
|
|
|
.where('availabilities.start_at >= ?', DateTime.current - training.auto_cancel_deadline.hours)
|
2023-01-24 17:09:56 +01:00
|
|
|
.find_each do |availability|
|
|
|
|
next if availability.reservations.count >= training.auto_cancel_threshold
|
2023-01-24 14:03:01 +01:00
|
|
|
|
2023-01-25 16:40:55 +01:00
|
|
|
auto_refund = Setting.get('wallet_module')
|
|
|
|
|
2023-01-24 17:09:56 +01:00
|
|
|
NotificationCenter.call type: 'notify_admin_training_auto_cancelled',
|
|
|
|
receiver: User.admins_and_managers,
|
2023-01-25 16:40:55 +01:00
|
|
|
attached_object: availability,
|
|
|
|
meta_data: { auto_refund: auto_refund }
|
2023-01-24 17:09:56 +01:00
|
|
|
|
|
|
|
availability.slots_reservations.find_each do |sr|
|
2023-01-25 16:40:55 +01:00
|
|
|
NotificationCenter.call type: 'notify_member_training_auto_cancelled',
|
|
|
|
receiver: sr.reservation.user,
|
|
|
|
attached_object: sr,
|
|
|
|
meta_data: { auto_refund: auto_refund }
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
sr.update(canceled_at: DateTime.current)
|
2023-01-25 16:40:55 +01:00
|
|
|
refund_after_cancel(sr.reservation) if auto_refund
|
2023-01-24 14:03:01 +01:00
|
|
|
end
|
|
|
|
end
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
2023-01-24 14:03:01 +01:00
|
|
|
|
2023-01-24 16:48:05 +01:00
|
|
|
# update the given training, depending on the provided settings
|
|
|
|
# @param training [Training]
|
|
|
|
# @param auto_cancel [Setting,NilClass]
|
|
|
|
# @param threshold [Setting,NilClass]
|
|
|
|
# @param deadline [Setting,NilClass]
|
|
|
|
def update_auto_cancel(training, auto_cancel, threshold, deadline)
|
|
|
|
previous_auto_cancel = auto_cancel.nil? ? Setting.find_by(name: 'trainings_auto_cancel').value : auto_cancel.previous_value
|
|
|
|
previous_threshold = threshold.nil? ? Setting.find_by(name: 'trainings_auto_cancel_threshold').value : threshold.previous_value
|
|
|
|
previous_deadline = deadline.nil? ? Setting.find_by(name: 'trainings_auto_cancel_deadline').value : deadline.previous_value
|
|
|
|
is_default = training.auto_cancel.to_s == previous_auto_cancel &&
|
|
|
|
[nil, previous_threshold].include?(training.auto_cancel_threshold.to_s) &&
|
|
|
|
[nil, previous_deadline].include?(training.auto_cancel_deadline.to_s)
|
|
|
|
|
|
|
|
return unless is_default
|
|
|
|
|
|
|
|
# update parameters if the given training is default
|
|
|
|
params = {}
|
|
|
|
params[:auto_cancel] = auto_cancel.value unless auto_cancel.nil?
|
|
|
|
params[:auto_cancel_threshold] = threshold.value unless threshold.nil?
|
|
|
|
params[:auto_cancel_deadline] = deadline.value unless deadline.nil?
|
|
|
|
training.update(params)
|
|
|
|
end
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
# @param trainings [ActiveRecord::Relation<Training>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def filter_by_disabled(trainings, filters)
|
|
|
|
return trainings if filters[:disabled].blank?
|
|
|
|
|
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
|
|
|
trainings.where(disabled: state)
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
# @param trainings [ActiveRecord::Relation<Training>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def filter_by_public_page(trainings, filters)
|
|
|
|
return trainings if filters[:public_page].blank?
|
|
|
|
|
|
|
|
state = filters[:public_page] == 'false' ? [nil, false] : true
|
|
|
|
trainings.where(public_page: state)
|
|
|
|
end
|
2023-01-25 16:40:55 +01:00
|
|
|
|
|
|
|
# @param reservation [Reservation]
|
|
|
|
def refund_after_cancel(reservation)
|
|
|
|
invoice_item = reservation.invoice_items.joins(:invoice).where(invoices: { type: nil }).first
|
|
|
|
service = WalletService.new(user: reservation.user, wallet: reservation.user.wallet)
|
|
|
|
transaction = service.credit(invoice_item.amount_after_coupon / 100.00)
|
|
|
|
service.create_avoir(transaction, DateTime.current, I18n.t('trainings.refund_for_auto_cancel')) if transaction
|
|
|
|
end
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
|
|
|
end
|