Skip to content

Commit

Permalink
Add invite and approve capability
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-truong-main committed Feb 1, 2024
1 parent 6422448 commit 95170f4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/controllers/partners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ def approve_application
end
end

def single_step_invite_and_approve
# Invite the partner
partner = current_organization.partners.find(params[:id])

partner_invite_service = PartnerInviteService.new(partner: partner, force: true)
partner_invite_service.call

# If no errors inviting, then approve the partner
if partner_invite_service.errors.none?
partner_approval_service = PartnerApprovalService.new(partner: partner)
partner_approval_service.call

if partner_approval_service.errors.none?
redirect_to partners_path, notice: "Partner invited and approved!"
else
redirect_to partners_path, error: "Failed to approve partner because: #{partner_approval_service.errors.full_messages}"
end
else
redirect_to partners_path, notice: "Failed to invite #{partner.name}! #{partner_invite_service.errors.full_messages}"
end
end
def show
@partner = current_organization.partners.find(params[:id])
@impact_metrics = @partner.impact_metrics unless @partner.uninvited?
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/ui_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def invite_button_to(link, options = {}, properties = {})
_link_to link, { icon: "envelope", type: "warning", text: "Invite", size: "xs" }.merge(options), properties
end

def invite_and_approve_button_to(link, options = {}, properties = {})
properties = { method: options[:method]&.to_sym || :post, rel: "nofollow", data: { confirm: options[:confirm] || "Are you sure?" } }.merge(properties)
_link_to link, { icon: "envelope", type: "info", text: "Invite and Approve", size: "xs" }.merge(options), properties
end

def refresh_button_to(link, options = {}, properties = {})
_link_to link, { icon: "sync", type: "info", text: "Refresh", size: "md" }.merge(options), properties
end
Expand Down
7 changes: 6 additions & 1 deletion app/views/partners/_partner_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% status = partner_row.status%>
<% can_single_step_invite_and_approve = partner_row.organization.use_single_step_invite_and_approve_partner_process %>
<tr>
<td class='text-blue-500'><%= link_to partner_row.name, partner_path(partner_row) %></td>
<td><%= link_to partner_row.email, "mailto:#{partner_row.email}" %></td>
Expand All @@ -25,7 +26,11 @@
<td>
<% case status %>
<% when "uninvited" %>
<%= invite_button_to(invite_partner_path(partner_row), confirm: "Send an invitation to #{partner_row.name} to begin using the partner application?") %>
<% if can_single_step_invite_and_approve %>
<%= invite_and_approve_button_to(single_step_invite_and_approve_partner_path(partner_row), confirm: "Single step invite and approve #{partner_row.name} to begin using the partner application?") %>
<% else %>
<%= invite_button_to(invite_partner_path(partner_row), confirm: "Send an invitation to #{partner_row.name} to begin using the partner application?") %>
<% end %>
<% when "invited" %>
<%= view_button_to partner_path(partner_row) + "#partner-information", { text: "Review Application", icon: "check", type: "warning" } %>
<%= invite_button_to(invite_partner_path(partner_row), confirm: "Re-send an invitation to #{partner_row.name}?", text: 'Re-send Invite') %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def set_up_flipper
patch :profile
get :approve_application
post :invite
post :single_step_invite_and_approve
post :invite_partner_user
post :recertify_partner
put :deactivate
Expand Down
61 changes: 61 additions & 0 deletions spec/requests/partners_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,65 @@
end
end
end

describe "POST # single_step_invite_and_approve" do
let(:partner) { create(:partner, organization: @organization) }

context "when invitation succeeded and approval succeed" do
before do
fake_partner_invite_service = instance_double(PartnerInviteService, call: nil, errors: [])
allow(PartnerInviteService).to receive(:new).and_return(fake_partner_invite_service)

fake_partner_approval_service = instance_double(PartnerApprovalService, call: nil, errors: [])
allow(PartnerApprovalService).to receive(:new).with(partner: partner).and_return(fake_partner_approval_service)
end

it "sends invitation email and approve partner in single step" do
post single_step_invite_and_approve_partner_path(default_params.merge(id: partner.id))

expect(PartnerInviteService).to have_received(:new).with(partner: partner, force: true)
expect(response).to have_http_status(:found)

expect(PartnerApprovalService).to have_received(:new).with(partner: partner)
expect(response).to redirect_to(partners_path(organization_id: @organization.to_param))
expect(flash[:notice]).to eq("Partner invited and approved!")
end
end

context "when invitation failed" do
let(:fake_error_msg) { Faker::Games::ElderScrolls.dragon }

before do
fake_partner_invite_service = instance_double(PartnerInviteService, call: nil)
allow(PartnerInviteService).to receive(:new).with(partner: partner, force: true).and_return(fake_partner_invite_service)
allow(fake_partner_invite_service).to receive_message_chain(:errors, :none?).and_return(false)
allow(fake_partner_invite_service).to receive_message_chain(:errors, :full_messages).and_return(fake_error_msg)
end

it "should redirect to the partners index page with a notice flash message" do
post single_step_invite_and_approve_partner_path(default_params.merge(id: partner.id))

expect(response).to redirect_to(partners_path(organization_id: @organization.to_param))
expect(flash[:notice]).to eq("Failed to invite #{partner.name}! #{fake_error_msg}")
end
end

context "when approval fails" do
let(:fake_error_msg) { Faker::Games::ElderScrolls.dragon }

before do
fake_partner_approval_service = instance_double(PartnerApprovalService, call: nil)
allow(PartnerApprovalService).to receive(:new).with(partner: partner).and_return(fake_partner_approval_service)
allow(fake_partner_approval_service).to receive_message_chain(:errors, :none?).and_return(false)
allow(fake_partner_approval_service).to receive_message_chain(:errors, :full_messages).and_return(fake_error_msg)
end

it "should redirect to the partners index page with a notice flash message" do
post single_step_invite_and_approve_partner_path(default_params.merge(id: partner.id))

expect(response).to redirect_to(partners_path(organization_id: @organization.to_param))
expect(flash[:error]).to eq("Failed to approve partner because: #{fake_error_msg}")
end
end
end
end

0 comments on commit 95170f4

Please sign in to comment.