1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/models/cart_item/reservation_slot.rb

57 lines
2.5 KiB
Ruby
Raw Normal View History

2022-12-28 17:51:27 +01:00
# frozen_string_literal: true
require_relative 'cart_item'
2022-12-28 17:51:27 +01:00
# A relation table between a pending reservation and a slot
class CartItem::ReservationSlot < ApplicationRecord
self.table_name = 'cart_item_reservation_slots'
2022-12-28 17:51:27 +01:00
belongs_to :cart_item, polymorphic: true
belongs_to :cart_item_machine_reservation, foreign_type: 'CartItem::MachineReservation', foreign_key: 'cart_item_id',
inverse_of: :cart_item_reservation_slots, class_name: 'CartItem::MachineReservation'
belongs_to :cart_item_space_reservation, foreign_type: 'CartItem::SpaceReservation', foreign_key: 'cart_item_id',
inverse_of: :cart_item_reservation_slots, class_name: 'CartItem::SpaceReservation'
belongs_to :cart_item_training_reservation, foreign_type: 'CartItem::TrainingReservation', foreign_key: 'cart_item_id',
inverse_of: :cart_item_reservation_slots, class_name: 'CartItem::TrainingReservation'
belongs_to :cart_item_event_reservation, foreign_type: 'CartItem::EventReservation', foreign_key: 'cart_item_id',
inverse_of: :cart_item_reservation_slots, class_name: 'CartItem::EventReservation'
2022-12-28 17:51:27 +01:00
belongs_to :slot
belongs_to :slots_reservation
2023-01-12 17:48:52 +01:00
after_create :add_to_places_cache
after_update :remove_from_places_cache, if: :canceled?
before_destroy :remove_from_places_cache
private
def add_to_places_cache
update_places_cache(:+)
end
def remove_from_places_cache
update_places_cache(:-)
end
# @param operation [Symbol] :+ or :-
def update_places_cache(operation)
user_method = operation == :+ ? :add_users : :remove_users
if cart_item_type == 'CartItem::EventReservation'
Slots::PlacesCacheService.change_places(slot, 'Event', cart_item.event_id, cart_item.total_tickets, operation)
Slots::PlacesCacheService.send(user_method,
slot,
'Event',
cart_item.event_id,
[cart_item.customer_profile.user_id])
else
Slots::PlacesCacheService.change_places(slot, cart_item.reservable_type, cart_item.reservable_id, 1, operation)
Slots::PlacesCacheService.send(user_method,
slot,
cart_item.reservable_type,
cart_item.reservable_id,
[cart_item.customer_profile.user_id])
end
end
2022-12-28 17:51:27 +01:00
end