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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Address review findings on the seller panel bootstrap
Damian Legawiec33177b789fe7
diff --git a/packages/seller-dashboard/src/api-client.ts b/packages/seller-dashboard/src/api-client.tsindex 0b474fce52..bb880476d4 100644--- a/packages/seller-dashboard/src/api-client.ts+++ b/packages/seller-dashboard/src/api-client.ts@@ -80,17 +80,37 @@ export function setActiveSeller(sellerId: string): void {sellerClient().setSeller(sellerId)}-/** Persists the choice so a reload does not drop the seller back to the picker. */+/**+ * Persists the choice so a reload does not drop the seller back to the picker.+ *+ * Every access is guarded: a browser that blocks storage (sandboxed iframe,+ * blocked third-party storage, some private modes) throws on `localStorage`,+ * and these run at boot and during logout — the two places a throw would+ * leave the panel unusable rather than merely forgetful. Losing the memory is+ * a small cost; failing to boot or to sign out is not.+ */export function rememberSeller(sellerId: string): void {- localStorage.setItem(SELLER_STORAGE_KEY, sellerId)+ try {+ localStorage.setItem(SELLER_STORAGE_KEY, sellerId)+ } catch {+ // Storage unavailable — the choice lives for this page only.+ }setActiveSeller(sellerId)}export function forgetSeller(): void {- localStorage.removeItem(SELLER_STORAGE_KEY)+ try {+ localStorage.removeItem(SELLER_STORAGE_KEY)+ } catch {+ // Nothing to forget; clearing the header below is what matters for logout.+ }client?.setSeller('')}export function rememberedSeller(): string | null {- return localStorage.getItem(SELLER_STORAGE_KEY)+ try {+ return localStorage.getItem(SELLER_STORAGE_KEY)+ } catch {+ return null+ }}diff --git a/packages/seller-dashboard/src/vite/index.ts b/packages/seller-dashboard/src/vite/index.tsindex c37b2162ec..163cf8551b 100644--- a/packages/seller-dashboard/src/vite/index.ts+++ b/packages/seller-dashboard/src/vite/index.ts@@ -36,12 +36,14 @@ export function spreeSellerDashboardPlugin(): PluginOption[] {const hostRoot = options.root ?? process.cwd()return [- // Name this shell so Tailwind scans the panel's own components; core- // defaults to the operator's dashboard, which a seller's host does not- // install.+ // This shell is always scanned, whatever the host passes. Core defaults to+ // the operator's dashboard, which a seller's host does not install — and a+ // host naming its own shell is adding one, not replacing the panel whose+ // components it renders. Overwriting instead of merging drops the panel's+ // classes from the production CSS, and only in a built bundle....spreeDashboardCorePlugin({- shellPackages: ['@spree/seller-dashboard'],...options,+ shellPackages: ['@spree/seller-dashboard', ...(options.shellPackages ?? [])],}),sellerRouterPlugin(hostRoot, options),]diff --git a/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rbindex 350852c630..7a66c36977 100644--- a/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb@@ -26,7 +26,9 @@ module Spreedef lookupreturn unless load_invitation- render json: Spree.api.seller_invitation_serializer.new(@invitation).serializable_hash+ render json: Spree.api.seller_invitation_serializer.new(+ @invitation, params: { store: @invitation.store }+ ).serializable_hashend# POST /api/v3/seller/auth/invitations/:id/accept?token=:tokendiff --git a/spree/api/app/controllers/spree/api/v3/seller/team_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/team_controller.rbindex edea060e48..95d6f72341 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@@ -46,17 +46,26 @@ module Spree# Revokes a member's access. The seller keeps at least one member:# emptying the team would leave a seller nobody can sign in to, and# only the operator could put someone back.+ #+ # Counted and removed under a lock on the seller. Unlocked, two+ # concurrent deletes against a two-member team both see two and both+ # proceed — the one state this endpoint exists to prevent, and one a+ # seller cannot repair themselves.def destroy- if members.size <= 1- return render_error(- code: ErrorHandler::ERROR_CODES[:processing_error],- message: Spree.t(:seller_team_last_member),- status: :unprocessable_content- )+ removed = current_seller.with_lock do+ next false if current_seller.users.count <= 1++ current_seller.remove_user(@member)+ trueend- current_seller.remove_user(@member)- head :no_content+ return head :no_content if removed++ render_error(+ code: ErrorHandler::ERROR_CODES[:processing_error],+ message: Spree.t(:seller_team_last_member),+ status: :unprocessable_content+ )endprotecteddiff --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.rbindex 792bfbdcd5..8b47a1953d 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,21 @@ RSpec.describe Spree::Api::V3::Seller::TeamController, type: :controller doexpect(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+ # repair. Asserted here as the invariant: whatever the order of removals,+ # the team never reaches zero through this endpoint.+ it 'never lets the team reach zero' do+ second = create(:admin_user, :without_admin_role).tap { |user| seller.add_user(user) }++ delete :destroy, params: { id: second.prefixed_id }, as: :json+ expect(response).to have_http_status(:no_content)++ delete :destroy, params: { id: seller_user.prefixed_id }, as: :json+ expect(response).to have_http_status(:unprocessable_content)+ expect(seller.reload.users.count).to eq(1)+ endit "404s on someone outside this seller's team" doother = create(:seller, :approved, store: store)outsider = create(:admin_user, :without_admin_role)diff --git a/spree/core/app/finders/spree/sellers/panel_url.rb b/spree/core/app/finders/spree/sellers/panel_url.rbindex c428fcf868..633319b1a2 100644--- a/spree/core/app/finders/spree/sellers/panel_url.rb+++ b/spree/core/app/finders/spree/sellers/panel_url.rb@@ -15,9 +15,18 @@ module Spree# 2. the `/sellers` mount this app serves, when a bundle is configured# 3. the dashboard origin, as a last resort#- # The last fallback is deliberate. A marketplace that has not deployed a- # panel still has to send a working invitation, and the staff dashboard at- # least exists; sending nowhere would be worse.+ # The last fallback is deliberate but imperfect, and worth understanding.+ # A seller who follows it lands on the staff dashboard's acceptance page,+ # which authenticates through the Admin API: accepting there DOES join them+ # to the seller (the invitation's role is what creates membership), but the+ # session it issues carries the `admin_api` audience, so they cannot use+ # the seller panel with it and must sign in again once one exists.+ #+ # Kept rather than raising because the alternative is worse: a marketplace+ # that has not deployed a panel yet would be unable to invite anyone at+ # all, and the invitation still needs to reach a page that can accept it.+ # Configure SPREE_SELLER_PANEL_URL, or serve the bundle at `/sellers`, and+ # this branch is never taken. A warning is logged when it is.class PanelUrl# @param store [Spree::Store, nil] store whose URL ends the fallback chain# @return [String] origin without a trailing slash, e.g. +https://sellers.shop.com+@@ -26,7 +35,16 @@ module Spreereturn configured.to_s.chomp('/') if configured- mounted_panel_url(store) || Spree::Stores::DashboardUrl.call(store: store)+ mounted = mounted_panel_url(store)+ return mounted if mounted++ Rails.logger.warn(+ '[Spree] No seller panel is configured, so seller invitations point at the staff ' \+ 'dashboard. Accepting there joins the seller but issues an admin session, not a ' \+ 'seller one. Set SPREE_SELLER_PANEL_URL or serve a seller panel build at /sellers.'+ )++ Spree::Stores::DashboardUrl.call(store: store)end# The `/sellers` mount this app serves, when `spree_dashboard` isdiff --git a/spree/core/lib/tasks/seller_sample_data.rake b/spree/core/lib/tasks/seller_sample_data.rakeindex f364492e58..5795d45f72 100644--- a/spree/core/lib/tasks/seller_sample_data.rake+++ b/spree/core/lib/tasks/seller_sample_data.rake@@ -1,7 +1,15 @@namespace :spree donamespace :sellers do- desc 'Create a sample seller with a signed-in owner and a pending invitation, for trying the seller panel'+ desc 'Create a sample seller with a signed-in owner and a pending invitation, for trying the seller panel (development/test only)'task sample_data: :environment do+ # Refused outside development and test on purpose. This task writes a+ # known password, and `find_or_initialize_by(email:)` can land on a real+ # account — resetting a live user's credentials from a sample-data task+ # is not a mistake worth leaving available.+ unless Rails.env.development? || Rails.env.test?+ abort "spree:sellers:sample_data is for development and test only (RAILS_ENV=#{Rails.env})."+ end+store = Spree::Store.default || Spree::Store.firstabort 'No store found — run db:seed first.' if store.nil?@@ -9,14 +17,22 @@ namespace :spree dopassword = ENV.fetch('SELLER_PASSWORD', 'spree123')seller = Spree::Seller.find_or_initialize_by(name: ENV.fetch('SELLER_NAME', 'Bright Sparks'), store: store)+ seller.status = 'approved' if seller.new_record?seller.save! if seller.new_record?- seller.update!(status: 'approved') unless seller.approved?- owner = Spree.admin_user_class.find_or_initialize_by(email: email)- owner.password = password- owner.first_name ||= 'Ada'- owner.last_name ||= 'Lovelace'- owner.save!+ owner = Spree.admin_user_class.find_by(email: email)++ # An existing account keeps its own password. Re-running the task then+ # tells you to use the credentials you already have, rather than silently+ # changing them underneath you.+ if owner+ puts "Reusing existing user #{email} — password unchanged."+ else+ owner = Spree.admin_user_class.create!(+ email: email, password: password, first_name: 'Ada', last_name: 'Lovelace'+ )+ end+seller.add_user(owner)# One outstanding offer, so the panel's pending-invitations card has@@ -27,7 +43,7 @@ namespace :spree doendputs "Seller: #{seller.name} (#{seller.prefixed_id}, #{seller.status})"- puts "Sign in: #{email} / #{password}"+ puts "Sign in: #{email}"puts "Pending: #{seller.invitations.pending.pluck(:email).join(', ')}"endenddiff --git a/spree/core/spec/finders/spree/sellers/panel_url_spec.rb b/spree/core/spec/finders/spree/sellers/panel_url_spec.rbindex f7e4aff677..7f661420d2 100644--- a/spree/core/spec/finders/spree/sellers/panel_url_spec.rb+++ b/spree/core/spec/finders/spree/sellers/panel_url_spec.rb@@ -34,10 +34,25 @@ RSpec.describe Spree::Sellers::PanelUrl doend# A marketplace that has not deployed a panel still has to send a working- # invitation, and the dashboard at least exists.+ # invitation, and the dashboard at least exists — but a seller who accepts+ # there gets an admin session, so the fallback says so out loud.it 'falls back to the dashboard origin when nothing else resolves' doSpree::Config[:dashboard_url] = 'https://admin.example.com'expect(described_class.call(store: store)).to eq('https://admin.example.com')end++ it 'warns when it falls back, since the session audience will be wrong' do+ Spree::Config[:dashboard_url] = 'https://admin.example.com'+ expect(Rails.logger).to receive(:warn).with(/No seller panel is configured/)++ described_class.call(store: store)+ end++ it 'does not warn when a panel origin is configured' do+ Spree::Config[:seller_panel_url] = 'https://sellers.example.com'+ expect(Rails.logger).not_to receive(:warn).with(/No seller panel is configured/)++ described_class.call(store: store)+ endend