Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4486: address pickup email issues #4648

Merged
merged 25 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b15db30
Add validation on pick_up_email
panacotar Sep 11, 2024
e4ffe4e
Add specs for pick_up_email validation
panacotar Sep 11, 2024
df4135c
fix typo in specs
panacotar Sep 11, 2024
ae32e37
update distribution mailer spec to support multiple pick up emails
panacotar Sep 12, 2024
7a78bc1
change pick up person to plural
panacotar Sep 12, 2024
3b32502
handle multiple pick up addresses in cc
panacotar Sep 12, 2024
ab1df0a
Merge branch 'main' into 4486-address-pickup-email-issues
panacotar Sep 12, 2024
202efcc
add another test for pick_up_email validator
panacotar Sep 12, 2024
d3dfed5
update pick_up_email validator to disregard commas at beginning, end …
panacotar Sep 12, 2024
1389c1d
flatten the cc array after merging with pick_up_emails
panacotar Sep 12, 2024
5be76e1
update test with unique pick up emails
panacotar Sep 12, 2024
70777ab
not allow repeated pick_up_email addresses
panacotar Sep 12, 2024
9d3ed3c
add tests for profile#split_pick_up_emails
panacotar Sep 13, 2024
53e98af
add split_pick_up_emails method on Profile model
panacotar Sep 13, 2024
d21aed3
update distribution mailer with split_pick_up_emails
panacotar Sep 13, 2024
bd72060
update test to use be_valid
panacotar Sep 13, 2024
92ac8fb
Merge branch 'main' into 4486-address-pickup-email-issues
panacotar Sep 13, 2024
1fb21ab
remove context wrapper
panacotar Sep 15, 2024
f9db471
add more tests for profile pick_up_emails
panacotar Sep 15, 2024
02fb105
fix split_pick_up_emails to handle edge cases
panacotar Sep 15, 2024
463a1f1
Merge branch 'main' into 4486-address-pickup-email-issues
panacotar Sep 15, 2024
4e4300a
add instructions for comma-separated emails
panacotar Sep 16, 2024
91d4db1
add placeholder to pick_up_email input
panacotar Sep 16, 2024
ab7eba7
Merge branch 'main' into 4486-address-pickup-email-issues
panacotar Sep 16, 2024
5bafd76
change emails label to addresses
panacotar Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/mailers/distribution_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def partner_mailer(current_organization, distribution, subject, distribution_cha
pdf = DistributionPdf.new(current_organization, @distribution).compute_and_render
attachments[format("%s %s.pdf", @partner.name, @distribution.created_at.strftime("%Y-%m-%d"))] = pdf
cc = [@partner.email]
cc.push(@partner.profile&.pick_up_email) if distribution.pick_up?
if distribution.pick_up? && @partner.profile&.pick_up_email
pick_up_emails = @partner.profile.split_pick_up_emails
cc.push(pick_up_emails)
end
cc.flatten!
cc.compact!
cc.uniq!

Expand Down
24 changes: 24 additions & 0 deletions app/models/partners/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Profile < Base

validate :client_share_is_0_or_100
validate :has_at_least_one_request_setting
validate :pick_up_email_addresses

self.ignored_columns = %w[
evidence_based_description
Expand All @@ -118,6 +119,12 @@ def client_share_total
served_areas.map(&:client_share).compact.sum
end

def split_pick_up_emails
return nil if pick_up_email.nil?

pick_up_email.split(/,|\s+/).compact_blank
end

private

def check_social_media
Expand All @@ -144,5 +151,22 @@ def has_at_least_one_request_setting
errors.add(:base, "At least one request type must be set")
end
end

def pick_up_email_addresses
# pick_up_email is a string of comma-separated emails, check specs for details
return if pick_up_email.nil?

emails = split_pick_up_emails
if emails.size > 3
errors.add(:pick_up_email, "There can't be more than three pick up email addresses.")
nil
end
if emails.uniq.size != emails.size
errors.add(:pick_up_email, "There should not be repeated email addresses.")
end
emails.each do |e|
errors.add(:pick_up_email, "Invalid email address for '#{e}'.") unless e.match? URI::MailTo::EMAIL_REGEXP
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/partners/profiles/edit/_pick_up_person.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="card-body">
<%= form.input :pick_up_name, label: "Pick Up Person Name", class: "form-control", wrapper: :input_group %>
<%= form.input :pick_up_phone, label: "Pick Up Person's Phone #", class: "form-control", wrapper: :input_group %>
<%= form.input :pick_up_email, label: "Pick Up Person's Email", class: "form-control", wrapper: :input_group %>
<%= form.input :pick_up_email, label: "Pick Up Person's Email (comma-separated if multiple addresses)", placeholder: "[email protected], [email protected]", class: "form-control", wrapper: :input_group %>
</div>
</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions spec/mailers/distribution_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
let(:distribution) { create(:distribution, organization: user.organization, comment: "Distribution comment", partner: partner) }
let(:request) { create(:request, distribution: distribution) }

let(:pick_up_email) { '[email protected]' }
let(:pick_up_email) { '[email protected], [email protected]' }
let(:pick_up_emails) { ['[email protected]', '[email protected]'] }

before do
organization.default_email_text = "Default email text example\n\n%{delivery_method} %{distribution_date}\n\n%{partner_name}\n\n%{comment}"
Expand All @@ -23,7 +24,7 @@
expect(mail.body.encoded).to match("Default email text example")
expect(mail.html_part.body).to match(%(From: <a href="mailto:[email protected]">[email protected]</a>))
expect(mail.to).to eq([distribution.request.user_email])
expect(mail.cc).to eq([distribution.partner.email, pick_up_email])
expect(mail.cc).to eq([distribution.partner.email, pick_up_emails.first, pick_up_emails.second])
expect(mail.from).to eq(["[email protected]"])
expect(mail.subject).to eq("test subject from TEST ORG")
end
Expand All @@ -43,10 +44,11 @@
expect(mail.body.encoded).to match("picked up")
end

context 'when parners profile pick_up_email is present' do
it 'sends email to the primary contact and partner`s pickup person' do
context 'when partners profile pick_up_email is present' do
it 'sends email to the primary contact and partner`s pickup persons' do
expect(mail.cc.first).to match(partner.email)
expect(mail.cc.second).to match(pick_up_email)
expect(mail.cc.second).to match(pick_up_emails.first)
expect(mail.cc.third).to match(pick_up_emails.second)
end

context 'when pickup person happens to be the same as the primary contact' do
Expand Down
79 changes: 79 additions & 0 deletions spec/models/partners/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,85 @@
end
end

describe "split pick up email" do
let(:profile) { build(:partner_profile, pick_up_email: "[email protected], [email protected]") }
it "should disregard commas at the beginning or end of the string" do
profile.update(pick_up_email: ", [email protected], [email protected],")
expect(profile.split_pick_up_emails).to match_array(["[email protected]", "[email protected]"])
end

it "should allow optional whitespace between email addresses" do
profile.update(pick_up_email: "[email protected], [email protected]")
expect(profile.split_pick_up_emails).to match_array(["[email protected]", "[email protected]"])
profile.update(pick_up_email: "[email protected],[email protected]")
expect(profile.split_pick_up_emails).to match_array(["[email protected]", "[email protected]"])
end

it "should handle nil value" do
profile.update(pick_up_email: nil)
expect(profile.split_pick_up_emails).to be_nil
end

it "should return empty array if for when pick_up_email is an empty string" do
profile.update(pick_up_email: "")
expect(profile.split_pick_up_emails).to match_array([])
end

it "should correctly split strings" do
profile.update(pick_up_email: "test me, [email protected], [email protected], test me")
expect(profile.split_pick_up_emails).to match_array(["test", "me", "[email protected]", "[email protected]", "test", "me"])
profile.update(pick_up_email: "test me. [email protected], [email protected]. test me")
expect(profile.split_pick_up_emails).to match_array(["test", "me.", "[email protected]", "[email protected].", "test", "me"])
end
end

describe "pick up email address validation" do
context "number of email addresses" do
let(:profile) { build(:partner_profile, pick_up_email: "[email protected], [email protected], [email protected], [email protected]") }
it "should not allow more than three email addresses" do
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected], [email protected], [email protected]")
expect(profile).to be_valid
end

it "should not allow repeated email addresses" do
profile.update(pick_up_email: "[email protected], [email protected], [email protected]")
expect(profile).to_not be_valid
end
end

context "invalid emails" do
let(:profile) { build(:partner_profile, pick_up_email: "[email protected], [email protected], asdf") }
it "should not allow invalid email addresses" do
expect(profile).to_not be_valid
profile.update(pick_up_email: "test me, [email protected], [email protected], test me")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected], [email protected]")
expect(profile).to be_valid
end

it "should not allow input having emails separated by non-word characters" do
profile.update(pick_up_email: "test me. [email protected], [email protected]. test me")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected]. [email protected]")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected][email protected]")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected]/ [email protected]/ [email protected]")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected] [email protected] [email protected]")
expect(profile).to_not be_valid
profile.update(pick_up_email: "[email protected]' [email protected]' [email protected]")
expect(profile).to_not be_valid
end

it "should handle nil value" do
profile.update(pick_up_email: nil)
expect(profile).to be_valid
end
end
end

describe "client share behaviour" do
context "no served areas" do
let(:profile) { build(:partner_profile) }
Expand Down