From 5072ad0175d6c269f054bd85172a520dab6ef4e2 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Tue, 29 Oct 2019 11:33:47 -0500 Subject: [PATCH] Possible partial fix for `point_id` reconcilation. --- app/mutations/point_groups/update.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/mutations/point_groups/update.rb b/app/mutations/point_groups/update.rb index fcaad2d3d8..fc676a72b0 100644 --- a/app/mutations/point_groups/update.rb +++ b/app/mutations/point_groups/update.rb @@ -15,7 +15,7 @@ class Update < Mutations::Command end def validate - validate_point_ids if point_ids.any? + validate_point_ids if point_ids.present? validate_sort_type end @@ -38,19 +38,26 @@ def update_attributes end def maybe_reconcile_points + # Nil means "ignore" + # [] means "reset" + return if point_ids.nil? + # STEP 0: Setup - @old_point_ids = Set.new(point_group.point_group_items.pluck(:id)) + @old_point_ids = Set.new(point_group.point_group_items.pluck(:point_id)) @new_point_ids = Set.new(point_ids) @dont_delete = @new_point_ids & @old_point_ids @do_delete = (@old_point_ids - @dont_delete).to_a # STEP 1: "Garbage Collection" of PGIs that are no longer used. - PointGroupItem.where(id: @do_delete).map(&:destroy!) + PointGroupItem + .where(point_group_id: point_group.id) + .where(point_id: @do_delete) + .destroy_all # STEP 2: Create missing PGIs @do_create = (@new_point_ids - @dont_delete) - PointGroupItem.create!(@do_create.to_a.uniq.map do |id| - { point_id: id, point_group_id: point_group.id } + PointGroupItem.create!(@do_create.to_a.uniq.map do |point_id| + { point_id: point_id, point_group_id: point_group.id } end) end end