diff --git a/app/services/event_registration_services/public_registration.rb b/app/services/event_registration_services/public_registration.rb index ff7c3e3e82..f6c8dd63e8 100644 --- a/app/services/event_registration_services/public_registration.rb +++ b/app/services/event_registration_services/public_registration.rb @@ -51,6 +51,7 @@ def call organization = find_organization if field_value(ORGANIZATION_NAME_IDENTIFIER).present? create_affiliation(person, organization) if organization create_agency_address(organization) if organization && field_value("agency_city").present? + update_agency_type(organization) if organization && field_value("agency_type").present? assign_tags(person, organization) @@ -272,6 +273,21 @@ def create_agency_address(organization) ) end + # Mirror the registrant's "Organization Type" answer onto the linked + # organization. The "Other" choice is a specify option, so its free text + # folds into the answer as "Other: " — split the option label from the + # typed text, store the label on agency_type and the stripped text on + # agency_type_other (cleared for non-"Other" choices). Latest registration + # wins; AhoyTrackable records the before/after on the update. + def update_agency_type(organization) + raw = field_value("agency_type").to_s.strip + label, _separator, specified = raw.partition(":") + label = label.strip + other_text = FormField.other_option?(label) ? specified.strip.presence : nil + + organization.update!(agency_type: label, agency_type_other: other_text) + end + def assign_tags(person, organization) primary_sector_ids = collect_sector_ids(FormField::PRIMARY_SECTOR_FIELD_IDENTIFIERS) additional_sector_ids = collect_sector_ids(FormField::ADDITIONAL_SECTOR_FIELD_IDENTIFIERS) diff --git a/app/services/form_builder_service.rb b/app/services/form_builder_service.rb index daa33f5721..c52810eb19 100644 --- a/app/services/form_builder_service.rb +++ b/app/services/form_builder_service.rb @@ -407,8 +407,7 @@ def build_person_contact_info_fields(form, position) position = add_field(form, position, "Organization Type", :single_select_radio, key: "agency_type", group: "person_contact_info", required: false, options: [ - "501c3/nonprofit", "For-profit", "Government agency", - "Other (please specify below)" + "501c3/nonprofit", "For-profit", "Government agency", "Other" ]) position = add_field(form, position, "Organization Street Address", :free_form_input_one_line, key: "agency_street", group: "person_contact_info", required: false) diff --git a/app/views/organizations/_form.html.erb b/app/views/organizations/_form.html.erb index f28a3f07cd..19f4d9043f 100644 --- a/app/views/organizations/_form.html.erb +++ b/app/views/organizations/_form.html.erb @@ -113,7 +113,7 @@ "501c3/nonprofit", "For-profit", "Government agency", - "Other (please specify below)" + "Other" ], selected: f.object.agency_type, input_html: { diff --git a/spec/services/event_registration_services/public_registration_spec.rb b/spec/services/event_registration_services/public_registration_spec.rb index 7f9eb316a2..1801619e3b 100644 --- a/spec/services/event_registration_services/public_registration_spec.rb +++ b/spec/services/event_registration_services/public_registration_spec.rb @@ -51,6 +51,59 @@ def register_with(position:) end end + describe "organization type sync" do + let!(:organization) { create(:organization, name: "Helping Hands") } + + def register_with_agency_type(value) + params = base_form_params(first_name: "Sam", last_name: "Rowe", email: "sam@example.com").merge( + field_id(described_class::ORGANIZATION_NAME_IDENTIFIER) => "Helping Hands", + field_id("agency_type") => value + ) + described_class.call(event: event, form: form, form_params: params) + organization.reload + end + + it "folds an 'Other' answer into agency_type and the stripped free text into agency_type_other" do + register_with_agency_type("Other: Equine therapy") + + expect(organization.agency_type).to eq("Other") + expect(organization.agency_type_other).to eq("Equine therapy") + end + + it "stores the answer as 'Other: ' on the form submission, like other specify options" do + register_with_agency_type("Other: Equine therapy") + + answer = FormAnswer.joins(:form_field) + .find_by(form_fields: { field_identifier: "agency_type" }) + expect(answer.submitted_answer).to eq("Other: Equine therapy") + end + + it "stores a non-'Other' classification with no agency_type_other" do + register_with_agency_type("501c3/nonprofit") + + expect(organization.agency_type).to eq("501c3/nonprofit") + expect(organization.agency_type_other).to be_nil + end + + it "overwrites a previously stored type with the latest registrant's answer" do + organization.update!(agency_type: "501c3/nonprofit", agency_type_other: nil) + + register_with_agency_type("Other: Equine therapy") + + expect(organization.agency_type).to eq("Other") + expect(organization.agency_type_other).to eq("Equine therapy") + end + + it "clears a stale agency_type_other when the latest answer is no longer 'Other'" do + organization.update!(agency_type: "Other", agency_type_other: "Equine therapy") + + register_with_agency_type("Government agency") + + expect(organization.agency_type).to eq("Government agency") + expect(organization.agency_type_other).to be_nil + end + end + describe "matching an existing registrant by name" do it "matches a person stored under a nickname when the registrant types their legal first name" do existing = create(:person, first_name: "Bob", legal_first_name: "Robert", diff --git a/spec/services/form_builder_service_spec.rb b/spec/services/form_builder_service_spec.rb index b6717e268c..ea67bd2aa6 100644 --- a/spec/services/form_builder_service_spec.rb +++ b/spec/services/form_builder_service_spec.rb @@ -75,9 +75,14 @@ it "offers the agency type as the four organization classifications" do field = form.form_fields.find_by(field_identifier: "agency_type") expect(field.answer_options.pluck(:name)).to contain_exactly( - "501c3/nonprofit", "For-profit", "Government agency", "Other (please specify below)" + "501c3/nonprofit", "For-profit", "Government agency", "Other" ) end + + it "treats the agency type 'Other' as a specify option that reveals a free-text box" do + field = form.form_fields.find_by(field_identifier: "agency_type") + expect(field.specify_option_labels).to contain_exactly("Other") + end end context "scholarship section" do