2019-01-17 09:53:28 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-01 15:43:16 +01:00
|
|
|
require './app/helpers/application_helper'
|
|
|
|
|
2019-01-17 09:53:28 +01:00
|
|
|
# Provides helper methods for Events resources and properties
|
|
|
|
class EventService
|
2021-02-02 15:52:55 +01:00
|
|
|
class << self
|
2022-12-01 15:43:16 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
|
2021-02-02 15:52:55 +01:00
|
|
|
def process_params(params)
|
|
|
|
# handle dates & times (whole-day events or not, maybe during many days)
|
|
|
|
range = EventService.date_range({ date: params[:start_date], time: params[:start_time] },
|
|
|
|
{ date: params[:end_date], time: params[:end_time] },
|
|
|
|
params[:all_day] == 'true')
|
|
|
|
params.merge!(availability_attributes: { id: params[:availability_id],
|
|
|
|
start_at: range[:start_at],
|
|
|
|
end_at: range[:end_at],
|
|
|
|
available_type: 'event' })
|
|
|
|
.extract!(:start_date, :end_date, :start_time, :end_time, :all_day)
|
|
|
|
# convert main price to centimes
|
2022-12-01 15:43:16 +01:00
|
|
|
params[:amount] = to_centimes(params[:amount]) if params[:amount].present?
|
2021-02-02 15:52:55 +01:00
|
|
|
# delete non-complete "other" prices and convert them to centimes
|
|
|
|
unless params[:event_price_categories_attributes].nil?
|
|
|
|
params[:event_price_categories_attributes].delete_if do |price_cat|
|
|
|
|
price_cat[:price_category_id].empty? || price_cat[:amount].empty?
|
|
|
|
end
|
|
|
|
params[:event_price_categories_attributes].each do |price_cat|
|
2022-12-01 15:43:16 +01:00
|
|
|
price_cat[:amount] = to_centimes(price_cat[:amount])
|
2021-02-02 15:52:55 +01:00
|
|
|
end
|
2019-01-17 09:53:28 +01:00
|
|
|
end
|
2021-02-02 15:52:55 +01:00
|
|
|
# return the resulting params object
|
|
|
|
params
|
2019-01-17 09:53:28 +01:00
|
|
|
end
|
|
|
|
|
2021-02-02 15:52:55 +01:00
|
|
|
def date_range(starting, ending, all_day)
|
2023-04-20 18:57:37 +02:00
|
|
|
start_date = Date.parse(starting[:date])
|
|
|
|
end_date = Date.parse(ending[:date])
|
2022-10-24 15:56:04 +02:00
|
|
|
start_time = starting[:time] ? Time.zone.parse(starting[:time]) : nil
|
|
|
|
end_time = ending[:time] ? Time.zone.parse(ending[:time]) : nil
|
|
|
|
if all_day || start_time.nil? || end_time.nil?
|
2023-02-13 11:39:36 +01:00
|
|
|
start_at = Time.zone.local(start_date.year, start_date.month, start_date.day, 0, 0, 0)
|
|
|
|
end_at = Time.zone.local(end_date.year, end_date.month, end_date.day, 23, 59, 59)
|
2021-02-02 15:52:55 +01:00
|
|
|
else
|
2023-02-13 11:39:36 +01:00
|
|
|
start_at = Time.zone.local(start_date.year, start_date.month, start_date.day, start_time.hour, start_time.min, start_time.sec)
|
|
|
|
end_at = Time.zone.local(end_date.year, end_date.month, end_date.day, end_time.hour, end_time.min, end_time.sec)
|
2021-02-02 15:52:55 +01:00
|
|
|
end
|
|
|
|
{ start_at: start_at, end_at: end_at }
|
2019-01-17 09:53:28 +01:00
|
|
|
end
|
2020-01-07 17:18:49 +01:00
|
|
|
|
2021-02-02 15:52:55 +01:00
|
|
|
# delete one or more events (if periodic)
|
|
|
|
def delete(event_id, mode = 'single')
|
|
|
|
results = []
|
|
|
|
event = Event.find(event_id)
|
|
|
|
events = case mode
|
|
|
|
when 'single'
|
|
|
|
[event]
|
|
|
|
when 'next'
|
|
|
|
Event.includes(:availability)
|
|
|
|
.where(
|
|
|
|
'availabilities.start_at >= ? AND events.recurrence_id = ?',
|
|
|
|
event.availability.start_at,
|
|
|
|
event.recurrence_id
|
|
|
|
)
|
|
|
|
.references(:availabilities, :events)
|
|
|
|
when 'all'
|
2022-10-24 15:56:04 +02:00
|
|
|
Event.where(recurrence_id: event.recurrence_id)
|
2021-02-02 15:52:55 +01:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
events.each do |e|
|
2023-01-19 17:41:21 +01:00
|
|
|
method = e.destroyable? ? :destroy : :soft_destroy!
|
|
|
|
# we use double negation because destroy can return either a boolean (false) or an Event (in case of delete success)
|
|
|
|
results.push status: !!e.send(method), event: e # rubocop:disable Style/DoubleNegation
|
2021-02-02 15:52:55 +01:00
|
|
|
end
|
|
|
|
results
|
|
|
|
end
|
2020-01-27 18:21:46 +01:00
|
|
|
end
|
2019-01-17 09:53:28 +01:00
|
|
|
end
|