spree_core 6.0.3 → 6.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/jobs/spree/payments/handle_webhook_job.rb +12 -1
- data/app/models/concerns/spree/has_custom_fields.rb +4 -1
- data/app/models/concerns/spree/personal_data_matching.rb +5 -1
- data/app/models/concerns/spree/product_scopes.rb +2 -2
- data/app/models/concerns/spree/publishable.rb +26 -3
- data/app/models/spree/authentication/strategies/base_strategy.rb +10 -1
- data/app/models/spree/cart.rb +7 -2
- data/app/models/spree/invitation.rb +6 -2
- data/app/models/spree/media.rb +25 -1
- data/app/models/spree/order.rb +5 -1
- data/app/models/spree/price.rb +3 -0
- data/app/models/spree/product.rb +6 -9
- data/app/models/spree/product_category.rb +7 -1
- data/app/models/spree/product_collection.rb +8 -1
- data/app/models/spree/seller.rb +93 -10
- data/app/models/spree/seller_requirements/complete_profile.rb +26 -7
- data/app/models/spree/stock_level.rb +30 -1
- data/app/models/spree/stock_movement.rb +1 -0
- data/app/services/spree/carts/destroy.rb +22 -6
- data/app/services/spree/collections/regenerate_products.rb +37 -15
- data/app/services/spree/companies/add_member.rb +9 -4
- data/app/services/spree/diagnostics/error_log.rb +139 -0
- data/app/services/spree/diagnostics/section.rb +72 -0
- data/app/services/spree/diagnostics/sections.rb +121 -0
- data/app/services/spree/diagnostics/stack.rb +73 -0
- data/app/services/spree/diagnostics.rb +132 -0
- data/app/services/spree/newsletter/subscribe.rb +7 -3
- data/app/services/spree/orders/remove_line_item.rb +16 -5
- data/app/services/spree/product_memberships.rb +21 -1
- data/app/services/spree/product_types/apply_to_products.rb +12 -2
- data/app/services/spree/products/auto_match_collections.rb +20 -4
- data/app/services/spree/stock_reservations/reserve.rb +9 -6
- data/app/services/spree/store_credits/apply.rb +9 -1
- data/app/services/spree/update_check.rb +7 -31
- data/app/tools/spree/agent_tools/describe_resource.rb +3 -0
- data/app/tools/spree/agent_tools/get_resource.rb +1 -1
- data/app/tools/spree/agent_tools/resource_write.rb +1 -1
- data/app/tools/spree/agent_tools/search_resources.rb +42 -10
- data/app/workflows/spree/carts/add_item.rb +51 -0
- data/app/workflows/spree/carts/complete.rb +14 -2
- data/app/workflows/spree/payments/handle_webhook.rb +7 -3
- data/app/workflows/spree/sellers/approve.rb +6 -0
- data/app/workflows/spree/sellers/create.rb +5 -2
- data/app/workflows/spree/stock_levels/bulk_upsert.rb +19 -7
- data/config/locales/be.yml +1 -1
- data/config/locales/en.yml +8 -0
- data/config/locales/fr.yml +1 -1
- data/config/locales/lt.yml +1 -1
- data/config/locales/ru.yml +1 -1
- data/config/locales/uk.yml +1 -1
- data/db/migrate/20240303174340_fix_spree_stock_item_unique_index.rb +14 -10
- data/db/migrate/20260814000001_rename_stock_items_to_stock_levels.rb +13 -7
- data/lib/spree/agent_tool.rb +3 -3
- data/lib/spree/agent_tools/operation_tool.rb +1 -1
- data/lib/spree/agent_tools/record_summary.rb +8 -2
- data/lib/spree/agent_tools/resource_map.rb +82 -7
- data/lib/spree/core/engine.rb +10 -0
- data/lib/spree/core/version.rb +1 -1
- data/lib/spree/core.rb +11 -0
- data/lib/spree/reporting/default_vocabulary.rb +25 -5
- data/lib/tasks/diagnostics.rake +11 -0
- metadata +10 -4
|
@@ -11,12 +11,23 @@ module Spree
|
|
|
11
11
|
# @param options [Hash] accepted for signature compatibility and ignored
|
|
12
12
|
# @return [Spree::ServiceModule::Result] value is the removed line item
|
|
13
13
|
def call(order:, line_item:, options: {})
|
|
14
|
-
result =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
)
|
|
14
|
+
result = nil
|
|
15
|
+
# A savepoint: a caller's transaction (the API's order lock) joins the
|
|
16
|
+
# workflow's own, so a late rejection would otherwise commit the removal.
|
|
17
|
+
ActiveRecord::Base.transaction(requires_new: true) do
|
|
18
|
+
result = Spree.order_upsert_items_workflow.call(
|
|
19
|
+
order: order,
|
|
20
|
+
items: [{ variant_id: line_item.variant_id, quantity: 0 }]
|
|
21
|
+
)
|
|
22
|
+
raise ActiveRecord::Rollback if result.failure?
|
|
23
|
+
end
|
|
24
|
+
return failure(line_item, result.error) if result.failure?
|
|
18
25
|
|
|
19
|
-
|
|
26
|
+
# The order workflow leaves totals to its caller.
|
|
27
|
+
Spree::Orders::Recalculate.call(order: order)
|
|
28
|
+
order.update_statuses!
|
|
29
|
+
|
|
30
|
+
success(line_item)
|
|
20
31
|
end
|
|
21
32
|
end
|
|
22
33
|
end
|
|
@@ -9,6 +9,7 @@ module Spree
|
|
|
9
9
|
def add_memberships(groups, products)
|
|
10
10
|
group_ids = groups.pluck(:id)
|
|
11
11
|
product_ids = products.pluck(:id)
|
|
12
|
+
existing = membership_class.where(group_key => group_ids, product_id: product_ids).ids
|
|
12
13
|
# Every group's current size in one grouped count, rather than a probe
|
|
13
14
|
# per group for the position to append at.
|
|
14
15
|
sizes = membership_class.where(group_key => group_ids).group(group_key).count
|
|
@@ -24,15 +25,23 @@ module Spree
|
|
|
24
25
|
end
|
|
25
26
|
membership_class.insert_all(rows)
|
|
26
27
|
|
|
28
|
+
created = membership_class.where(group_key => group_ids, product_id: product_ids).where.not(id: existing)
|
|
29
|
+
|
|
27
30
|
refresh_after_membership_change(group_ids, products)
|
|
31
|
+
publish_membership_events(created, 'created')
|
|
28
32
|
end
|
|
29
33
|
|
|
30
34
|
# Removes the products, then re-packs the remaining positions from 1.
|
|
31
35
|
def remove_memberships(groups, products)
|
|
32
36
|
group_ids = groups.pluck(:id)
|
|
37
|
+
product_ids = products.pluck(:id)
|
|
38
|
+
deleted = []
|
|
33
39
|
|
|
34
40
|
ApplicationRecord.transaction do
|
|
35
|
-
|
|
41
|
+
# Ordered so two callers queue for the same rows in the same sequence rather than deadlocking on each other.
|
|
42
|
+
deleted = membership_class.where(group_key => group_ids, product_id: product_ids).order(:id).lock.to_a
|
|
43
|
+
|
|
44
|
+
membership_class.where(group_key => group_ids, product_id: product_ids).delete_all
|
|
36
45
|
|
|
37
46
|
# What survived the delete, for every group in one read.
|
|
38
47
|
remaining = membership_class.where(group_key => group_ids).order(:position).pluck(group_key, :product_id).
|
|
@@ -50,6 +59,17 @@ module Spree
|
|
|
50
59
|
end
|
|
51
60
|
|
|
52
61
|
refresh_after_membership_change(group_ids, products)
|
|
62
|
+
publish_membership_events(deleted, 'deleted')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def publish_membership_events(memberships, action)
|
|
66
|
+
memberships = memberships.to_a
|
|
67
|
+
return if memberships.empty?
|
|
68
|
+
|
|
69
|
+
ActiveRecord::Associations::Preloader.new(records: memberships, associations: { product: :store }).call
|
|
70
|
+
|
|
71
|
+
name = "#{membership_class.model_name.element}.#{action}"
|
|
72
|
+
memberships.each { |membership| membership.publish_event(name) }
|
|
53
73
|
end
|
|
54
74
|
|
|
55
75
|
# Bulk writes skip the join model's callbacks, so counters, caches and the
|
|
@@ -136,8 +136,9 @@ module Spree
|
|
|
136
136
|
scoped_ids = store.products.where(id: product_ids).ids
|
|
137
137
|
return [] if scoped_ids.empty?
|
|
138
138
|
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
existing_rows = Spree::ProductCategory.where(product_id: scoped_ids, category_id: category_ids).
|
|
140
|
+
pluck(:id, :product_id, :category_id)
|
|
141
|
+
existing = existing_rows.to_set { |_id, product_id, category_id| [product_id, category_id] }
|
|
141
142
|
|
|
142
143
|
# acts_as_list scopes position per category here (unlike option types,
|
|
143
144
|
# which scope per product), so each category continues its own sequence.
|
|
@@ -160,8 +161,17 @@ module Spree
|
|
|
160
161
|
return [] if rows.empty?
|
|
161
162
|
|
|
162
163
|
Spree::ProductCategory.insert_all(rows)
|
|
164
|
+
publish_categorized_events(scoped_ids, category_ids, existing_rows.map(&:first))
|
|
165
|
+
|
|
163
166
|
rows.pluck(:product_id).uniq
|
|
164
167
|
end
|
|
168
|
+
|
|
169
|
+
def publish_categorized_events(product_ids, category_ids, existing_ids)
|
|
170
|
+
created = Spree::ProductCategory.where(product_id: product_ids, category_id: category_ids).
|
|
171
|
+
where.not(id: existing_ids)
|
|
172
|
+
|
|
173
|
+
created.each { |membership| membership.publish_event('product_category.created') }
|
|
174
|
+
end
|
|
165
175
|
end
|
|
166
176
|
end
|
|
167
177
|
end
|
|
@@ -9,22 +9,28 @@ module Spree
|
|
|
9
9
|
# @param product [Spree::Product]
|
|
10
10
|
# @return [Spree::ServiceModule::Base::Result]
|
|
11
11
|
def call(product:)
|
|
12
|
-
return
|
|
12
|
+
return if product.blank?
|
|
13
13
|
|
|
14
14
|
collections_to_remove = []
|
|
15
15
|
collections_to_add = []
|
|
16
16
|
|
|
17
17
|
# existing memberships that no longer match (tags/prices changed)
|
|
18
|
-
product.collections.automatic.includes(:rules).
|
|
18
|
+
product.collections.automatic.includes(:rules).find_each do |collection|
|
|
19
19
|
collections_to_remove << collection unless collection.products_matching_rules.ids.include?(product.id)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
# Held before the delete: the rows are gone by the time their deleted
|
|
23
|
+
# events can be published.
|
|
24
|
+
removed_rows = []
|
|
25
|
+
|
|
22
26
|
if collections_to_remove.any?
|
|
23
|
-
Spree::ProductCollection.where(collection: collections_to_remove, product: product)
|
|
27
|
+
scope = Spree::ProductCollection.where(collection: collections_to_remove, product: product)
|
|
28
|
+
removed_rows = scope.to_a
|
|
29
|
+
scope.delete_all
|
|
24
30
|
end
|
|
25
31
|
|
|
26
32
|
# automatic collections in the product's store that it now matches
|
|
27
|
-
Spree::Collection.automatic.where(store_id: product.store_id).includes(:rules, :products).
|
|
33
|
+
Spree::Collection.automatic.where(store_id: product.store_id).includes(:rules, :products).find_each do |collection|
|
|
28
34
|
collections_to_add << collection if collection.products.exclude?(product) && collection.products_matching_rules.ids.include?(product.id)
|
|
29
35
|
end
|
|
30
36
|
|
|
@@ -56,11 +62,21 @@ module Spree
|
|
|
56
62
|
Spree::Product.reset_counters(product.id, :product_collections)
|
|
57
63
|
end
|
|
58
64
|
|
|
65
|
+
publish_membership_events(product, removed_rows, collections_to_add)
|
|
66
|
+
|
|
59
67
|
success(product)
|
|
60
68
|
end
|
|
61
69
|
|
|
62
70
|
private
|
|
63
71
|
|
|
72
|
+
def publish_membership_events(product, removed_rows, added_collections)
|
|
73
|
+
removed_rows.each { |row| row.publish_event('product_collection.deleted') }
|
|
74
|
+
return if added_collections.empty?
|
|
75
|
+
|
|
76
|
+
Spree::ProductCollection.where(collection_id: added_collections.map(&:id), product_id: product.id).
|
|
77
|
+
find_each { |row| row.publish_event('product_collection.created') }
|
|
78
|
+
end
|
|
79
|
+
|
|
64
80
|
def product_collection_attributes(collection, product, position)
|
|
65
81
|
{
|
|
66
82
|
collection_id: collection.id,
|
|
@@ -139,16 +139,19 @@ module Spree
|
|
|
139
139
|
)
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
+
# Handoff to a draft clears the cart reference. A SQL inequality alone
|
|
143
|
+
# drops those holds instead of counting them against competing checkouts.
|
|
142
144
|
def held_by_others(stock_level_ids, owner)
|
|
143
145
|
return {} if stock_level_ids.empty?
|
|
144
146
|
|
|
145
147
|
owner_column = owner.is_a?(Spree::Cart) ? :cart_id : :order_id
|
|
146
|
-
Spree::StockReservation
|
|
147
|
-
|
|
148
|
-
.
|
|
149
|
-
|
|
150
|
-
.
|
|
151
|
-
|
|
148
|
+
owner_reference = Spree::StockReservation.arel_table[owner_column]
|
|
149
|
+
Spree::StockReservation.
|
|
150
|
+
active.
|
|
151
|
+
where(stock_level_id: stock_level_ids).
|
|
152
|
+
where(owner_reference.eq(nil).or(owner_reference.not_eq(owner.id))).
|
|
153
|
+
group(:stock_level_id).
|
|
154
|
+
sum(:quantity)
|
|
152
155
|
end
|
|
153
156
|
|
|
154
157
|
# One SELECT for all (stock_level_id, line_item_id) pairs we need to
|
|
@@ -57,8 +57,16 @@ module Spree
|
|
|
57
57
|
apply_store_credits(remaining_total, except: payments.map(&:source_id)) if remaining_total.positive?
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
# Allocates credit through an active payment method owned by the order store.
|
|
61
|
+
# No method is required when the outstanding amount is zero or negative.
|
|
62
|
+
#
|
|
63
|
+
# @param remaining_total [Numeric] maximum outstanding amount to allocate
|
|
64
|
+
# @param except [Array<Integer>] credit IDs already applied to this order
|
|
65
|
+
# @return [void]
|
|
60
66
|
def apply_store_credits(remaining_total, except: [])
|
|
61
|
-
|
|
67
|
+
return unless remaining_total.positive?
|
|
68
|
+
|
|
69
|
+
payment_method = @order.store.payment_methods.store_credit.available.first
|
|
62
70
|
raise 'Store credit payment method could not be found' unless payment_method
|
|
63
71
|
|
|
64
72
|
spendable_store_credits.where.not(id: except).oldest_first.each do |credit|
|
|
@@ -21,17 +21,6 @@ module Spree
|
|
|
21
21
|
# The read timeout applies to each read, not the whole response, so a
|
|
22
22
|
# slowly trickling answer needs an overall deadline as well.
|
|
23
23
|
TOTAL_TIMEOUT = 2
|
|
24
|
-
# Order-of-magnitude buckets: enough to tell a hobby install from a large
|
|
25
|
-
# merchant, never an exact figure.
|
|
26
|
-
BUCKETS = {
|
|
27
|
-
0..0 => '0',
|
|
28
|
-
1..9 => '1-9',
|
|
29
|
-
10..99 => '10-99',
|
|
30
|
-
100..999 => '100-999',
|
|
31
|
-
1_000..9_999 => '1k-9k',
|
|
32
|
-
10_000..99_999 => '10k-99k'
|
|
33
|
-
}.freeze
|
|
34
|
-
LARGEST_BUCKET = '100k+'.freeze
|
|
35
24
|
|
|
36
25
|
class << self
|
|
37
26
|
# Always false in the test environment and on CI servers (which set
|
|
@@ -77,13 +66,13 @@ module Spree
|
|
|
77
66
|
install_id: Spree.install_id,
|
|
78
67
|
ruby: RUBY_VERSION,
|
|
79
68
|
rails: Rails.version,
|
|
80
|
-
database:
|
|
81
|
-
gems:
|
|
82
|
-
dashboard: dashboard_served?,
|
|
83
|
-
stores: bucket(Spree::Store.count),
|
|
84
|
-
products: bucket(Spree::Product.count),
|
|
85
|
-
orders: bucket(Spree::Order.complete.count),
|
|
86
|
-
sellers: seller_bucket
|
|
69
|
+
database: Diagnostics::Stack.database,
|
|
70
|
+
gems: Diagnostics::Stack.spree_gems.keys.join(','),
|
|
71
|
+
dashboard: Diagnostics::Stack.dashboard_served?,
|
|
72
|
+
stores: Diagnostics::Stack.bucket(Spree::Store.count),
|
|
73
|
+
products: Diagnostics::Stack.bucket(Spree::Product.count),
|
|
74
|
+
orders: Diagnostics::Stack.bucket(Spree::Order.complete.count),
|
|
75
|
+
sellers: Diagnostics::Stack.seller_bucket
|
|
87
76
|
}.compact
|
|
88
77
|
end
|
|
89
78
|
|
|
@@ -142,19 +131,6 @@ module Spree
|
|
|
142
131
|
nil
|
|
143
132
|
end
|
|
144
133
|
|
|
145
|
-
def bucket(count)
|
|
146
|
-
BUCKETS.find { |range, _| range.cover?(count) }&.last || LARGEST_BUCKET
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def seller_bucket
|
|
150
|
-
count = Spree::Seller.count
|
|
151
|
-
bucket(count) if count.positive?
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
def dashboard_served?
|
|
155
|
-
(defined?(Spree::Dashboard) && Spree::Dashboard.dist_path.present?) || Spree::Config.dashboard_url.present?
|
|
156
|
-
end
|
|
157
|
-
|
|
158
134
|
def env_flag?(name)
|
|
159
135
|
ActiveModel::Type::Boolean.new.cast(ENV[name].presence) == true
|
|
160
136
|
end
|
|
@@ -101,6 +101,9 @@ module Spree
|
|
|
101
101
|
# product's variants when written — needs that record's id as
|
|
102
102
|
# parent_id on the calls whose route runs under it.
|
|
103
103
|
parent: %i[index create update].lazy.filter_map { |action| entry.parent_key(action) }.first,
|
|
104
|
+
# The records a search can list this resource under with parent_id
|
|
105
|
+
# — products under a category, collection or catalog.
|
|
106
|
+
listed_under: entry.index_parents.keys.sort.presence,
|
|
104
107
|
filterable_fields: entry.filterable_fields,
|
|
105
108
|
# What a generic write accepts, so the model learns the schema here
|
|
106
109
|
# rather than by sending an invalid write and reading the refusal —
|
|
@@ -20,7 +20,7 @@ module Spree
|
|
|
20
20
|
entry = ResourceMap.find(resource)
|
|
21
21
|
return unknown_resource(resource) if entry.nil?
|
|
22
22
|
return { error: "You do not have permission to read #{entry.key}." } unless
|
|
23
|
-
|
|
23
|
+
entry.readable_by?(context)
|
|
24
24
|
|
|
25
25
|
return { error: "#{entry.key} cannot be fetched one at a time." } if entry.api_path(:show).blank?
|
|
26
26
|
|
|
@@ -46,7 +46,7 @@ module Spree
|
|
|
46
46
|
# own words. This check only avoids offering a tool that cannot work
|
|
47
47
|
# — a resource the caller cannot even read.
|
|
48
48
|
return [nil, { error: "You do not have permission to change #{entry.key}." }] unless
|
|
49
|
-
|
|
49
|
+
entry.readable_by?(context)
|
|
50
50
|
|
|
51
51
|
[entry, nil]
|
|
52
52
|
end
|
|
@@ -9,7 +9,8 @@ module Spree
|
|
|
9
9
|
# reported it as a fact.
|
|
10
10
|
class SearchResources < Spree::AgentTool
|
|
11
11
|
DEFAULT_LIMIT = 10
|
|
12
|
-
|
|
12
|
+
# The Admin API's own page ceiling; asking for more returns this many.
|
|
13
|
+
MAX_LIMIT = 100
|
|
13
14
|
|
|
14
15
|
tool_name 'search_resources'
|
|
15
16
|
description 'Search the store. Filters are Ransack predicates ' \
|
|
@@ -24,15 +25,18 @@ module Spree
|
|
|
24
25
|
|
|
25
26
|
param :resource, description: 'Resource key — call describe_resource for the list', required: true
|
|
26
27
|
param :filters, type: :object, description: 'Ransack filter hash, e.g. {"name_cont": "shirt"}'
|
|
27
|
-
param :sort, description: 'Sort, e.g. "created_at
|
|
28
|
-
param :limit, type: :integer,
|
|
28
|
+
param :sort, description: 'Sort field, prefixed with - for descending, e.g. "-created_at" for newest first'
|
|
29
|
+
param :limit, type: :integer,
|
|
30
|
+
description: "Records per page (default #{DEFAULT_LIMIT}, at most #{MAX_LIMIT}). " \
|
|
31
|
+
'When total exceeds count, pass next_page as page to fetch the rest.'
|
|
32
|
+
param :page, type: :integer, description: 'Page number, starting at 1 (default 1)'
|
|
29
33
|
param :parent_id, description: PARENT_ID_DESCRIPTION
|
|
30
34
|
|
|
31
|
-
def call(resource:, filters: nil, sort: nil, limit: nil, parent_id: nil)
|
|
35
|
+
def call(resource:, filters: nil, sort: nil, limit: nil, page: nil, parent_id: nil)
|
|
32
36
|
entry = ResourceMap.find(resource)
|
|
33
37
|
return unknown_resource(resource) if entry.nil?
|
|
34
38
|
return not_searchable(entry) if entry.api_path(:index).blank?
|
|
35
|
-
return forbidden(entry) unless
|
|
39
|
+
return forbidden(entry) unless entry.readable_by?(context)
|
|
36
40
|
|
|
37
41
|
applied = sanitize_filters(filters)
|
|
38
42
|
# Ransack drops a filter it does not recognise and returns the whole
|
|
@@ -44,9 +48,10 @@ module Spree
|
|
|
44
48
|
return invalid_filters(entry, unrecognised) if unrecognised.any?
|
|
45
49
|
|
|
46
50
|
path = entry.path_for(:index, parent_id: parent_id)
|
|
51
|
+
return wrong_parent(entry, parent_id) if path.nil? && parent_id.present? && !entry.nested?(:index)
|
|
47
52
|
return not_searchable(entry) if path.nil?
|
|
48
53
|
|
|
49
|
-
response = dispatch.call(method: :get, path: path, params: query_for(applied, sort, limit))
|
|
54
|
+
response = dispatch.call(method: :get, path: path, params: query_for(applied, sort, limit, page))
|
|
50
55
|
return { error: response.error_message } unless response.success?
|
|
51
56
|
|
|
52
57
|
rows = Array(response.data)
|
|
@@ -59,8 +64,13 @@ module Spree
|
|
|
59
64
|
# for a store holding hundreds.
|
|
60
65
|
total: meta['count'] || rows.size,
|
|
61
66
|
count: rows.size,
|
|
67
|
+
page: meta['page'],
|
|
68
|
+
pages: meta['pages'],
|
|
69
|
+
# Named outright, because a model told only "25 of 88" reports the
|
|
70
|
+
# 25 as the answer.
|
|
71
|
+
next_page: meta['next'],
|
|
62
72
|
records: rows.map { |row| RecordSummary.from_payload(entry: entry, payload: row) }
|
|
63
|
-
}
|
|
73
|
+
}.compact
|
|
64
74
|
end
|
|
65
75
|
|
|
66
76
|
def summary(arguments)
|
|
@@ -81,13 +91,24 @@ module Spree
|
|
|
81
91
|
# Ransack filters travel under `q`, the way every other client sends
|
|
82
92
|
# them — so an unknown one is refused by the API's own filter
|
|
83
93
|
# validation rather than silently widening the search here.
|
|
84
|
-
def query_for(filters, sort, limit)
|
|
85
|
-
params = { limit: capped_limit(limit) }
|
|
94
|
+
def query_for(filters, sort, limit, page)
|
|
95
|
+
params = { limit: capped_limit(limit), page: [page.to_i, 1].max }
|
|
86
96
|
params[:q] = filters if filters.any?
|
|
87
|
-
params[:sort] = sort if sort.present?
|
|
97
|
+
params[:sort] = api_sort(sort) if sort.present?
|
|
88
98
|
params
|
|
89
99
|
end
|
|
90
100
|
|
|
101
|
+
# The API reads `-field` for descending. Models also write Ransack's
|
|
102
|
+
# `field desc`, which reached the API as `field desc asc` and sorted
|
|
103
|
+
# ascending, so a request for the newest records paged through the
|
|
104
|
+
# oldest.
|
|
105
|
+
def api_sort(sort)
|
|
106
|
+
sort.to_s.split(',').map do |part|
|
|
107
|
+
field, direction = part.strip.split(/\s+/, 2)
|
|
108
|
+
direction.to_s.casecmp?('desc') ? "-#{field.delete_prefix('-')}" : field
|
|
109
|
+
end.join(',')
|
|
110
|
+
end
|
|
111
|
+
|
|
91
112
|
# Empty values match everything, so they are dropped — but `false` is a
|
|
92
113
|
# real filter value (`{"out_of_stock": false}`), which `compact_blank`
|
|
93
114
|
# would have thrown away.
|
|
@@ -141,6 +162,17 @@ module Spree
|
|
|
141
162
|
{ error: "You do not have permission to read #{entry.key}." }
|
|
142
163
|
end
|
|
143
164
|
|
|
165
|
+
# A parent the resource is not listed under is refused rather than
|
|
166
|
+
# dropped: dropped, the search listed the whole store and the model
|
|
167
|
+
# reported every claim as the one order's.
|
|
168
|
+
def wrong_parent(entry, parent_id)
|
|
169
|
+
parents = entry.index_parents.keys.sort
|
|
170
|
+
return { error: "#{entry.key} are not listed under another record. Search without parent_id and use filters." } if parents.empty?
|
|
171
|
+
|
|
172
|
+
{ error: "#{entry.key} cannot be listed under #{parent_id}. Pass the id of one of: #{parents.join(', ')}.",
|
|
173
|
+
parents: parents }
|
|
174
|
+
end
|
|
175
|
+
|
|
144
176
|
# A resource reached only under a parent, or one whose controller
|
|
145
177
|
# resolves its scope per request. Named rather than guessed at, because
|
|
146
178
|
# a tool cannot invent the parent id such a path needs.
|
|
@@ -21,6 +21,14 @@ module Spree
|
|
|
21
21
|
end
|
|
22
22
|
super(cart: cart, variant: variant, quantity: quantity, metadata: metadata, options: options, price: price)
|
|
23
23
|
|
|
24
|
+
# First, before anything is priced or reserved: a cart whose
|
|
25
|
+
# completion is in flight (or retained behind a payment-covered draft
|
|
26
|
+
# or a divided checkout) must not change under the purchase it
|
|
27
|
+
# supplied. The Store API fences this under its cart lock; a direct
|
|
28
|
+
# call to this workflow has to answer the same way rather than fall
|
|
29
|
+
# through to Reserve and report a misleading stock shortage.
|
|
30
|
+
step :guard_completion_claim
|
|
31
|
+
|
|
24
32
|
step :parse_manual_price unless price.nil?
|
|
25
33
|
|
|
26
34
|
# Core behaviour rather than a hook: an agreement's quantity terms are
|
|
@@ -43,6 +51,10 @@ module Spree
|
|
|
43
51
|
external_step :resolve_price if price.nil?
|
|
44
52
|
|
|
45
53
|
ApplicationRecord.transaction do
|
|
54
|
+
# Again under the row lock: the external steps above can take a
|
|
55
|
+
# while, and a completion may have claimed the cart since the
|
|
56
|
+
# first check read it.
|
|
57
|
+
step :guard_completion_claim_in_lock
|
|
46
58
|
step :add_to_line_item
|
|
47
59
|
step :handle_stock_reservations if cart.in_checkout?
|
|
48
60
|
step :recalculate, with: -> { Spree.cart_recalculate_workflow }
|
|
@@ -54,6 +66,45 @@ module Spree
|
|
|
54
66
|
|
|
55
67
|
private
|
|
56
68
|
|
|
69
|
+
# Draft orders (the admin path) carry no completion claim — only a cart
|
|
70
|
+
# does. Read from a fresh copy of the row, not the caller's instance:
|
|
71
|
+
# that one may predate the claim, and may carry in-memory changes the
|
|
72
|
+
# workflow must keep. The error matches the Store API's lock fence so a
|
|
73
|
+
# caller can treat both entry points alike.
|
|
74
|
+
def guard_completion_claim
|
|
75
|
+
return unless guarded_cart?
|
|
76
|
+
return unless completion_holds?(Spree::Cart.find(cart.id))
|
|
77
|
+
|
|
78
|
+
reject_claimed_cart
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Again under the row lock: the external steps can take a while, and a
|
|
82
|
+
# completion may have claimed — or finished — the cart since the first
|
|
83
|
+
# check. A no-op wait on the Store API path, which already holds this
|
|
84
|
+
# lock. +lock.find+ rather than +lock!+, which refuses to discard the
|
|
85
|
+
# caller's in-memory changes (pricing warnings).
|
|
86
|
+
def guard_completion_claim_in_lock
|
|
87
|
+
return unless guarded_cart?
|
|
88
|
+
return unless completion_holds?(Spree::Cart.lock.find(cart.id))
|
|
89
|
+
|
|
90
|
+
reject_claimed_cart
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def guarded_cart?
|
|
94
|
+
cart.is_a?(Spree::Cart) && cart.persisted?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Finalize clears the claim as it stamps +completed_at+, so a finished
|
|
98
|
+
# cart answers false to +completion_claimed?+ and has to be refused on
|
|
99
|
+
# its own.
|
|
100
|
+
def completion_holds?(fresh_cart)
|
|
101
|
+
fresh_cart.completed_at.present? || fresh_cart.completion_claimed?
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def reject_claimed_cart
|
|
105
|
+
failure(cart, code: 'completion_in_progress', message: I18n.t('spree.cart_completion_in_progress'))
|
|
106
|
+
end
|
|
107
|
+
|
|
57
108
|
# finite? is not redundant: BigDecimal parses "NaN" and "Infinity" and
|
|
58
109
|
# neither is negative. reject! rather than failure(cart, message), which
|
|
59
110
|
# would drop the message.
|
|
@@ -110,7 +110,7 @@ module Spree
|
|
|
110
110
|
end
|
|
111
111
|
|
|
112
112
|
finalize!(cart, result)
|
|
113
|
-
halt!(result)
|
|
113
|
+
halt!(order_group || result)
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
# The split already ran, so the money and the division are settled — what
|
|
@@ -233,6 +233,7 @@ module Spree
|
|
|
233
233
|
ApplicationRecord.transaction do
|
|
234
234
|
order.payments.update_all(order_id: nil, cart_id: cart.id)
|
|
235
235
|
order.payment_sessions.update_all(order_id: nil, cart_id: cart.id)
|
|
236
|
+
repoint_reservation_lines!(order, @copied_line_items.invert)
|
|
236
237
|
order.stock_reservations.update_all(order_id: nil, cart_id: cart.id)
|
|
237
238
|
Spree::CouponCode.where(order_id: order.id).update_all(order_id: nil, cart_id: cart.id)
|
|
238
239
|
order.reload.destroy!
|
|
@@ -284,7 +285,11 @@ module Spree
|
|
|
284
285
|
)
|
|
285
286
|
order.save!
|
|
286
287
|
|
|
288
|
+
# Copied lines validate against the new order, so its own holds must already belong to it.
|
|
289
|
+
cart.stock_reservations.update_all(order_id: order.id, cart_id: nil)
|
|
287
290
|
line_item_map = copy_line_items!(cart, order)
|
|
291
|
+
@copied_line_items = line_item_map
|
|
292
|
+
repoint_reservation_lines!(order, line_item_map)
|
|
288
293
|
fulfillment_map = copy_fulfillments!(cart, order, line_item_map)
|
|
289
294
|
fee_map = copy_typed_lines!(cart, order, line_item_map, fulfillment_map)
|
|
290
295
|
copy_promotions!(cart, order)
|
|
@@ -312,6 +317,14 @@ module Spree
|
|
|
312
317
|
order
|
|
313
318
|
end
|
|
314
319
|
|
|
320
|
+
# Seller splitting follows the copied line ids. Compensation applies the
|
|
321
|
+
# inverse map before deleting those copies, keeping the cart's holds alive.
|
|
322
|
+
def repoint_reservation_lines!(order, line_item_map)
|
|
323
|
+
line_item_map.each do |source, target|
|
|
324
|
+
order.stock_reservations.where(line_item_id: source.id).update_all(line_item_id: target.id)
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
315
328
|
# Freezes the buyer's tax registration onto the order, stamped with which
|
|
316
329
|
# link of the chain won. A copy rather than a reference: the customer can
|
|
317
330
|
# change or withdraw the number later, and the placed order's tax still
|
|
@@ -524,7 +537,6 @@ module Spree
|
|
|
524
537
|
def repoint_money_records!(cart, order)
|
|
525
538
|
cart.payments.update_all(order_id: order.id, cart_id: nil)
|
|
526
539
|
cart.payment_sessions.update_all(order_id: order.id, cart_id: nil)
|
|
527
|
-
cart.stock_reservations.update_all(order_id: order.id, cart_id: nil)
|
|
528
540
|
Spree::CouponCode.where(cart_id: cart.id).update_all(order_id: order.id, cart_id: nil)
|
|
529
541
|
order.payments.reset
|
|
530
542
|
end
|
|
@@ -54,9 +54,13 @@ module Spree
|
|
|
54
54
|
# banks), and the capture webhook that follows must still be able to
|
|
55
55
|
# complete its payment. Not halt! — that is forbidden inside a
|
|
56
56
|
# transaction (there is nothing committed to halt with).
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
#
|
|
58
|
+
# Completing the owner is checked on its own, not folded into that
|
|
59
|
+
# guard: a payment can be completed (a synchronous confirmation, an
|
|
60
|
+
# earlier delivery) while the order is not, and a redelivery or job
|
|
61
|
+
# retry must still place the order or the buyer stays charged
|
|
62
|
+
# without one.
|
|
63
|
+
step :ensure_payment unless payment_session.reload.payment&.completed?
|
|
60
64
|
step :complete_session
|
|
61
65
|
step :complete_owner unless order.reload.completed?
|
|
62
66
|
end
|
|
@@ -2,6 +2,11 @@ module Spree
|
|
|
2
2
|
module Sellers
|
|
3
3
|
# Lets a seller trade. Also the way back for one that was suspended or
|
|
4
4
|
# turned down, which is why the guard admits more than the linear path.
|
|
5
|
+
#
|
|
6
|
+
# A seller the operator created and never invited (or whose invitation is
|
|
7
|
+
# still open) can be admitted too, but only with `override_requirements`:
|
|
8
|
+
# the operator is skipping onboarding altogether, so it must be as
|
|
9
|
+
# deliberate — and as visible on the event — as skipping the checklist.
|
|
5
10
|
class Approve < Spree::Workflow
|
|
6
11
|
hooks :validate, :after_approve
|
|
7
12
|
|
|
@@ -40,6 +45,7 @@ module Spree
|
|
|
40
45
|
|
|
41
46
|
return if seller.onboarding? || seller.ready_for_review? ||
|
|
42
47
|
seller.suspended? || seller.rejected?
|
|
48
|
+
return if override_requirements && (seller.pending? || seller.invited?)
|
|
43
49
|
|
|
44
50
|
failure(seller, :not_approvable)
|
|
45
51
|
end
|
|
@@ -53,14 +53,17 @@ module Spree
|
|
|
53
53
|
# location in the marketplace needs to see. `default` is per-owner, so
|
|
54
54
|
# this is the default among *this seller's* locations and leaves the
|
|
55
55
|
# operator's own alone.
|
|
56
|
+
#
|
|
57
|
+
# A seller created with a returns address already has its location, so
|
|
58
|
+
# that one becomes the default rather than a second being made.
|
|
56
59
|
def provision_stock_location
|
|
57
|
-
@stock_location = seller.stock_locations.new(
|
|
60
|
+
@stock_location = seller.stock_locations.first || seller.stock_locations.new(
|
|
58
61
|
store: seller.store,
|
|
59
62
|
name: seller.name,
|
|
60
|
-
default: true,
|
|
61
63
|
active: true,
|
|
62
64
|
country_code: seller.store.default_country_code
|
|
63
65
|
)
|
|
66
|
+
@stock_location.default = true
|
|
64
67
|
# A new seller has no catalog, so there is nothing to propagate — the
|
|
65
68
|
# levels arrive with their products.
|
|
66
69
|
@stock_location.propagate_all_variants = false
|
|
@@ -13,8 +13,7 @@ module Spree
|
|
|
13
13
|
# external system knows. The movement records the delta between what Spree
|
|
14
14
|
# held and what the feed says.
|
|
15
15
|
class BulkUpsert < Spree::Workflow
|
|
16
|
-
|
|
17
|
-
# What the stock history shows against a movement this service wrote.
|
|
16
|
+
# What the stock history shows against a movement whose row gave no reason.
|
|
18
17
|
FEED_REASON = 'inventory_feed'.freeze
|
|
19
18
|
|
|
20
19
|
# @param store [Spree::Store] whose shelves these are. Ids are resolved
|
|
@@ -23,11 +22,13 @@ module Spree
|
|
|
23
22
|
# @param rows [Array<Hash>] one per shelf: +variant_id+ and
|
|
24
23
|
# +stock_location_id+ naming it (prefixed ids, or raw), then either
|
|
25
24
|
# +count_on_hand+ (the figure it should end at) or +adjustment+ (a
|
|
26
|
-
# signed change); +backorderable+ optional
|
|
27
|
-
# @return [Spree::ServiceModule::Result] +{ stock_level_count: N }
|
|
25
|
+
# signed change); +backorderable+ and +reason+ optional
|
|
26
|
+
# @return [Spree::ServiceModule::Result] +{ stock_level_count: N }+, or a
|
|
27
|
+
# failure naming the first row a record refused, by its position in
|
|
28
|
+
# +rows+; nothing is written then
|
|
28
29
|
def perform(store:, rows:)
|
|
29
30
|
@store = store
|
|
30
|
-
rows = resolve(Array(rows).map { |row| row.with_indifferent_access })
|
|
31
|
+
rows = resolve(Array(rows).each_with_index.map { |row, index| row.with_indifferent_access.merge(index: index) })
|
|
31
32
|
keyed = rows.select { |row| row[:variant_id].present? && row[:stock_location_id].present? }
|
|
32
33
|
return success(stock_level_count: 0) if keyed.empty?
|
|
33
34
|
|
|
@@ -38,7 +39,11 @@ module Spree
|
|
|
38
39
|
existing = existing_stock_levels(deduped)
|
|
39
40
|
count = 0
|
|
40
41
|
Spree::StockLevel.transaction do
|
|
41
|
-
deduped.each
|
|
42
|
+
deduped.each do |row|
|
|
43
|
+
count += 1 if apply_row(row, existing)
|
|
44
|
+
rescue ActiveRecord::RecordInvalid => e
|
|
45
|
+
reject_row(row, e.record.errors)
|
|
46
|
+
end
|
|
42
47
|
end
|
|
43
48
|
|
|
44
49
|
success(stock_level_count: count)
|
|
@@ -110,7 +115,7 @@ module Spree
|
|
|
110
115
|
stock_level: stock_level,
|
|
111
116
|
count_on_hand: row[:count_on_hand],
|
|
112
117
|
adjustment: row[:adjustment],
|
|
113
|
-
reason: FEED_REASON
|
|
118
|
+
reason: row[:reason].presence || FEED_REASON
|
|
114
119
|
)
|
|
115
120
|
# A feed that states a figure Spree cannot read means a typo, and a
|
|
116
121
|
# typo must be refused rather than obeyed: `to_i` would zero a shelf
|
|
@@ -119,6 +124,13 @@ module Spree
|
|
|
119
124
|
|
|
120
125
|
result.value.count_on_hand != before
|
|
121
126
|
end
|
|
127
|
+
|
|
128
|
+
# Raised inside the transaction, so every row before this one rolls back.
|
|
129
|
+
def reject_row(row, errors)
|
|
130
|
+
failure(nil, code: 'invalid_stock_levels',
|
|
131
|
+
message: "Row #{row[:index]}: #{errors.full_messages.to_sentence}",
|
|
132
|
+
rows: [{ index: row[:index], errors: errors.messages }])
|
|
133
|
+
end
|
|
122
134
|
end
|
|
123
135
|
end
|
|
124
136
|
end
|