Skip to content

Commit e92a3af

Browse files
author
Olexii Kasianenko
committed
Implement mandatory fields validation in request approval process
1 parent 173d7be commit e92a3af

File tree

11 files changed

+82
-300
lines changed

11 files changed

+82
-300
lines changed

app/models/partner.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class Partner < ApplicationRecord
4848

4949
has_many_attached :documents
5050

51-
# validates :name, presence: true, uniqueness: { scope: :organization }
52-
5351
validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP }
5452

5553
validates :quota, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true

app/models/partners/profile.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -191,40 +191,6 @@ def self.agency_types_for_selection
191191
agency_types.keys.map(&:to_sym).sort_by { |sym| I18n.t(sym, scope: :partners_profile) }.partition { |v| v != :other }.flatten
192192
end
193193

194-
def check_social_media
195-
return if organization.one_step_partner_invite
196-
return if website.present? || twitter.present? || facebook.present? || instagram.present?
197-
return if partner.partials_to_show.exclude?("media_information")
198-
199-
unless no_social_media_presence
200-
errors.add(:no_social_media_presence, "No social media presence must be checked if you have not provided any of Website, Twitter, Facebook, or Instagram.")
201-
end
202-
errors
203-
end
204-
205-
def check_mandatory_fields
206-
return if organization.one_step_partner_invite
207-
208-
mandatory_fields = [
209-
:agency_type,
210-
:address1,
211-
:city,
212-
:state,
213-
:zip_code,
214-
:program_name,
215-
:program_description
216-
]
217-
messages = []
218-
messages << "Name can't be blank" if partner.name.blank?
219-
mandatory_fields.each do |field|
220-
if send(field).blank?
221-
messages << "#{field.to_s.humanize.capitalize} can't be blank"
222-
end
223-
end
224-
errors.add(:base, messages.join(", ")) if messages.any?
225-
errors
226-
end
227-
228194
private
229195

230196
def client_share_is_0_or_100

app/services/partner_profile_update_service.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ def initialize(old_partner, new_partner_params, new_profile_params)
77
@profile = @partner.profile
88
@partner_params = new_partner_params
99
@profile_params = new_profile_params
10+
@error_messages = []
1011
end
1112

1213
def call
1314
return self unless validation
1415

1516
perform_profile_service do
16-
@partner.update(@partner_params)
17+
@partner.update!(@partner_params)
1718
@profile.served_areas.destroy_all
1819
@profile.attributes = @profile_params
1920
@profile.save!(context: :edit)
@@ -52,6 +53,7 @@ def validation
5253

5354
check_social_media
5455
check_mandatory_fields
56+
@error = @error_messages.join(". ") if @error_messages.any?
5557
@error.nil?
5658
end
5759

@@ -60,14 +62,14 @@ def check_mandatory_fields
6062
missing_fields = mandatory_fields.select { |field| @profile_params[field].blank? }
6163
missing_fields.prepend :agency_name if @partner_params[:name].blank?
6264
if missing_fields.any?
63-
@error = "Missing mandatory fields: #{missing_fields.join(', ')}"
65+
@error_messages << "Missing mandatory fields: #{missing_fields.join(", ")}"
6466
end
6567
end
6668

6769
def check_social_media
6870
social_media_fields = %i[website facebook twitter instagram]
69-
if social_media_fields.all? { |field| @profile_params[field].blank? } && @profile_params[:no_social_media_presence] == '0'
70-
@error = "At least one social media field must be filled out or 'No social media presence' must be checked."
71+
if social_media_fields.all? { |field| @profile_params[field].blank? } && !@profile_params[:no_social_media_presence]
72+
@error_messages << "At least one social media field must be filled out or 'No social media presence' must be checked."
7173
end
7274
end
7375
end

app/services/partner_update_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def validation
2525
mandatory_fields = %i[name]
2626
missing_fields = mandatory_fields.select { |field| params[field].blank? }
2727
if missing_fields.any?
28-
@error = "Missing mandatory fields: #{missing_fields.join(', ')}"
28+
@error = "Missing mandatory fields: #{missing_fields.join(", ")}"
2929
return false
3030
end
3131
true

app/services/partners/request_approval_service.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,44 @@ def valid?
3333
errors.add :base, partner.profile.errors.full_messages.join('. ')
3434
end
3535

36-
errors.merge! partner.profile.errors if partner.profile.check_social_media
37-
errors.merge! partner.profile.errors if partner.profile.check_mandatory_fields
36+
check_social_media
37+
check_mandatory_fields
3838

3939
errors.none?
4040
end
41+
42+
def check_mandatory_fields
43+
return if partner.organization.one_step_partner_invite
44+
45+
mandatory_fields = [
46+
:agency_type,
47+
:address1,
48+
:city,
49+
:state,
50+
:zip_code,
51+
:program_name,
52+
:program_description
53+
]
54+
messages = []
55+
messages << "Name can't be blank" if partner.name.blank?
56+
mandatory_fields.each do |field|
57+
if partner.profile.send(field).blank?
58+
messages << "#{field.to_s.humanize.capitalize} can't be blank"
59+
end
60+
end
61+
errors.add(:base, messages.join(", ")) if messages.any?
62+
errors
63+
end
64+
65+
def check_social_media
66+
return if partner.organization.one_step_partner_invite
67+
return if partner.profile.website.present? || partner.profile.twitter.present? || partner.profile.facebook.present? || partner.profile.instagram.present?
68+
return if partner.partials_to_show.exclude?("media_information")
69+
70+
unless partner.profile.no_social_media_presence
71+
errors.add(:base, "No social media presence must be checked if you have not provided any of Website, Twitter, Facebook, or Instagram.")
72+
end
73+
errors
74+
end
4175
end
4276
end

clock.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Clockwork
88
handler do |job|
9-
puts "Running #{job}"
9+
Rails.logger.info("Running #{job}")
1010
end
1111

1212
DATA_TYPES = %w[Distribution Purchase Donation]

spec/requests/partners/profiles_requests_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@
107107
{
108108
partner: {
109109
name: "Partnerdude", profile: {
110-
no_social_media_presence: '0',
110+
no_social_media_presence: "0",
111111
website: "",
112112
twitter: "",
113113
facebook: "",
114114
instagram: "",
115-
agency_type: 'OTHER',
116-
address1: '123 Main St',
117-
city: 'Anytown',
118-
state: 'CA',
119-
zip_code: '12345',
120-
program_name: 'Test Program',
121-
program_description: 'Test Description'
115+
agency_type: "OTHER",
116+
address1: "123 Main St",
117+
city: "Anytown",
118+
state: "CA",
119+
zip_code: "12345",
120+
program_name: "Test Program",
121+
program_description: "Test Description"
122122
}
123123
}
124124
}

spec/services/partner_profile_update_service_spec.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
end
2222

2323
context "and there are other errors in the profile" do
24+
before do
25+
profile.partner.awaiting_review!
26+
end
2427
it "does not store the new values and it returns a failure" do
2528
expect(profile.served_areas.size).to eq(0)
2629
result = PartnerProfileUpdateService.new(profile.partner, partner_params, other_incorrect_attributes).call
2730
expect(result.success?).to eq(false)
28-
expect(result.error.to_s).to include("No social media presence must be checked")
31+
expect(result.error.to_s).to include("'No social media presence' must be checked")
2932
profile.reload
3033
expect(profile.served_areas.size).to eq(0)
3134
end
@@ -59,13 +62,16 @@
5962
end
6063
end
6164
context "and there are errors on the profile" do
65+
before do
66+
profile.partner.awaiting_review!
67+
end
6268
it "maintains the old values and returns failure" do
6369
profile.reload
6470
expect(profile.served_areas.size).to eq(2)
65-
PartnerProfileUpdateService.new(profile.partner, partner_params, other_incorrect_attributes).call
71+
result = PartnerProfileUpdateService.new(profile.partner, partner_params, other_incorrect_attributes).call
6672
profile.reload
6773
expect(profile.served_areas.size).to eq(2)
68-
expect(profile.errors).to_not be_empty
74+
expect(result.success?).to eq(false)
6975
end
7076
end
7177
end
@@ -108,7 +114,7 @@
108114
it "returns failure" do
109115
result = PartnerProfileUpdateService.new(partner, partner_params, basic_correct_attributes).call
110116
expect(result.success?).to eq(false)
111-
expect(result.error.to_s).to include("Partner '#{partner.name}' had error(s) preventing the profile from being updated: Email is invalid")
117+
expect(result.error.to_s).to include("Validation failed: Email is invalid")
112118
end
113119

114120
it "doesn't update the partner profile" do

spec/services/partners/request_approval_service_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,28 @@
3131
end
3232
end
3333

34+
context 'when the partner is not yet awaiting approval and mandatory fields are missed' do
35+
it 'should return an error re mandatory fields' do
36+
partner.update(name: '')
37+
partner.profile.update(
38+
agency_type: '',
39+
address1: '',
40+
city: '',
41+
state: '',
42+
zip_code: '',
43+
program_name: '',
44+
program_description: ''
45+
)
46+
47+
expect(subject.errors.full_messages)
48+
.to include("Name can't be blank, Agency type can't be blank, Address1 can't be blank, City can't be blank, State can't be blank, Zip code can't be blank, Program name can't be blank, Program description can't be blank")
49+
end
50+
end
51+
3452
context 'when the partner is not yet waiting for approval and there is no profile error' do
3553
it 'does not have an error' do
3654
partner.profile.update(website: 'website URL', facebook: '', twitter: '', instagram: '', no_social_media_presence: false)
55+
partner.organization.update(one_step_partner_invite: true)
3756
expect(subject.errors.full_messages).to be_empty
3857
end
3958
end
@@ -44,6 +63,7 @@
4463
allow(OrganizationMailer).to receive(:partner_approval_request).with(partner: partner, organization: partner.organization).and_return(fake_mailer)
4564
expect(partner.status).not_to eq(:awaiting_review)
4665
partner.profile.update(website: 'website URL')
66+
partner.organization.update(one_step_partner_invite: true)
4767
end
4868

4969
it 'should set the status on the partner record to awaiting_review' do

spec/system/partners/approval_process_spec.rb

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -109,89 +109,4 @@
109109
end
110110
end
111111
end
112-
113-
describe "request approval with invalid details" do
114-
let(:partner_user) { partner.primary_user }
115-
let(:partner) { FactoryBot.create(:partner) }
116-
117-
before do
118-
login_as(partner_user)
119-
visit partner_user_root_path
120-
click_on 'My Profile'
121-
end
122-
123-
subject { all('button', text: 'Submit for Approval').last.click }
124-
125-
context "Social media information is absent" do
126-
before do
127-
partner.profile.update(website: '', facebook: '', twitter: '', instagram: '', no_social_media_presence: false)
128-
end
129-
130-
context "partner status is invited" do
131-
before do
132-
partner.update(status: :invited)
133-
end
134-
135-
it "should render an error message", :aggregate_failures do
136-
subject
137-
assert page.has_content? 'No social media presence must be checked if you have not provided any of Website, Twitter, Facebook, or Instagram.'
138-
end
139-
140-
context "partner's organization one_step_partner_invite is true" do
141-
before do
142-
partner.organization.update(one_step_partner_invite: true)
143-
end
144-
145-
it "should render an error message about social media presence only", :aggregate_failures do
146-
subject
147-
assert page.has_content? 'You have submitted your details for approval.'
148-
end
149-
end
150-
end
151-
152-
context "partner status is awaiting_review" do
153-
before do
154-
partner.update(status: :awaiting_review)
155-
end
156-
157-
it "should render an error message", :aggregate_failures do
158-
subject
159-
assert page.has_content? 'This partner has already requested approval.'
160-
end
161-
end
162-
end
163-
164-
context "Mandatory fields are empty" do
165-
before do
166-
partner.update(name: '')
167-
partner.profile.update(
168-
agency_type: '',
169-
address1: '',
170-
city: '',
171-
state: '',
172-
zip_code: '',
173-
program_name: '',
174-
program_description: ''
175-
)
176-
end
177-
178-
context "partner status is invited" do
179-
before do
180-
partner.update(status: :invited)
181-
end
182-
183-
it "should render error messages for each missing field", :aggregate_failures do
184-
subject
185-
assert page.has_content? "Name can't be blank"
186-
assert page.has_content? "Agency type can't be blank"
187-
assert page.has_content? "Address1 can't be blank"
188-
assert page.has_content? "City can't be blank"
189-
assert page.has_content? "State can't be blank"
190-
assert page.has_content? "Zip code can't be blank"
191-
assert page.has_content? "Program name can't be blank"
192-
assert page.has_content? "Program description can't be blank"
193-
end
194-
end
195-
end
196-
end
197112
end

0 commit comments

Comments
 (0)