diff --git a/app/services/partner_invite_service.rb b/app/services/partner_invite_service.rb index baa77d0475..7f11d4b8ea 100644 --- a/app/services/partner_invite_service.rb +++ b/app/services/partner_invite_service.rb @@ -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 end private diff --git a/app/views/users/mailer/invitation_instructions.html.erb b/app/views/users/mailer/invitation_instructions.html.erb index 5367fb0f52..4f8a2cb788 100644 --- a/app/views/users/mailer/invitation_instructions.html.erb +++ b/app/views/users/mailer/invitation_instructions.html.erb @@ -341,10 +341,20 @@
-

Hi there!

- -

Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!

-

Please, click the button below to accept this invitation and create a password to access your account.

+ <% organization = @resource.partner&.organization %> + <% is_primary_partner = @resource.id == @resource.partner&.primary_user&.id %> +

Hello <%= @resource.email %>

+ <% if @resource.partner.present? && is_primary_partner %> +

You've been invited to become a partner with <%= organization.name %>!

+

Please click the link below to accept your invitation and create an account and you'll be able to begin requesting distributions.

+

Please contact <%= organization.email %> if you are encountering any issues.

+ <% elsif @resource.partner.present? && !is_primary_partner %> +

You've been invited to <%= @resource.partner.name %>'s account for requesting items from <%= organization.name %>!

+

Please click the link below to accept your invitation and create an account and you'll be able to begin requesting distributions.

+ <% else %> +

Your request has been approved and you're invited to become an user of the Human Essentials inventory management system!

+

Please, click the button below to accept this invitation and create a password to access your account.

+ <% end %> @@ -363,7 +373,7 @@ <% if @resource.invitation_due_at %>

<%= t("devise.mailer.invitation_instructions.accept_until", due_date: l(@resource.invitation_due_at, format: :'devise.mailer.invitation_instructions.accept_until_format')) %>

<% end %> -

If you didn't request an invite or feel you received this email in error, just ignore this email.

+

Feel free to ignore this email if you are not interested or if you feel it was sent by mistake.

diff --git a/spec/mailers/custom_devise_mailer_spec.rb b/spec/mailers/custom_devise_mailer_spec.rb new file mode 100644 index 0000000000..001e9818f6 --- /dev/null +++ b/spec/mailers/custom_devise_mailer_spec.rb @@ -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 #{user.partner.organization.name}!") + 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 #{user.partner.name}'s account for requesting items from #{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 diff --git a/spec/services/partner_invite_service_spec.rb b/spec/services/partner_invite_service_spec.rb index c1435144fd..b763a41d5e 100644 --- a/spec/services/partner_invite_service_spec.rb +++ b/spec/services/partner_invite_service_spec.rb @@ -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 @@ -18,7 +18,7 @@ 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 @@ -26,20 +26,33 @@ 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