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.

[VEN-105] Product states - pre-Orders support - drop available_on logic - with both fields (#11605)

Konrad Gorazde46e71fdd288
diff --git a/api/app/controllers/spree/api/v2/platform/orders_controller.rb b/api/app/controllers/spree/api/v2/platform/orders_controller.rb
index 74ec151f4e..8bf794dbba 100644
--- a/api/app/controllers/spree/api/v2/platform/orders_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/orders_controller.rb
@@ -100,7 +100,7 @@ module Spree
end
def allowed_sort_attributes
- super.push(:available_on, :total, :payment_total, :item_total, :shipment_total,
+ super.push(:available_on, :make_active_at, :total, :payment_total, :item_total, :shipment_total,
:adjustment_total, :promo_total, :included_tax_total, :additional_tax_total,
:item_count, :tax_total, :completed_at)
end
diff --git a/api/app/controllers/spree/api/v2/platform/products_controller.rb b/api/app/controllers/spree/api/v2/platform/products_controller.rb
index e808b1d209..4f77285994 100644
--- a/api/app/controllers/spree/api/v2/platform/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/products_controller.rb
@@ -16,7 +16,7 @@ module Spree
end
def allowed_sort_attributes
- super << :available_on
+ super.push(:available_on, :make_active_at)
end
def sorted_collection
diff --git a/api/spec/requests/spree/api/v2/platform/products_spec.rb b/api/spec/requests/spree/api/v2/platform/products_spec.rb
index 36503b869d..90db92f32d 100644
--- a/api/spec/requests/spree/api/v2/platform/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/platform/products_spec.rb
@@ -196,6 +196,32 @@ describe 'API V2 Platform Products Spec' do
end
end
end
+
+ context 'sorting by make_active_at' do
+ before { store.products.each_with_index { |p, i| p.update(make_active_at: Time.current - i.days) } }
+
+ context 'ascending order' do
+ before { get '/api/v2/platform/products?sort=make_active_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by make_active_at' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:make_active_at).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/platform/products?sort=-make_active_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by make_active_at with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(make_active_at: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
end
context 'paginate products' do
diff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
index 4f4dc9913e..bc1137a000 100644
--- a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
@@ -26,6 +26,7 @@ describe Spree::Api::V2::Platform::ProductSerializer do
name: product.name,
description: product.description,
available_on: product.available_on,
+ make_active_at: product.make_active_at,
status: product.status,
deleted_at: product.deleted_at,
slug: product.slug,
diff --git a/core/app/helpers/spree/products_helper.rb b/core/app/helpers/spree/products_helper.rb
index de8bc8462c..3746019e1f 100644
--- a/core/app/helpers/spree/products_helper.rb
+++ b/core/app/helpers/spree/products_helper.rb
@@ -76,15 +76,20 @@ module Spree
# will return a human readable string
def available_status(product)
- return Spree.t(:discontinued) if product.discontinued?
+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
+ `Spree::ProductsHelper#available_status` method from spree/core is deprecated and will be removed.
+ Please use `Spree::Admin::ProductsHelper#available_status` from spree_backend instead.
+ DEPRECATION
+
+ return Spree.t(:archived) if product.discontinued?
return Spree.t(:deleted) if product.deleted?
if product.available?
- Spree.t(:available)
- elsif product.available_on&.future?
+ Spree.t(:active)
+ elsif product.make_active_at&.future?
Spree.t(:pending_sale)
else
- Spree.t(:no_available_date_set)
+ Spree.t(:draft)
end
end
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 014f7d63a7..0fc4ad70c2 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -284,7 +284,7 @@ module Spree
if user.try(:has_spree_role?, 'admin')
with_deleted
else
- not_deleted.not_discontinued.where("#{Product.quoted_table_name}.available_on <= ?", Time.current)
+ not_deleted.where(status: 'active')
end
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 66aa7f7fee..a0e5c11928 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -124,7 +124,7 @@ module Spree
end
validates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: true, scope: spree_base_uniqueness_scope }
- validate :discontinue_on_must_be_later_than_available_on, if: -> { available_on && discontinue_on }
+ validate :discontinue_on_must_be_later_than_make_active_at, if: -> { make_active_at && discontinue_on }
scope :for_store, ->(store) { joins(:store_products).where(StoreProduct.table_name => { store_id: store.id }) }
@@ -497,8 +497,8 @@ module Spree
removed_classifications.each &:remove_from_list
end
- def discontinue_on_must_be_later_than_available_on
- if discontinue_on < available_on
+ def discontinue_on_must_be_later_than_make_active_at
+ if discontinue_on < make_active_at
errors.add(:discontinue_on, :invalid_date_range)
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 21b887226f..65525d0252 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -14,7 +14,7 @@ module Spree
belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variants
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
- delegate :name, :name=, :description, :slug, :available_on, :shipping_category_id,
+ delegate :name, :name=, :description, :slug, :available_on, :make_active_at, :shipping_category_id,
:meta_description, :meta_keywords, :shipping_category, to: :product
# we need to have this callback before any dependent: :destroy associations
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index a526baf2a3..a235391b18 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -91,6 +91,7 @@ en:
name: Name
spree/product:
available_on: Available On
+ make_active_at: Make Active At
discontinue_on: Discontinue On
cost_currency: Cost Currency
cost_price: Cost Price
@@ -583,6 +584,7 @@ en:
auto_capture: Auto Capture
availability: availability
available_on: Available On
+ make_active_at: Make Active At
available: Available
average_order_value: Average Order Value
avs_response: AVS Response
diff --git a/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rb b/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rb
new file mode 100644
index 0000000000..01ffe9d5d4
--- /dev/null
+++ b/core/db/migrate/20220117100333_add_make_active_at_to_spree_products.rb
@@ -0,0 +1,17 @@
+class AddMakeActiveAtToSpreeProducts < ActiveRecord::Migration[5.2]
+ def change
+ add_column :spree_products, :make_active_at, :datetime
+ add_index :spree_products, :make_active_at
+
+ Spree::Product.
+ where('discontinue_on IS NULL or discontinue_on > ?', Time.current).
+ where('available_on <= ?', Time.current).
+ where(status: 'draft').
+ update_all(status: 'active', updated_at: Time.current)
+
+ Spree::Product.
+ where('discontinue_on <= ?', Time.current).
+ where.not(status: 'archived').
+ update_all(status: 'archived', updated_at: Time.current)
+ end
+end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index a099ee519d..5c32ed2f35 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -89,7 +89,7 @@ module Spree
@@product_properties_attributes = [:property_name, :value, :position]
@@product_attributes = [
- :name, :description, :available_on, :discontinue_on, :permalink, :meta_description,
+ :name, :description, :available_on, :make_active_at, :discontinue_on, :permalink, :meta_description,
:meta_keywords, :price, :sku, :deleted_at, :prototype_id,
:option_values_hash, :weight, :height, :width, :depth,
:shipping_category_id, :tax_category_id,
diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rb
index 5fad238945..aeb3ecd5e2 100644
--- a/core/lib/spree/testing_support/factories/product_factory.rb
+++ b/core/lib/spree/testing_support/factories/product_factory.rb
@@ -6,6 +6,7 @@ FactoryBot.define do
cost_price { 17.00 }
sku { generate(:sku) }
available_on { 1.year.ago }
+ make_active_at { 1.year.ago }
deleted_at { nil }
shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) }
status { 'active' }
diff --git a/core/lib/tasks/core.rake b/core/lib/tasks/core.rake
index 9e221d98ee..5c40dd5ccf 100644
--- a/core/lib/tasks/core.rake
+++ b/core/lib/tasks/core.rake
@@ -187,9 +187,9 @@ use rake db:load_file[/absolute/path/to/sample/filename.rb]}
end
namespace :core do
- desc 'Set "active" status on draft products where available_on is in the past'
+ desc 'Set "active" status on draft products where make_active_at is in the past'
task activate_products: :environment do |_t, _args|
- Spree::Product.where('available_on <= ?', Time.current).where(status: 'draft').update_all(status: 'active', updated_at: Time.current)
+ Spree::Product.where('make_active_at <= ?', Time.current).where(status: 'draft').update_all(status: 'active', updated_at: Time.current)
end
desc 'Set "archived" status on active products where discontinue_on is in the past'
diff --git a/core/spec/finders/spree/products/find_spec.rb b/core/spec/finders/spree/products/find_spec.rb
index a8902b8cd2..38babe9996 100644
--- a/core/spec/finders/spree/products/find_spec.rb
+++ b/core/spec/finders/spree/products/find_spec.rb
@@ -507,7 +507,7 @@ module Spree
params: params
).execute.map(&:id)
- expect(product_ids).to eq Spree::Product.available.order(available_on: :desc).map(&:id)
+ expect(product_ids).to eq Spree::Product.available.order(make_active_at: :desc).map(&:id)
end
it 'returns products in price-high-to-low order' do
diff --git a/core/spec/helpers/products_helper_spec.rb b/core/spec/helpers/products_helper_spec.rb
index 4dcefbb802..c35697a31a 100644
--- a/core/spec/helpers/products_helper_spec.rb
+++ b/core/spec/helpers/products_helper_spec.rb
@@ -294,7 +294,7 @@ module Spree
context 'product is available' do
it 'has available status' do
- expect(status).to eq(Spree.t(:available))
+ expect(status).to eq(Spree.t(:active))
end
end
@@ -314,20 +314,30 @@ module Spree
end
it 'has discontinued status' do
- expect(status).to eq(Spree.t(:discontinued))
+ expect(status).to eq(Spree.t(:archived))
end
end
context 'product will be available soon' do
before do
product.status = 'draft'
- product.available_on = 1.month.from_now
+ product.make_active_at = 1.month.from_now
end
it 'has pending sale status' do
expect(status).to eq(Spree.t(:pending_sale))
end
end
+
+ context 'draft product' do
+ before do
+ product.status = 'draft'
+ end
+
+ it 'has draft status' do
+ expect(status).to eq(Spree.t(:draft))
+ end
+ end
end
end
end
diff --git a/core/spec/lib/tasks/core_spec.rb b/core/spec/lib/tasks/core_spec.rb
index d6f972f91d..54c5a95da8 100644
--- a/core/spec/lib/tasks/core_spec.rb
+++ b/core/spec/lib/tasks/core_spec.rb
@@ -9,26 +9,26 @@ describe 'core:activate_products' do
it { expect(subject.prerequisites).to include('environment') }
end
- it 'draft, available_on in the past -> active' do
- product.update(status: 'draft', available_on: 1.day.ago)
+ it 'draft, make_active_at in the past -> active' do
+ product.update(status: 'draft', make_active_at: 1.day.ago)
subject.invoke
expect(product.reload.status).to eq('active')
end
- it 'draft, available_on in the future -> draft' do
- product.update(status: 'draft', available_on: 1.day.from_now)
+ it 'draft, make_active_at in the future -> draft' do
+ product.update(status: 'draft', make_active_at: 1.day.from_now)
subject.invoke
expect(product.reload.status).to eq('draft')
end
- it 'archived, available_on in the past -> archived' do
- product.update(status: 'archived', available_on: 1.day.ago)
+ it 'archived, make_active_at in the past -> archived' do
+ product.update(status: 'archived', make_active_at: 1.day.ago)
subject.invoke
expect(product.reload.status).to eq('archived')
end
- it 'archived, available_on in the future -> archived' do
- product.update(status: 'archived', available_on: 1.day.from_now)
+ it 'archived, make_active_at in the future -> archived' do
+ product.update(status: 'archived', make_active_at: 1.day.from_now)
subject.invoke
expect(product.reload.status).to eq('archived')
end
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 80e1ebb909..0ecd2bacca 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -18,7 +18,7 @@ describe 'Product scopes', type: :model do
end
context 'when available' do
- let!(:product_2) { create(:product, available_on: Time.current - 1.day,stores: [store]) }
+ let!(:product_2) { create(:product, status: 'active', stores: [store]) }
it { expect(Spree::Product.available).to include(product_2) }
end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 0f0f2eddde..3abd7317d3 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -275,10 +275,10 @@ describe Spree::Product, type: :model do
end
end
- describe '#discontinue_on_must_be_later_than_available_on' do
- before { product.available_on = Date.today }
+ describe '#discontinue_on_must_be_later_than_make_active_at' do
+ before { product.make_active_at = Date.today }
- context 'available_on is a date earlier than discontinue_on' do
+ context 'make_active_at is a date earlier than discontinue_on' do
before { product.discontinue_on = 5.days.from_now }
it 'is valid' do
@@ -286,7 +286,7 @@ describe Spree::Product, type: :model do
end
end
- context 'available_on is a date earlier than discontinue_on' do
+ context 'make_active_at is a date earlier than discontinue_on' do
before { product.discontinue_on = 5.days.ago }
context 'is not valid' do
@@ -297,10 +297,10 @@ describe Spree::Product, type: :model do
end
end
- context 'available_on and discontinue_on are nil' do
+ context 'make_active_at and discontinue_on are nil' do
before do
product.discontinue_on = nil
- product.available_on = nil
+ product.make_active_at = nil
end
it 'is valid' do
diff --git a/sample/db/samples/products.rb b/sample/db/samples/products.rb
index bd852698ea..7beac2b2fd 100644
--- a/sample/db/samples/products.rb
+++ b/sample/db/samples/products.rb
@@ -23,6 +23,7 @@ PRODUCTS.each do |(parent_name, taxon_name, product_name)|
product.price = rand(10...100) + 0.99
product.description = FFaker::Lorem.paragraph
product.available_on = Time.zone.now
+ product.make_active_at = Time.zone.now
product.status = 'active'
if parent_name == 'Women' and %w[Dresses Skirts].include?(taxon_name)
product.option_types = [color, length, size]