diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e55c1a31..3752670f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog Fab-manager - Fix a bug: unable to configure RAILS_LOCALE to fr-CA +- Fix a bug: unable to fix availabilities for events ## v5.7.0 2023 February 17 diff --git a/lib/tasks/fablab/fix_availabilities.rake b/lib/tasks/fablab/fix_availabilities.rake index f5ecaef60..4a05bdb8b 100644 --- a/lib/tasks/fablab/fix_availabilities.rake +++ b/lib/tasks/fablab/fix_availabilities.rake @@ -12,15 +12,17 @@ namespace :fablab do other_slots = Slot.where(availability_id: slot.availability_id) reservations = SlotsReservation.where(slot_id: other_slots.map(&:id)) + type = available_type(reservations) a = Availability.new( id: slot.availability_id, start_at: other_slots.group('id').select('min(start_at) as min').first[:min], end_at: other_slots.group('id').select('max(end_at) as max').first[:max], - available_type: available_type(reservations), + available_type: type, machine_ids: machines_ids(reservations, slot.availability_id), space_ids: space_ids(reservations, slot.availability_id), training_ids: training_ids(reservations, slot.availability_id) ) + create_mock_event(reservations, slot.availability_id) if type == 'event' && a.event.nil? raise StandardError, "unable to save availability for slot #{slot.id}: #{a.errors.full_messages}" unless a.save(validate: false) end end @@ -85,4 +87,32 @@ namespace :fablab do [] end + + # @param reservations [ActiveRecord::Relation] + # @param availability_id [Number] + def create_mock_event(reservations, availability_id) + model = find_similar_event(reservations) + invoice_item = reservations.first&.reservation&.invoice_items&.find_by(main: true) + Event.create!( + title: model&.title || invoice_item&.description, + description: model&.description || invoice_item&.description, + category: model&.category || Category.first, + availability_id: availability_id + ) + end + + # @param reservations [ActiveRecord::Relation] + # @return [Event,NilClass] + def find_similar_event(reservations) + reservations.each do |reservation| + reservation.reservation.invoice_items.each do |invoice_item| + words = invoice_item.description.split + (0..words.count).each do |w| + try_title = words[0..words.count - w].join(' ') + event = Event.find_by("title LIKE '#{try_title}%'") + return event unless event.nil? + end + end + end + end end