diff --git a/app/models/organisation.rb b/app/models/organisation.rb index b03ed00d7..503cee7ba 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -258,6 +258,21 @@ class Organisation < ApplicationRecord Organisation.visible.where(group:).order(:created_at).first end + # Shown on the edit organisation page to indicate which group member the user initially entered, if possible + def selected_group_member + return nil if oldest_group_member.nil? + + # find organisation by group_member_id if it exists + selected_group = Organisation.visible.find_by(id: group_member_id) + + # ensure that its still in the same group + if selected_group.present? && selected_group.group == group + selected_group + else + oldest_group_member + end + end + private def validate_profit_status diff --git a/app/views/organisations/edit.html.erb b/app/views/organisations/edit.html.erb index 94dd974da..321dbe8ef 100644 --- a/app/views/organisations/edit.html.erb +++ b/app/views/organisations/edit.html.erb @@ -42,7 +42,7 @@ :id, :name, label: { text: "Search for an organisation that is part of the same group as this organisation", size: "m" }, - options: { disabled: [""], selected: @organisation.oldest_group_member&.id || "" }, + options: { disabled: [""], selected: @organisation.selected_group_member&.id || "" }, "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> <%= f.govuk_radio_button :group_member, false, label: { text: "No" } %>