Skip to content

Commit

Permalink
Possible partial fix for point_id reconcilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Oct 29, 2019
1 parent c418e4b commit 5072ad0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions app/mutations/point_groups/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 5072ad0

Please sign in to comment.