Skip to content

Commit

Permalink
Adjust name querying for invalid partners to appear in calling function
Browse files Browse the repository at this point in the history
  • Loading branch information
torienyart committed Aug 22, 2023
1 parent 55b2f18 commit 4b65761
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions app/services/organization_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class << self
# @return [Boolean]
def update(organization, params)
return false unless valid?(organization, params)
# return organization unless valid?(organization, params)


if params.has_key?("partner_form_fields")
Expand Down Expand Up @@ -45,17 +44,16 @@ def update_partner_flags(organization)

def valid?(organization, params)
fields_marked_for_disabling = FIELDS.select { |field| params[field] == "false" }

# Here we do a check: if applying the params for disabling request types to all
# partners would mean any one partner would have all its request types disabled,
# then we should not apply the params and return an error message. As per:
# github.com/rubyforgood/human-essentials/issues/3264
invalid_partner_names = find_invalid_partners(organization, fields_marked_for_disabling)
invalid_partner_names = find_invalid_partners(organization, fields_marked_for_disabling).map(&:name)

if invalid_partner_names.empty?
return true
else
organization.errors.add(:base, "Please update #{invalid_partner_names.join(", ")}, so that they could make requests if you completed this change, then try again. Thank you.")
organization.errors.add(:base, "The following partners would be unable to make requests with this update: #{invalid_partner_names.join(", ")}")
return false
end
end
Expand All @@ -64,7 +62,7 @@ def find_invalid_partners(organization, fields_marked_for_disabling)
#finds any partners who's request types will all be disabled
organization.partners.select do |partner|
all_fields_will_be_disabled?(partner, fields_marked_for_disabling)
end.map(&:name)
end
end

def all_fields_will_be_disabled?(partner, fields_marked_for_disabling)
Expand Down
4 changes: 2 additions & 2 deletions spec/services/organization_update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

context "when object is invalid" do
it "should not update, return false" do
it "should not update and return false" do
params = {name: "A brand NEW NEW name",
url: "something that IS NOT A URL"}
described_class.update(organization, params)
Expand Down Expand Up @@ -63,7 +63,7 @@
end

it "should add an error message to the organization" do
expect(organization.errors.full_messages).to eq(["Please update #{partner_one.name}, so that they could make requests if you completed this change, then try again. Thank you."])
expect(organization.errors.full_messages).to eq(["The following partners would be unable to make requests with this update: #{partner_one.name}"])
end
end

Expand Down

0 comments on commit 4b65761

Please sign in to comment.