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.

Let a catalog be created with its audience and minimums

Damian Legawiecc83b0f53f83d
diff --git a/packages/dashboard/e2e/catalogs.spec.ts b/packages/dashboard/e2e/catalogs.spec.ts
index cf132a7e35..e8936d5335 100644
--- a/packages/dashboard/e2e/catalogs.spec.ts
+++ b/packages/dashboard/e2e/catalogs.spec.ts
@@ -223,6 +223,30 @@ test.describe('catalogs', () => {
})
})
+ // The order minimums stage like everything else on the page: nothing is
+ // written until Save, and Discard is the undo.
+ test('stages an order minimum and persists it on Save', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, CATALOGS_PATH(creds.store_id), CTA)
+
+ const name = `E2E Catalog Minimum ${Date.now()}`
+ await createCatalog(page, name)
+
+ await page.getByRole('button', { name: /^add$/i }).first().click()
+ await page.getByLabel(/minimum amount/i).fill('500')
+
+ // Still unsaved: a reload before Save must lose it.
+ await page.reload()
+ await expect(page.getByLabel(/minimum amount/i)).toHaveCount(0, { timeout: 15_000 })
+
+ await page.getByRole('button', { name: /^add$/i }).first().click()
+ await page.getByLabel(/minimum amount/i).fill('500')
+ await saveCatalog(page)
+
+ await page.reload()
+ await expect(page.getByLabel(/minimum amount/i)).toHaveValue('500.0', { timeout: 15_000 })
+ })
+
test('renames a catalog from the header Save', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, CATALOGS_PATH(creds.store_id), CTA)
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/catalogs_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/catalogs_controller_spec.rb
index 6fc2dbff07..551a46a617 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/catalogs_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/catalogs_controller_spec.rb
@@ -221,6 +221,31 @@ RSpec.describe Spree::Api::V3::Admin::CatalogsController, type: :controller do
end
end
+ describe 'POST #create with nested sets' do
+ let!(:company) { create(:company, store: store) }
+
+ # The dashboard's create sheet shares the detail page's form, so it sends
+ # both keys on every create — empty when the merchant filled neither.
+ it 'accepts the sets as empty arrays' do
+ post :create, params: { name: 'Fresh', assignments: [], order_minimums: [] }, as: :json
+
+ expect(response).to have_http_status(:created)
+ end
+
+ it 'stands a catalog up with its audience and minimums in one request' do
+ post :create, params: {
+ name: 'Fresh',
+ assignments: [{ assignable_type: 'company', assignable_id: company.prefixed_id }],
+ order_minimums: [{ currency: 'USD', amount: '500' }]
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ catalog = store.catalogs.find_by(name: 'Fresh')
+ expect(catalog.catalog_assignments.map(&:assignable)).to eq([company])
+ expect(catalog.order_minimums.pluck(:currency)).to eq(['USD'])
+ end
+ end
+
describe 'PATCH #update with nested sets' do
let!(:company) { create(:company, store: store) }
let!(:group) { create(:customer_group, store: store) }
diff --git a/spree/core/app/workflows/spree/catalogs/create.rb b/spree/core/app/workflows/spree/catalogs/create.rb
index 1f601b7555..f595c210bd 100644
--- a/spree/core/app/workflows/spree/catalogs/create.rb
+++ b/spree/core/app/workflows/spree/catalogs/create.rb
@@ -21,6 +21,8 @@ module Spree
ApplicationRecord.transaction do
step :save_catalog
step :apply_price_list
+ step :apply_assignments
+ step :apply_order_minimums
run_hooks :after_create
end
@@ -31,11 +33,35 @@ module Spree
# The list payload is held back rather than assigned: the catalog needs
# an id before a list can point at it.
+ # The nested sets are held back rather than assigned: they are applied
+ # inside the save's transaction, once the catalog has an id to point at.
def build_catalog
attrs = attributes.to_h.with_indifferent_access
@price_list_attributes = attrs.key?(:price_list) ? attrs[:price_list] : nil
- @catalog = store.catalogs.new(attrs.except(:price_list))
+ @assignables = attrs[:assignables]
+ @order_minimums = attrs[:order_minimums]
+
+ @catalog = store.catalogs.new(attrs.except(:price_list, :assignables, :order_minimums))
+ end
+
+ # A catalog can be created with its audience and minimums already
+ # stated, so standing one up is a single request the way the price list
+ # already is.
+ def apply_assignments
+ return if @assignables.blank?
+
+ result = Spree::Catalogs::SetAssignments.call(catalog: catalog, assignables: @assignables)
+ failure(catalog, result.error) unless result.success?
+ end
+
+ def apply_order_minimums
+ return if @order_minimums.blank?
+
+ result = Spree::Catalogs::SetOrderMinimums.call(
+ catalog: catalog, order_minimums: @order_minimums
+ )
+ failure(catalog, result.error) unless result.success?
end
def save_catalog