Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Count a seller's team by people, not by roles

Damian Legawiec95e076c83480
diff --git a/spree/api/app/controllers/spree/api/v3/seller/team_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/team_controller.rb
index 95d6f72341..2aaaa1a0c8 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/team_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/team_controller.rb
@@ -53,7 +53,12 @@ module Spree
# seller cannot repair themselves.
def destroy
removed = current_seller.with_lock do
- next false if current_seller.users.count <= 1
+ # `distinct`: `users` reaches through role_users, so a member
+ # holding two roles on this seller is counted twice — while
+ # `remove_user` deletes every one of their roles at once. Without
+ # it, a two-role seller with one person reads as a team of two
+ # and this check would let them remove themselves.
+ next false if current_seller.users.distinct.count <= 1
current_seller.remove_user(@member)
true
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/team_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/team_controller_spec.rb
index 8b47a1953d..111c2df288 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/team_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/team_controller_spec.rb
@@ -97,6 +97,20 @@ RSpec.describe Spree::Api::V3::Seller::TeamController, type: :controller do
expect(seller.reload.users).to include(seller_user)
end
+ # `users` reaches through role_users, so one person holding two roles is
+ # two rows — while removing them deletes both at once. Counted without
+ # `distinct`, a one-person team would read as two and let them go.
+ it 'refuses to remove the last member even when they hold two roles' do
+ second_role = seller.roles.create!(name: "Packer #{SecureRandom.hex(3)}")
+ seller.add_user(seller_user, second_role)
+ expect(seller.reload.users.count).to eq(2)
+
+ delete :destroy, params: { id: seller_user.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(seller.reload.users).to include(seller_user)
+ end
+
# Counted and removed under a lock on the seller. Unlocked, two concurrent
# deletes against a two-member team both observe two and both proceed,
# leaving a seller nobody can sign in to — a state only an operator can