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

Update resource before to process email so that partner invitation emails have correct body #3027

7 changes: 6 additions & 1 deletion app/services/partner_invite_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ def call
return self unless valid?

partner.update!(status: 'invited')
User.invite!(email: partner.email, partner: partner.profile)
# skip invitation is necessary because when email will be send for this situation needs partner reference updated,
# and, in this case, we create invite, reload object and send invitation email.
user = User.invite!(email: partner.email, partner: partner.profile, skip_invitation: true)

user.reload
user.deliver_invitation
Comment on lines +16 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor suggestion, can you add a comment to explain why this was added? I sense it will be unclear to future maintainers about why this was added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @edwinthinks, I agree. Is it ok? Thanks

end

private
Expand Down
20 changes: 15 additions & 5 deletions app/views/users/mailer/invitation_instructions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,20 @@
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<p>Hi there!</p>

<p>Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!</p>
<p>Please, click the button below to accept this invitation and create a password to access your account.</p>
<% organization = @resource.partner&.organization %>
<% is_primary_partner = @resource.id == @resource.partner&.primary_user&.id %>
<p>Hello <%= @resource.email %></p>
<% if @resource.partner.present? && is_primary_partner %>
<p>You've been invited to become a partner with <strong><%= organization.name %>!</strong></p>
<p>Please click the link below to accept your invitation and create an account and you'll be able to begin requesting distributions.</p>
<p><strong> Please contact <%= organization.email %> if you are encountering any issues. </strong></p>
<% elsif @resource.partner.present? && !is_primary_partner %>
<p>You've been invited to <strong><%= @resource.partner.name %>'s</strong> account for requesting items from <strong><%= organization.name %>!</strong></p>
<p>Please click the link below to accept your invitation and create an account and you'll be able to begin requesting distributions.</p>
<% else %>
<p>Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!</p>
<p>Please, click the button below to accept this invitation and create a password to access your account.</p>
<% end %>
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="btn btn-primary">
<tbody>
<tr>
Expand All @@ -363,7 +373,7 @@
<% if @resource.invitation_due_at %>
<p><%= t("devise.mailer.invitation_instructions.accept_until", due_date: l(@resource.invitation_due_at, format: :'devise.mailer.invitation_instructions.accept_until_format')) %></p>
<% end %>
<p>If you didn't request an invite or feel you received this email in error, just ignore this email.</p>
<p>Feel free to ignore this email if you are not interested or if you feel it was sent by mistake.</p>
</td>
</tr>
</table>
Expand Down
41 changes: 41 additions & 0 deletions spec/mailers/custom_devise_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
RSpec.describe CustomDeviseMailer, type: :mailer, skip_seed: true do
describe "#invitation_instructions" do
let(:user) { create(:user) }
let(:mail) { described_class.invitation_instructions(user, SecureRandom.uuid) }

context "when partner is invited" do
let(:partner) do
partner = create(:partner, :uninvited)
partner.profile.primary_user.delete
partner.profile.reload
partner
end

let(:user) { create(:user, partner: partner.profile) }

it "invites to primary user" do
expect(mail.subject).to eq("You've been invited to be a partner with #{user.partner.organization.name}")
expect(mail.html_part.body).to include("You've been invited to become a partner with <strong>#{user.partner.organization.name}!</strong>")
end
end

context "when other partner users invited" do
let(:partner) { create(:partner) }
let(:user) { create(:user, partner: partner.profile) }

it "invites to partner user" do
expect(mail.subject).to eq("You've been invited to #{user.partner.name}'s partnerbase account")
expect(mail.html_part.body).to include("You've been invited to <strong>#{user.partner.name}'s</strong> account for requesting items from <strong>#{user.partner.organization.name}!")
end
end

context "when user is invited" do
let(:user) { create(:user) }

it "invites to user" do
expect(mail.subject).to eq("Your Human Essentials App Account Approval")
expect(mail.html_part.body).to include("Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!")
end
end
end
end
23 changes: 18 additions & 5 deletions spec/services/partner_invite_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
expect(subject).to be_a_kind_of(PartnerInviteService)
end

context 'when the partner user has already been invited' do
context 'when the user has already been invited' do
before do
expect(partner.profile.primary_user).not_to eq(nil)
end
Expand All @@ -18,28 +18,41 @@
end
end

context 'when the partner user has not been invited yet' do
context 'when the user has not been invited yet' do
let(:partner) do
partner = create(:partner, :uninvited)
partner.profile.primary_user.delete
partner.profile.reload
partner
end

let(:user) { instance_double(User, reload: -> {}, deliver_invitation: -> {}) }

before do
allow(User).to receive(:invite!)
allow(User).to receive(:invite!).and_return(user)
end

it 'should update the status of the partner to invited' do
expect { subject }.to change { partner.status }.to('invited')
end

it 'should invite them' do
it 'should create invite' do
subject
expect(User).to have_received(:invite!).with(
email: partner.email,
partner: partner.profile
partner: partner.profile,
skip_invitation: true
)
end

it 'should reload user object' do
subject
expect(user).to have_received(:reload)
end

it 'should invite them' do
subject
expect(user).to have_received(:deliver_invitation)
end
end
end