Skip to content

Commit

Permalink
Add User#affiliate_enterprises
Browse files Browse the repository at this point in the history
  • Loading branch information
dacook committed Jul 25, 2024
1 parent da7bbcf commit e9d7a0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/spree/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def disabled=(value)
self.disabled_at = value == '1' ? Time.zone.now : nil
end

def affiliate_enterprises
return [] unless Flipper.enabled?(:affiliate_sales_data, self)

Enterprise.joins(:connected_apps)
.where(connected_apps: { type: "ConnectedApps::AffiliateSalesData" })
.where.not(connected_apps: { data: nil })
end

protected

def password_required?
Expand Down
27 changes: 27 additions & 0 deletions spec/models/spree/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,31 @@
expect(user.disabled).to be_falsey
end
end

describe "#affiliate_enterprises" do
let(:user) { create(:user) }
let(:affiliate_enterprise) { create(:enterprise) }
let(:other_enterprise) { create(:enterprise) }
subject{ user.affiliate_enterprises }

before do
ConnectedApps::AffiliateSalesData.create(enterprise: affiliate_enterprise, data: true)
end

context "user does not have feature" do
it { is_expected.to be_empty }
end

context "user has feature affiliate_sales_data" do
before do
Flipper.enable_actor(:affiliate_sales_data, user)
user.reload
end

it "includes only affiliate enterprises" do
is_expected.to include affiliate_enterprise
is_expected.not_to include other_enterprise
end
end
end
end

0 comments on commit e9d7a0b

Please sign in to comment.