2022-07-12 17:46:01 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# helpers for managing slots reservations (reservations for a time unit)
|
|
|
|
class SlotsReservationsService
|
|
|
|
class << self
|
|
|
|
def cancel(slot_reservation)
|
2022-07-26 15:08:59 +02:00
|
|
|
# first we mark ths slot reservation as cancelled in DB, to free a ticket
|
2023-11-10 09:13:23 +01:00
|
|
|
slot_reservation.update!(canceled_at: Time.current)
|
2022-07-12 17:46:01 +02:00
|
|
|
|
|
|
|
# then we try to remove this reservation from ElasticSearch, to keep the statistics up-to-date
|
|
|
|
model_name = slot_reservation.reservation.reservable.class.name
|
|
|
|
client = Elasticsearch::Model.client
|
|
|
|
|
|
|
|
model = "Stats::#{model_name}".constantize
|
|
|
|
client.delete_by_query(
|
|
|
|
index: model.index_name,
|
|
|
|
type: model.document_type,
|
|
|
|
conflicts: 'proceed',
|
|
|
|
body: { query: { match: { reservationId: slot_reservation.reservation_id } } }
|
|
|
|
)
|
2022-07-26 15:08:59 +02:00
|
|
|
rescue Faraday::ConnectionFailed
|
|
|
|
warn 'Unable to update data in elasticsearch'
|
2022-07-12 17:46:01 +02:00
|
|
|
end
|
2023-06-26 16:02:54 +02:00
|
|
|
|
|
|
|
def validate(slot_reservation)
|
2023-07-11 10:36:44 +02:00
|
|
|
if slot_reservation.update(is_valid: true)
|
2023-06-27 19:04:18 +02:00
|
|
|
reservable = slot_reservation.reservation.reservable
|
|
|
|
if reservable.is_a?(Event)
|
|
|
|
reservable.update_nb_free_places
|
|
|
|
reservable.save
|
|
|
|
end
|
2023-07-12 15:07:14 +02:00
|
|
|
Slots::PlacesCacheService.refresh(slot_reservation.slot)
|
2023-06-26 16:02:54 +02:00
|
|
|
NotificationCenter.call type: 'notify_member_reservation_validated',
|
|
|
|
receiver: slot_reservation.reservation.user,
|
|
|
|
attached_object: slot_reservation.reservation
|
|
|
|
NotificationCenter.call type: 'notify_admin_reservation_validated',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: slot_reservation.reservation
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2023-07-11 10:36:44 +02:00
|
|
|
|
|
|
|
def invalidate(slot_reservation)
|
|
|
|
if slot_reservation.update(is_valid: false)
|
|
|
|
reservable = slot_reservation.reservation.reservable
|
|
|
|
if reservable.is_a?(Event)
|
|
|
|
reservable.update_nb_free_places
|
|
|
|
reservable.save
|
|
|
|
end
|
2023-07-12 15:07:14 +02:00
|
|
|
Slots::PlacesCacheService.refresh(slot_reservation.slot)
|
2023-07-11 14:57:38 +02:00
|
|
|
NotificationCenter.call type: 'notify_member_reservation_invalidated',
|
|
|
|
receiver: slot_reservation.reservation.user,
|
|
|
|
attached_object: slot_reservation.reservation
|
|
|
|
NotificationCenter.call type: 'notify_admin_reservation_invalidated',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: slot_reservation.reservation
|
2023-07-11 10:36:44 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2022-07-12 17:46:01 +02:00
|
|
|
end
|
|
|
|
end
|