Skip to content

Commit

Permalink
Fix bug with duplicated users
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Jun 7, 2024
1 parent d492b60 commit 312ddcb
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 155 deletions.
67 changes: 29 additions & 38 deletions app/controllers/pairer/boards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def create_group
end

def lock_group
@group = @board.groups.find_by!(public_id: params.require(:group_id))
@group = @board.current_groups.find_by!(public_id: params.require(:group_id))

@group.toggle!(:locked)
broadcast_changes
Expand All @@ -167,7 +167,7 @@ def lock_group
end

def delete_group
@group = @board.groups.find_by!(public_id: params.require(:group_id))
@group = @board.current_groups.find_by!(public_id: params.require(:group_id))

@group.destroy!
broadcast_changes
Expand All @@ -180,7 +180,7 @@ def delete_group
end

def update_group
@group = @board.groups.find_by!(public_id: params.require(:group_id))
@group = @board.current_groups.find_by!(public_id: params.require(:group_id))

person_ids_was = @group.person_ids_array
roles_was = @group.roles_array
Expand All @@ -192,48 +192,39 @@ def update_group

@group.assign_attributes(attrs)

new_person_ids = @group.person_ids_array - person_ids_was
new_roles = @group.roles_array - roles_was

if @group.changed?
changed = true

ActiveRecord::Base.transaction do
if params[:removal] == "true"
@group.save!

if attrs[:person_ids].present? && @group.person_ids_array.empty?
@board.groups.detect do |g|
if g.person_ids_array.any?{|person_id| person_ids_was.include?(person_id) }
g.person_ids = g.person_ids_array - person_ids_was
g.save!
true
end
end
elsif attrs[:person_ids].present?
@board.groups.detect do |g|
next if g.id == @group.id

if g.person_ids_array.any?{|person_id| @group.person_ids_array.include?(person_id) }
g.person_ids = g.person_ids_array - @group.person_ids_array
g.save!
true
else
ActiveRecord::Base.transaction do
@group.save!

if new_person_ids.any?
@board.current_groups.detect do |g|
next if g.id == @group.id

if g.person_ids_array.intersection(new_person_ids).any?
g.person_ids = g.person_ids_array - @group.person_ids_array
g.save!
true
end
end
end
end

if attrs[:roles].present? && @group.roles_array.empty?
@board.groups.detect do |g|
if g.roles_array.any?{|person_id| roles_was.include?(person_id) }
g.roles = g.roles_array - roles_was
g.save!
true
end
end
elsif attrs[:roles].present?
@board.groups.detect do |g|
next if g.id == @group.id

if g.roles_array.any?{|role| @group.roles_array.include?(role) }
g.roles = g.roles_array - @group.roles_array
g.save!
true
if new_roles.any?
@board.current_groups.detect do |g|
next if g.id == @group.id

if g.roles_array.intersection(new_roles).any?
g.roles = g.roles_array - @group.roles_array
g.save!
true
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/pairer/boards/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ javascript:
$.ajax({
url: "#{update_group_board_path(@board, format: :js)}",
method: "POST",
data: {group_id: group_id, person_ids: person_ids},
data: {group_id: group_id, person_ids: person_ids, removal: "true"},
});
}
Expand Down
2 changes: 0 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
end

RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"

config.use_transactional_fixtures = true

config.infer_spec_type_from_file_location!
Expand Down
Loading

0 comments on commit 312ddcb

Please sign in to comment.