# frozen_string_literal: true # Provides helper methods to create new Events and its recurring occurrences class Event::CreateEventService class << self def create_occurence(event, date) service = Availabilities::CreateAvailabilitiesService.new occurrence = Event.new( recurrence: 'none', title: title, description: event.description, event_image: occurrence_image(event), event_files: occurrence_files(event), availability: Availability.new(start_at: occurence_start_date(event, date), end_at: occurrence_end_date(event, date), available_type: 'event'), availability_id: nil, category_id: event.category_id, age_range_id: event.age_range_id, event_themes: event.event_themes, amount: event.amount, event_price_categories: occurrence_price_categories(event), nb_total_places: event.nb_total_places, recurrence_id: event.id ) occurrence.save service.create_slots(occurrence.availability) end private def occurence_start_date(event, date) start_at = DateTime.new(date.year, date.month, date.day, event.availability.start_at.hour, event.availability.start_at.min, event.availability.start_at.sec, event.availability.start_at.zone) dst_correction(event.availability.start_at, start_at) end def occurrence_end_date(event, date) days_diff = event.availability.end_at.day - event.availability.start_at.day end_date = date + days_diff.days end_at = DateTime.new(end_date.year, end_date.month, end_date.day, event.availability.end_at.hour, event.availability.end_at.min, event.availability.end_at.sec, event.availability.end_at.zone) dst_correction(event.availability.start_at, end_at) end def occurrence_image(event) EventImage.new(attachment: event.event_image.attachment) if event.event_image end def occurrence_files(event) event.event_files.map do |f| EventFile.new(attachment: f.attachment) end end def occurrence_price_categories(event) event.event_price_categories.map do |epc| EventPriceCategory.new(price_category_id: epc.price_category_id, amount: epc.amount) end end end end