From 893ea9ec5095c59b776cde811ee0d6a0fbf53946 Mon Sep 17 00:00:00 2001
From: Felix Eckhofer <felix@eckhofer.com>
Date: Sat, 4 Jan 2025 17:15:56 +0100
Subject: [PATCH] Fix logic for merging assignments from conference variants

---
 app/models/conference.rb | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/app/models/conference.rb b/app/models/conference.rb
index 4e54003..262439f 100644
--- a/app/models/conference.rb
+++ b/app/models/conference.rb
@@ -71,6 +71,7 @@ class Conference < ApplicationRecord
   def compare_engelsystem_shifts(additional_conferences = [])
     return unless translation_angel_id = fetch_translation_angel_id
     return unless data = fetch_engelsystem("angeltypes/#{translation_angel_id}/shifts")
+
     engelsystem_shifts = data.each_with_object({}) do |shift, hash|
       hash[shift['id']] = shift
         &.dig("needed_angel_types")
@@ -78,22 +79,29 @@ class Conference < ApplicationRecord
         &.dig("entries")
         &.map{ |t| t["user"]["name"] }
     end
-    Session.where(conference: [self]+additional_conferences).includes(:assignments).each do |session|
-      next if session.engelsystem_id.blank?
-      engelsystem_assigned = engelsystem_shifts[session.engelsystem_id]
-      local_assigned = session.assignments.includes(:user).map{|a|a.user.name}
+
+    Session
+        .where(conference: [self, *additional_conferences])
+        .where.not(engelsystem_id: nil)
+        .includes(assignments: :user)
+        .group_by(&:engelsystem_id)
+        .each do |engelsystem_id, sessions|
+      engelsystem_assigned = engelsystem_shifts[engelsystem_id]
+      local_assigned = sessions.flat_map(&:assignments).map{|a|a.user.name}
 
       only_engelsystem = engelsystem_assigned - local_assigned
       only_local = local_assigned - engelsystem_assigned
 
       unless only_engelsystem.blank? and only_local.blank?
         puts "============================="
-        puts "Session: #{session.title} (#{session.engelsystem_id})"
+        puts "Session: #{sessions[0].title} (#{engelsystem_id})"
         puts "============================="
         puts "Not signed up in engelsystem: #{only_local.join(", ")}" unless only_local.blank?
         puts "Missing in local assignments: #{only_engelsystem.join(", ")}" unless only_engelsystem.blank?
         puts
       end
     end
+
+    return true
   end
 end
-- 
GitLab