2020-03-25 17:45:53 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# SlotsReservation is the relation table between a Slot and a Reservation.
|
2022-07-04 16:55:02 +02:00
|
|
|
# It holds detailed data about a Reservation for the attached Slot.
|
2020-03-25 10:16:47 +01:00
|
|
|
class SlotsReservation < ApplicationRecord
|
2017-02-28 13:23:31 +01:00
|
|
|
belongs_to :slot
|
|
|
|
belongs_to :reservation
|
2022-12-28 17:51:27 +01:00
|
|
|
has_one :cart_item_reservation_slot, class_name: 'CartItem::ReservationSlot', dependent: :nullify
|
2017-02-28 13:23:31 +01:00
|
|
|
|
2023-01-12 17:48:52 +01:00
|
|
|
after_create :add_to_places_cache
|
2022-07-04 16:55:02 +02:00
|
|
|
after_update :set_ex_start_end_dates_attrs, if: :slot_changed?
|
|
|
|
after_update :notify_member_and_admin_slot_is_modified, if: :slot_changed?
|
2020-03-25 17:45:53 +01:00
|
|
|
|
2022-07-04 16:55:02 +02:00
|
|
|
after_update :notify_member_and_admin_slot_is_canceled, if: :canceled?
|
|
|
|
after_update :update_event_nb_free_places, if: :canceled?
|
2023-01-12 17:48:52 +01:00
|
|
|
after_update :remove_from_places_cache, if: :canceled?
|
|
|
|
|
|
|
|
before_destroy :remove_from_places_cache
|
2022-07-04 16:55:02 +02:00
|
|
|
|
|
|
|
def set_ex_start_end_dates_attrs
|
2022-12-28 17:51:27 +01:00
|
|
|
update_columns(ex_start_at: previous_slot.start_at, ex_end_at: previous_slot.end_at) # rubocop:disable Rails/SkipsModelValidations
|
2022-07-04 16:55:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def slot_changed?
|
|
|
|
saved_change_to_slot_id?
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_slot
|
|
|
|
Slot.find(slot_id_before_last_save)
|
|
|
|
end
|
|
|
|
|
|
|
|
def canceled?
|
|
|
|
saved_change_to_canceled_at?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_event_nb_free_places
|
|
|
|
return unless reservation.reservable_type == 'Event'
|
|
|
|
|
|
|
|
reservation.update_event_nb_free_places
|
|
|
|
end
|
|
|
|
|
2023-01-12 17:48:52 +01:00
|
|
|
def add_to_places_cache
|
|
|
|
update_places_cache(:+)
|
|
|
|
Slots::PlacesCacheService.add_users(slot, reservation.reservable_type, reservation.reservable_id, [reservation.statistic_profile.user_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_places_cache
|
|
|
|
update_places_cache(:-)
|
|
|
|
Slots::PlacesCacheService.remove_users(slot, reservation.reservable_type, reservation.reservable_id, [reservation.statistic_profile.user_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param operation [Symbol] :+ or :-
|
|
|
|
def update_places_cache(operation)
|
|
|
|
if reservation.reservable_type == 'Event'
|
|
|
|
Slots::PlacesCacheService.change_places(slot,
|
|
|
|
reservation.reservable_type,
|
|
|
|
reservation.reservable_id,
|
|
|
|
reservation.total_booked_seats,
|
|
|
|
operation)
|
|
|
|
else
|
|
|
|
Slots::PlacesCacheService.change_places(slot, reservation.reservable_type, reservation.reservable_id, 1, operation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-04 16:55:02 +02:00
|
|
|
def notify_member_and_admin_slot_is_modified
|
|
|
|
NotificationCenter.call type: 'notify_member_slot_is_modified',
|
|
|
|
receiver: reservation.user,
|
|
|
|
attached_object: self
|
|
|
|
NotificationCenter.call type: 'notify_admin_slot_is_modified',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_member_and_admin_slot_is_canceled
|
|
|
|
NotificationCenter.call type: 'notify_member_slot_is_canceled',
|
|
|
|
receiver: reservation.user,
|
|
|
|
attached_object: self
|
|
|
|
NotificationCenter.call type: 'notify_admin_slot_is_canceled',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: self
|
2017-02-28 13:23:31 +01:00
|
|
|
end
|
|
|
|
end
|