Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Keep a member participant's row when the form sends a new name

Guido Boykef21ab053e2cb
diff --git a/.github/workflows/guards.yml b/.github/workflows/guards.yml
index cff7597..deeb8c1 100644
--- a/.github/workflows/guards.yml
+++ b/.github/workflows/guards.yml
@@ -605,6 +605,7 @@ jobs:
spec/services/actors/move_meeting_spec.rb
spec/services/actors/save_meeting_spec.rb
spec/services/actors/claim_meeting_participants_spec.rb
+ spec/requests/organisations/meeting_participant_person_link_spec.rb
spec/requests/organisations/meetings_overview_columns_spec.rb
spec/requests/organisations/meetings_overview_lane_spec.rb
spec/requests/organisations/meetings_overview_lane_access_spec.rb
diff --git a/app/services/actors/save_meeting.rb b/app/services/actors/save_meeting.rb
index 0fbb9da..aac5d6c 100644
--- a/app/services/actors/save_meeting.rb
+++ b/app/services/actors/save_meeting.rb
@@ -146,24 +146,28 @@ module Actors
raise ActorError, @meeting.errors.full_messages if @meeting.errors.any?
end
- # Rows are matched by normalized_name and updated in place, never rebuilt.
- # That is what keeps person_id (the link to the people directory): the form
- # does not know it, and it never comes from the payload. A row survives every
- # save that keeps its name, in any spelling, with its link. A new name is a
- # new row without a link: the form has no rename, a chip removed and another
+ # Rows are matched and updated in place, never rebuilt. That is what keeps
+ # person_id (the link to the people directory): the form does not know it,
+ # and it never comes from the payload. A row survives every save that keeps
+ # the participant, with its link: the same normalized name in any spelling,
+ # or the same member under another name (the form labels a member chip with
+ # the member's current profile name, so a change of name in the profile
+ # comes back on the next save). A new name without such a member is a new
+ # row without a link: the form has no rename, a chip removed and another
# added is another participant, and the link dialog sets the link again.
def reconcile_participants(desired)
- keep = desired.map { |d| d[:normalized_name] }
- # Remove participants no longer present (empty desired -> clears them all).
- @meeting.meeting_participants.where.not(normalized_name: keep).destroy_all
+ # Load the rows once (no per-row SELECT / N+1) and upsert against them.
+ existing = @meeting.meeting_participants.to_a
+ rows = match_participant_rows(existing, desired)
+ # Remove participants no longer present (empty desired -> clears them all),
+ # through the association so the loaded rows drop them too.
+ @meeting.meeting_participants.destroy(*(existing - rows.compact))
- # Load the survivors once (no per-row SELECT / N+1) and upsert against them.
- existing = @meeting.meeting_participants.index_by(&:normalized_name)
# Share the already-loaded org instance so the belongs_to :organisation
# presence validation does not re-query it once per participant (N+1).
org = @meeting.organisation
- desired.each do |d|
- participant = existing[d[:normalized_name]] || @meeting.meeting_participants.build
+ desired.zip(rows).each do |d, participant|
+ participant ||= @meeting.meeting_participants.build
participant.organisation = org
participant.display_name = d[:display_name]
participant.user_id = d[:user_id]
@@ -171,6 +175,18 @@ module Actors
end
end
+ # The existing row for each desired participant, or nil for a new one: the
+ # row of its normalized name first, else the row of its member that no name
+ # claimed. Name first, so a member's old row goes rather than collide with a
+ # row that keeps the new name. Each user_id occurs once in desired
+ # (guard_against_duplicates!), so a row is never matched twice.
+ def match_participant_rows(existing, desired)
+ by_name = existing.index_by(&:normalized_name)
+ rows = desired.map { |d| by_name[d[:normalized_name]] }
+ by_user = (existing - rows.compact).select(&:user_id).index_by(&:user_id)
+ desired.zip(rows).map { |d, row| row || (d[:user_id] && by_user[d[:user_id]]) }
+ end
+
# PII fail-safe (Gespräche Slice 6, plan §5/§7): a meeting that is NOT indexed
# — opt-in off, or no content to index — must leave NO retrievable fragments.
# Runs synchronously inside the actor transaction (unlike Note, which keeps
diff --git a/spec/requests/organisations/meeting_participant_person_link_spec.rb b/spec/requests/organisations/meeting_participant_person_link_spec.rb
new file mode 100644
index 0000000..766dcaf
--- /dev/null
+++ b/spec/requests/organisations/meeting_participant_person_link_spec.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# A participant's person link (meeting_participants.person_id) survives a save of
+# the meeting form that leaves the participant alone. The form labels a member
+# chip with the member's CURRENT profile name, so after a change of name the
+# untouched chip comes back under another name. The actor spec covers the rule;
+# this one walks the real edit form, so a change to how the form names a member
+# cannot go round it.
+RSpec.describe 'Meeting edit keeps the person link of a member participant', type: :request do
+ let(:organisation) { create(:organisation) }
+ let(:project) { ActsAsTenant.with_tenant(organisation) { create(:project, organisation: organisation) } }
+ let(:writer) do
+ user = create(:user, first_name: 'Karl', last_name: 'Krause')
+ ActsAsTenant.with_tenant(organisation) do
+ role = create(:role, title: "Role #{SecureRandom.hex(4)}", organisation: organisation)
+ create(:membership, user: user, organisation: organisation, role: role, active: true)
+ create(:project_permission, user: user, project: project, meetings_access: 'read_write')
+ end
+ user
+ end
+
+ it 'keeps the row and person_id after the member changed their name in the profile' do
+ meeting, row, person = ActsAsTenant.with_tenant(organisation) do
+ m = create(:meeting, project: project, organisation: organisation, creator: writer, title: 'Retro')
+ p = create(:person, organisation: organisation, name: 'Karl Krause')
+ r = create(:meeting_participant, meeting: m, display_name: 'Karl Krause', user_id: writer.id)
+ r.update_column(:person_id, p.id)
+ [m, r, p]
+ end
+ writer.update!(last_name: 'Krause-Meier')
+ sign_in writer
+
+ get edit_organisation_project_meeting_path(organisation, project, meeting, format: :turbo_stream)
+ expect(response).to have_http_status(:ok)
+ form = Nokogiri::HTML(response.body.gsub(%r{</?template>}, ''))
+ option = form.at_css(%(select#meeting-participants-select option[value="user:#{writer.id}"]))
+ expect(option&.[]('selected')).to be_present
+
+ # What the chips field posts for that chip: the option text as display_name
+ # (meeting_participants_controller.js), behind the blank sentinel row.
+ patch organisation_project_meeting_path(organisation, project, meeting),
+ params: { meeting: { title: 'Retro', occurred_at: meeting.occurred_at,
+ participants: { '0' => { display_name: '' },
+ '1' => { display_name: option.text.strip, user_id: writer.id } } } }
+ expect(response).to have_http_status(:found)
+
+ rows = ActsAsTenant.with_tenant(organisation) do
+ MeetingParticipant.where(meeting_id: meeting.id).pluck(:id, :user_id, :person_id)
+ end
+ expect(rows).to eq([[row.id, writer.id, person.id]])
+ end
+end
diff --git a/spec/services/actors/save_meeting_spec.rb b/spec/services/actors/save_meeting_spec.rb
index 2a6ed87..fae0e91 100644
--- a/spec/services/actors/save_meeting_spec.rb
+++ b/spec/services/actors/save_meeting_spec.rb
@@ -311,6 +311,42 @@ RSpec.describe Actors::SaveMeeting do
expect(rows.first).to match([satisfy { |id| id != row.id }, 'karl kraus', nil])
expect(Person.exists?(person.id)).to be(true)
end
+
+ # The form labels a member chip with the member's CURRENT profile name, not
+ # the stored one. After a change of name in the profile, an untouched save
+ # sends the same member under a new name: still the same participant.
+ it 'keeps the row and person_id when a member arrives under a new name' do
+ member = org_member
+ meeting = described_class.call(
+ project: project, params: valid_params.merge(participants: [{ display_name: 'Karl Krause', user_id: member.id }]),
+ current_user: user
+ ).resource
+ row = linked_row(meeting, 'Karl Krause')
+
+ save(meeting, valid_params.merge(participants: [{ display_name: 'Karl Krause-Meier', user_id: member.id }]))
+
+ rows = meeting.reload.meeting_participants.pluck(:id, :display_name, :user_id, :person_id)
+ expect(rows).to eq([[row.id, 'Karl Krause-Meier', member.id, person.id]])
+ end
+
+ # A row whose name is sent keeps its name; matching by member comes second,
+ # so the member's old row goes instead of colliding with it.
+ it 'matches by name before it matches by member' do
+ member = org_member
+ meeting = described_class.call(
+ project: project,
+ params: valid_params.merge(participants: [{ display_name: 'Karl Krause', user_id: member.id },
+ { display_name: 'Karl Krause-Meier' }]),
+ current_user: user
+ ).resource
+ linked_row(meeting, 'Karl Krause')
+ named = meeting.meeting_participants.find_by!(normalized_name: 'karl krause-meier')
+
+ save(meeting, valid_params.merge(participants: [{ display_name: 'Karl Krause-Meier', user_id: member.id }]))
+
+ rows = meeting.reload.meeting_participants.pluck(:id, :user_id, :person_id)
+ expect(rows).to eq([[named.id, member.id, nil]])
+ end
end
end