app/strategies/meeting_reservation_updater.rb
# Process a single reservation tuple, identified by the keys meeting_id,
# event_id & badge_id.
# The event_id can be nil in case the processing involves only the header row
# of the reservation.
#
# The header and detail tuples will be:
# - updated, when the row exists;
# - created with the specified values, when not found;
# - deleted (just the detail rows), when is_not_coming == true.
#
# The method will act according to the presence of the first 2 parameters,
# the others are not required and can be nil.
# The method updates also the internal SQL diff log file accordingly.
#
# == Returns:
#
# The updated/created Meeting/Event/Relay/Reservation instance,
# or +true+ in case of deletion;
#
# Result is +false+ when the row was not found and the deletion was skipped,
# or +nil+ only in case of error during the update/create transaction.
#
Process a single reservation tuple, identified by the keys meeting_id,
event_id & badge_id.
The event_id can be nil in case the processing involves only the header row
of the reservation.

The header and detail tuples will be:
updated, when the row exists;
created with the specified values, when not found;
deleted (just the detail rows), when is_not_coming == true.

The method will act according to the presence of the first 2 parameters,
the others are not required and can be nil.
The method updates also the internal SQL diff log file accordingly.



The updated/created Meeting/Event/Relay/Reservation instance,
or +true+ in case of deletion.

Result is +false+ when the row was not found and the deletion was skipped,
or +nil+ only in case of error during the update/create transaction.
Process a single reservation tuple identified by the keys meeting_id event_id & badge_id . The event_id can be nil in case the processing involves only the header row of the reservation . The header and detail tuples will be : updated when the row exists ; created with the specified values when not found ; deleted ( just the detail rows ) when is_not_coming == true . The method will act according to the presence of the first 2 parameters the others are not required and can be nil . The method updates also the internal SQL diff log file accordingly . The updated / created Meeting / Event / Relay / Reservation instance or + true + in case of deletion . Result is + false + when the row was not found and the deletion was skipped or + nil + only in case of error during the update / create transaction .
def process!( meeting_id, badge_id, event_id, is_doing_this, is_not_coming = false, has_confirmed = false,
                timing_text_value = nil, notes = nil, relay_notes = nil )
    return nil unless meeting_id.present? && badge_id.present?
    badge = Badge.find_by_id( badge_id )
    event = event_id ? MeetingEvent.joins(:event_type).includes(:event_type).find_by_id( event_id ) : nil
    unless badge
      return nil
    end
    mrs = MeetingReservation.where( meeting_id: meeting_id, badge_id: badge_id ).first
    if mrs.nil?
      mrs = create_new_header_row!( meeting_id, badge, is_not_coming, has_confirmed, notes )
    else
      mrs = edit_existing_header_row!( mrs, is_not_coming, has_confirmed, notes )
    end
    if is_not_coming ||
       (event && !event.event_type.is_a_relay? && !timing_text_value.present?) ||
       (event && event.event_type.is_a_relay? && !relay_notes.present?)
      return delete_existing_rows!( meeting_id, badge_id, event_id )
    elsif event.nil?                                
      return mrs
    else                                            
      team_id    = badge.team_id
      swimmer_id = badge.swimmer_id
      klass      = event.event_type.is_a_relay? ? MeetingRelayReservation : MeetingEventReservation
      suggested_timing = TimingParser.parse( timing_text_value )
      mres = klass.where( badge_id: badge_id, meeting_event_id: event_id ).first
      ActiveRecord::Base.transaction do
        if mres                                   
          edit_existing_detail_row!( mres, event, is_doing_this, suggested_timing, relay_notes )
        else                                      
          create_new_detail_row!( meeting_id, badge, event, is_doing_this, suggested_timing, relay_notes )
        end
      end
    end
  rescue
    puts "RESCUE: '#{ $! }'"
    puts caller
    return nil
  end
def process! ( meeting_id , badge_id , event_id , is_doing_this , is_not_coming = false , has_confirmed = false , timing_text_value = nil , notes = nil , relay_notes = nil ) return nil unless meeting_id . present? && badge_id . present? badge = Badge . find_by_id ( badge_id ) event = event_id ? MeetingEvent . joins ( :event_type ) . includes ( :event_type ) . find_by_id ( event_id ) : nil unless badge return nil end mrs = MeetingReservation . where ( meeting_id : meeting_id , badge_id : badge_id ) . first if mrs . nil? mrs = create_new_header_row! ( meeting_id , badge , is_not_coming , has_confirmed , notes ) else mrs = edit_existing_header_row! ( mrs , is_not_coming , has_confirmed , notes ) end if is_not_coming || ( event && ! event . event_type . is_a_relay? && ! timing_text_value . present? ) || ( event && event . event_type . is_a_relay? && ! relay_notes . present? ) return delete_existing_rows! ( meeting_id , badge_id , event_id ) elsif event . nil? return mrs else team_id = badge . team_id swimmer_id = badge . swimmer_id klass = event . event_type . is_a_relay? ? MeetingRelayReservation : MeetingEventReservation suggested_timing = TimingParser . parse ( timing_text_value ) mres = klass . where ( badge_id : badge_id , meeting_event_id : event_id ) . first ActiveRecord :: Base . transaction do if mres edit_existing_detail_row! ( mres , event , is_doing_this , suggested_timing , relay_notes ) else create_new_detail_row! ( meeting_id , badge , event , is_doing_this , suggested_timing , relay_notes ) end end end rescue puts "RESCUE: '#{ $! }'" puts caller return nil end
Process a single reservation tuple, identified by the keys meeting_id,
event_id & badge_id.
Process a single reservation tuple identified by the keys meeting_id event_id & badge_id .
# DEBUG #    puts "\r\nprocess!( meeting_id: #{ meeting_id }: badge_id: #{ badge_id }, event_id: #{ event_id }, is_doing_this: #{ is_doing_this }, is_not_coming: #{ is_not_coming }, has_confirmed: #{ has_confirmed }, time: #{ timing_text_value }, notes: '#{ notes }', relay_notes: '#{ relay_notes }')" # DEBUG #      puts "\r\nBadge NOT found!" # DEBUG #    puts "\r\nRESERVATION HEADER: #{ mrs.inspect }" #                                               # --- Header CREATE or UPDATE --- # --- DELETE detail row(s) --- # --- No event specified (only header row was involved) --- # --- UPDATE or CREATE detail rows --- # DEBUG #      puts "timing: '#{ timing_text_value }' => #{ suggested_timing }; klass: #{ klass }, notes: '#{ notes }'" # --- UPDATE --- # DEBUG #            puts "before UPDATE (#{ klass })" # --- CREATE --- # DEBUG #            puts "before CREATE (#{ klass })" # DEBUG
meeting_id
<not_specific>
badge_id
<not_specific>
event_id
<not_specific>
is_doing_this
<not_specific>
is_not_coming
<not_specific>
has_confirmed
<not_specific>
timing_text_value
<not_specific>
notes
<not_specific>
relay_notes
<not_specific>