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.

Fixed Products Performance report if product does not have `available_on` set

Damian Legawiec126751283eea
diff --git a/core/app/models/spree/report_line_items/products_performance.rb b/core/app/models/spree/report_line_items/products_performance.rb
index 6c9eaed1e3..550aaa8aa1 100644
--- a/core/app/models/spree/report_line_items/products_performance.rb
+++ b/core/app/models/spree/report_line_items/products_performance.rb
@@ -63,7 +63,9 @@ module Spree
end
def weeks_online
- (Time.current - record.available_on.in_time_zone(store.preferred_timezone)).seconds.in_weeks.to_i
+ start_date = record.available_on.presence || record.created_at
+
+ (Time.current - start_date.in_time_zone(store.preferred_timezone)).seconds.in_weeks.to_i
end
def mapped_categories
diff --git a/core/spec/models/spree/report_line_items/products_performance_spec.rb b/core/spec/models/spree/report_line_items/products_performance_spec.rb
index b6f2f2b696..f661c5096a 100644
--- a/core/spec/models/spree/report_line_items/products_performance_spec.rb
+++ b/core/spec/models/spree/report_line_items/products_performance_spec.rb
@@ -55,12 +55,24 @@ RSpec.describe Spree::ReportLineItems::ProductsPerformance do
end
describe '#weeks_online' do
- before do
- product.update(available_on: 2.weeks.ago)
+ context 'with available on' do
+ before do
+ product.update(available_on: 2.weeks.ago)
+ end
+
+ it 'returns number of weeks since product activation' do
+ expect(subject.weeks_online).to eq(2)
+ end
end
- it 'returns number of weeks since product activation' do
- expect(subject.weeks_online).to eq(2)
+ context 'without available on' do
+ before do
+ product.update(available_on: nil, created_at: 3.weeks.ago)
+ end
+
+ it 'returns number of weeks since product creation' do
+ expect(subject.weeks_online).to eq(3)
+ end
end
end