From 2d85d19ddc0ddc9d4374c92b85e124c852098859 Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 21 Aug 2022 10:58:55 -0400 Subject: [PATCH] plan-737 fixes --- .../reports/schedule_reports_controller.rb | 25 ++++++------------- app/services/change_service.rb | 25 ++++++++++++++----- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/controllers/reports/schedule_reports_controller.rb b/app/controllers/reports/schedule_reports_controller.rb index f3cc67451..4ae0e5b3e 100644 --- a/app/controllers/reports/schedule_reports_controller.rb +++ b/app/controllers/reports/schedule_reports_controller.rb @@ -21,12 +21,11 @@ def schedule_diff ChangeService.published_changes(from: from, to: to) else live = true - ChangeService.session_changes(from: from) + to ||= Time.now + ChangeService.session_changes(from: from, to: to) end fully_dropped = ChangeService.dropped_people(from: from, to: to) - to ||= Time.now - workbook = FastExcel.open(constant_memory: true) init_sheets(workbook: workbook) @@ -65,9 +64,8 @@ def check_sessions_changed(changes:, to:, live:) if (change[:changes]['room_id'] || change[:changes]['start_time']) #&& !change[:changes]['status'] next if ignore_session_status_change?(change: change) - next if live && !change[:changes]['status'] && ['draft', 'dropped'].include?(change[:object].status) + next if live && !change[:changes]['status'] && ['draft', 'dropped'].include?(change[:recent].status) - # Rails.logger.debug "******** SESSION ADD/REMOVE #{change[:changes]} " # If either room or time was added to the session if room_added?(change) || start_time_added?(change) session_added_row(@session_added, change, to) @@ -94,6 +92,7 @@ def check_sessions_changed(changes:, to:, live:) end # If the object is scheduked and title or description was changed + # We need the object as it is now??? if change[:object].start_time && change[:object].room_id check_status_change(change: change, to: to, live: live) @@ -140,7 +139,7 @@ def check_assignments_changed(changes:, state_change_sessions:, to:) roles = [moderator.id, participant.id] changes.each do |id, change| - changed_assignment = change[:object] + changed_assignment = change[:recent] changed_assignment ||= SessionAssignment.find(id) if SessionAssignment.exists?(id) next unless changed_assignment @@ -191,12 +190,7 @@ def check_status_change(change:, to:, live: false) return if ignore_session_status_change?(change: change) if session_status_change_to_drop?(change: change) - if live - return unless change[:object].published_session - - live_drop(session: change[:object], sheet: @participants_add_drop) - end - + live_drop(session: change[:object], sheet: @participants_add_drop) if live session_removed_row(@session_removed, change) return @@ -307,10 +301,9 @@ def session_room_change_row(sheet, change) end def session_removed_row(sheet, change) - # TODO: if it is a destroy .... sheet.append_row( [ - change[:object].title + change[:recent].title ] ) end @@ -321,7 +314,7 @@ def session_time_change_row(sheet, change) # Rails.logger.debug "********* #{change[:changes]['start_time'][1]} => #{new_time.class} #{new_time.strftime("%H")}" sheet.append_row( [ - change[:object].title, + change[:recent].title, orig_time ? FastExcel.date_num(orig_time, orig_time.in_time_zone.utc_offset) : nil, new_time ? FastExcel.date_num(new_time, new_time.in_time_zone.utc_offset) : nil ], @@ -353,8 +346,6 @@ def session_added_row(sheet, change, to) object.room&.name, assignments[:moderators].collect{|a| a.person.published_name}.join("; "), assignments[:participants].collect{|a| a.person.published_name}.join("; ") - # object.participant_assignments.where("session_assignment_role_type_id = ?", moderator).collect{|a| a.person.published_name}.join("; "), - # object.participant_assignments.where("session_assignment_role_type_id = ?", participant).collect{|a| a.person.published_name}.join("; ") ], [ nil, nil, nil, nil, @date_time_style, nil, nil, nil diff --git a/app/services/change_service.rb b/app/services/change_service.rb index ddf8bc78d..41c789ac4 100644 --- a/app/services/change_service.rb +++ b/app/services/change_service.rb @@ -38,9 +38,12 @@ def self.object_as_of(audit:, item_id:, item_type:, to:) object_version = audit.where("item_id = ? and item_type = ? and created_at <= ?", item_id, item_type, to) .order('created_at desc') .first - return nil unless object_version + object = if object_version + object_version.reify + else + item_type.constantize.find(session_id) if item_type.constantize.exists?(id) + end - object = object_version.reify return object end @@ -66,7 +69,7 @@ def self.assignments_for(session_id:, role_id:, to:) res = [] grouped_audits.each do |key, item_audits| - change = self.comnbined_changes(item_audits: item_audits, type: SessionAssignment) + change = self.combined_changes(item_audits: item_audits, type: SessionAssignment, to: to) res.concat [change[:object]] if self.assigned?(change: change, role_id: role_id) end @@ -119,7 +122,7 @@ def self.get_changes(clazz:, type:, from:, to:, publishable_session_ids: nil) grouped_audits.each do |key, item_audits| # Rails.logger.debug "**** AUDIT #{key} #{publishable_session_ids}" # just in case we sort by date - change = self.comnbined_changes(item_audits: item_audits, type: type) + change = self.combined_changes(item_audits: item_audits, type: type, to: to) if publishable_session_ids && change[:object].respond_to?(:session_id) next unless publishable_session_ids.include?(change[:object].session_id) end @@ -130,7 +133,7 @@ def self.get_changes(clazz:, type:, from:, to:, publishable_session_ids: nil) changes end - def self.comnbined_changes(item_audits:, type:) + def self.combined_changes(item_audits:, type:, to:) changes = nil item_audits.sort{|a,b| a.created_at <=> b.created_at}.each do |audit| @@ -138,13 +141,23 @@ def self.comnbined_changes(item_audits:, type:) if changes changes[:changes] = self.merge_change_set(to: changes[:changes], from: audit.object_changes) else + # Get the most recent version (as of the to datetime) + recent = self.object_as_of(audit: audit.class, item_id: audit.item_id, item_type: audit.item_type, to: to) + # Get the old version of the object obj = if audit.event == 'create' type.find(audit.item_id) if type.exists?(audit.item_id) else audit.reify end - changes = {item_id: audit.item_id, item_type: audit.item_type, event: audit.event, object: obj, changes: audit.object_changes} + changes = { + item_id: audit.item_id, + item_type: audit.item_type, + event: audit.event, + object: obj, + recent: recent, + changes: audit.object_changes + } end end