Skip to content

Commit

Permalink
Enable/disable affiliate sales data
Browse files Browse the repository at this point in the history
  • Loading branch information
dacook committed Jul 17, 2024
1 parent a100b68 commit c5c2f8f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
14 changes: 11 additions & 3 deletions app/controllers/admin/connected_apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ class ConnectedAppsController < ApplicationController
def create
authorize! :admin, enterprise

app = ConnectedApp.create!(enterprise_id: enterprise.id)
app.connect(api_key: spree_current_user.spree_api_key, channel: SessionChannel.for_request(request))
attributes = {}
attributes[:type] = connected_app_params[:type] if connected_app_params[:type]

app = ConnectedApp.create!(enterprise_id: enterprise.id, **attributes)
app.connect(api_key: spree_current_user.spree_api_key,
channel: SessionChannel.for_request(request))

render_panel
end

def destroy
authorize! :admin, enterprise

app = enterprise.connected_apps.first
app = enterprise.connected_apps.find(params.require(:id))
app.destroy

render_panel
Expand All @@ -29,5 +33,9 @@ def enterprise
def render_panel
redirect_to "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel"
end

def connected_app_params
params.permit(:type)
end
end
end
4 changes: 3 additions & 1 deletion app/models/connected_apps/affiliate_sales_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#
module ConnectedApps
class AffiliateSalesData < ConnectedApp
def connect; end
def connect(_opts)
update! data: true # not-nil value indicates it is ready
end

def disconnect; end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
%p= t ".tagline"
%div
- if connected_app.nil?
= button_to t(".enable"), admin_enterprise_connected_apps_path(enterprise.id), method: :post, disabled: !managed_by_user?(enterprise)
= button_to t(".enable"), admin_enterprise_connected_apps_path(enterprise.id, type: "ConnectedApps::AffiliateSalesData"), method: :post, disabled: !managed_by_user?(enterprise)
-# This is only seen by super-admins:
%em= t(".need_to_be_manager") unless managed_by_user?(enterprise)
- else
= button_to t(".disable"), admin_enterprise_connected_app_path(0, enterprise_id: enterprise.id), method: :delete
= button_to t(".disable"), admin_enterprise_connected_app_path(connected_app.id, enterprise_id: enterprise.id), method: :delete
%hr
.connected-app__description
= t ".description_html"
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
&nbsp;
= t ".loading"
- else
= button_to t(".disable"), admin_enterprise_connected_app_path(0, enterprise_id: enterprise.id), method: :delete
= button_to t(".disable"), admin_enterprise_connected_app_path(connected_app.id, enterprise_id: enterprise.id), method: :delete

.connected-app__connection
- if connected_app&.ready?
Expand Down
52 changes: 44 additions & 8 deletions spec/system/admin/enterprises/connected_apps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@
end

describe "Discover Regenerative" do
let(:section_heading) { self.class.description }

it "can be enabled and disabled" do
visit edit_admin_enterprise_path(enterprise)

scroll_to :bottom
click_link "Connected apps"

within section_containing_heading "Discover Regenerative" do
within section_containing_heading do
click_button "Allow data sharing"
end

# (page is reloaded so we need to evaluate within block again)
within section_containing_heading "Discover Regenerative" do
within section_containing_heading do
expect(page).not_to have_button "Allow data sharing"
expect(page).to have_button "Loading", disabled: true

perform_enqueued_jobs(only: ConnectAppJob)
end

within section_containing_heading "Discover Regenerative" do
expect(page).not_to have_button "Loading", disabled: true
expect(page).to have_content "account is connected"
expect(page).to have_link "Manage listing"

click_button "Stop sharing"
end

within section_containing_heading "Discover Regenerative" do
within section_containing_heading do
expect(page).to have_button "Allow data sharing"
expect(page).not_to have_button "Stop sharing"
expect(page).not_to have_content "account is connected"
Expand All @@ -68,15 +68,51 @@

visit "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel"

within section_containing_heading "Discover Regenerative" do
within section_containing_heading do
expect(page).to have_button("Allow data sharing", disabled: true)
expect(page).to have_content "Only managers can connect apps."
end
end
end

describe "Affiliate Sales Data" do
let(:section_heading) { "INRAE / UFC QUE CHOISIR Research" }

it "can be enabled and disabled" do
visit edit_admin_enterprise_path(enterprise)

scroll_to :bottom
click_link "Connected apps"

within section_containing_heading do
click_button "Allow data sharing"
end

# (page is reloaded so we need to evaluate within block again)
within section_containing_heading do
expect(page).not_to have_button "Allow data sharing"
click_button "Stop sharing"
end

within section_containing_heading do
expect(page).to have_button "Allow data sharing"
expect(page).not_to have_button "Stop sharing"
end
end

it "can't be enabled by non-manager" do
login_as create(:admin_user)

visit "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel"

within section_containing_heading do
expect(page).to have_button("Allow data sharing", disabled: true)
expect(page).to have_content "Only managers can connect apps."
end
end
end

def section_containing_heading(heading)
def section_containing_heading(heading = section_heading)
page.find("h3", text: heading).ancestor("section")
end
end

0 comments on commit c5c2f8f

Please sign in to comment.