spree_core 6.0.2 → 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/addresses/geocode_address_job.rb +6 -3
- data/app/jobs/spree/payments/handle_webhook_job.rb +12 -1
- data/app/jobs/spree/variants/remove_line_item_job.rb +5 -0
- 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/preference_schema.rb +5 -0
- data/app/models/concerns/spree/product_scopes.rb +2 -2
- data/app/models/concerns/spree/publishable.rb +26 -3
- data/app/models/concerns/spree/typed_associations.rb +53 -20
- data/app/models/spree/address.rb +2 -2
- data/app/models/spree/authentication/strategies/base_strategy.rb +10 -1
- data/app/models/spree/cart.rb +7 -2
- data/app/models/spree/collection.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 +6 -2
- data/app/models/spree/price.rb +3 -0
- data/app/models/spree/product.rb +21 -14
- data/app/models/spree/product_category.rb +7 -1
- data/app/models/spree/product_collection.rb +8 -1
- data/app/models/spree/promotion.rb +1 -2
- 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 +8 -4
- data/app/services/spree/orders/create_from_cart.rb +6 -2
- 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/create_export.rb +3 -1
- data/app/tools/spree/agent_tools/create_import.rb +1 -0
- data/app/tools/spree/agent_tools/create_media.rb +2 -1
- data/app/tools/spree/agent_tools/create_resource.rb +12 -4
- data/app/tools/spree/agent_tools/delete_resource.rb +12 -4
- data/app/tools/spree/agent_tools/describe_resource.rb +72 -6
- data/app/tools/spree/agent_tools/get_resource.rb +8 -5
- data/app/tools/spree/agent_tools/resource_write.rb +16 -3
- data/app/tools/spree/agent_tools/search_resources.rb +51 -20
- data/app/tools/spree/agent_tools/update_resource.rb +6 -6
- data/app/tools/spree/agent_tools/upload_file.rb +1 -0
- data/app/workflows/spree/carts/add_item.rb +51 -0
- data/app/workflows/spree/carts/complete.rb +18 -3
- data/app/workflows/spree/carts/merge.rb +25 -4
- data/app/workflows/spree/orders/complete.rb +39 -14
- data/app/workflows/spree/payments/handle_webhook.rb +7 -3
- data/app/workflows/spree/products/activate.rb +7 -2
- data/app/workflows/spree/products/archive.rb +7 -2
- data/app/workflows/spree/products/draft.rb +7 -2
- data/app/workflows/spree/products/status_transition.rb +24 -0
- 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 +11 -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/generators/spree/api_resource/api_resource_generator.rb +35 -3
- data/lib/generators/spree/api_resource/templates/admin_controller.rb.tt +9 -4
- data/lib/generators/spree/api_resource/templates/admin_integration_spec.rb.tt +74 -0
- data/lib/generators/spree/api_resource/templates/store_controller.rb.tt +11 -6
- data/lib/spree/agent_tool.rb +105 -1
- data/lib/spree/agent_tools/default_catalog.rb +16 -21
- data/lib/spree/agent_tools/legacy_tools.rb +212 -0
- data/lib/spree/agent_tools/operation_catalog.rb +206 -0
- data/lib/spree/agent_tools/operation_tool.rb +189 -0
- data/lib/spree/agent_tools/record_summary.rb +8 -2
- data/lib/spree/agent_tools/registry.rb +65 -31
- data/lib/spree/agent_tools/resource_map.rb +207 -9
- data/lib/spree/agent_tools.rb +3 -3
- data/lib/spree/core/dependencies_helper.rb +96 -40
- data/lib/spree/core/engine.rb +12 -1
- data/lib/spree/core/version.rb +1 -1
- data/lib/spree/core.rb +30 -2
- data/lib/spree/reporting/default_vocabulary.rb +25 -5
- data/lib/spree/testing_support/extension_rake.rb +12 -0
- data/lib/spree/upgrades/5_6_to_6_0/manifest.yml +34 -0
- data/lib/tasks/dependencies.rake +2 -2
- data/lib/tasks/diagnostics.rake +11 -0
- metadata +29 -7
- data/lib/spree/agent_tools/workflow_endpoints.rb +0 -88
- data/lib/spree/agent_tools/workflow_schema.rb +0 -279
- data/lib/spree/agent_tools/workflow_tool.rb +0 -654
data/app/models/spree/seller.rb
CHANGED
|
@@ -22,7 +22,7 @@ module Spree
|
|
|
22
22
|
# #payout_account_reference.
|
|
23
23
|
include Spree::HasExternalReferences
|
|
24
24
|
|
|
25
|
-
MEMOIZED_METHODS = %w[onboarding_requirements onboarding_progress
|
|
25
|
+
MEMOIZED_METHODS = %w[onboarding_requirements onboarding_progress products_count_by_status returns_location].freeze
|
|
26
26
|
|
|
27
27
|
publishes_lifecycle_events
|
|
28
28
|
publishes_events :approved, :invited, :onboarding_reopened, :onboarding_started, :rejected,
|
|
@@ -106,6 +106,36 @@ module Spree
|
|
|
106
106
|
billing_address&.owner = self
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
+
# The address fields a stock location keeps on itself, as the API names
|
|
110
|
+
# them. A location is a place rather than a person, so it has no name or
|
|
111
|
+
# label of its own.
|
|
112
|
+
RETURNS_ADDRESS_FIELDS = %w[company address1 address2 city postal_code country_code state_code state_name
|
|
113
|
+
phone].freeze
|
|
114
|
+
|
|
115
|
+
# The API writes the returns address inline, like the billing address. It
|
|
116
|
+
# is the returns location's own address, so it lands on that location — a
|
|
117
|
+
# new one for a seller who has none — and a received return still restocks
|
|
118
|
+
# somewhere the catalog knows. Saved with the seller, in its transaction.
|
|
119
|
+
def returns_address=(attributes)
|
|
120
|
+
attributes = attributes.to_h.stringify_keys.slice(*RETURNS_ADDRESS_FIELDS)
|
|
121
|
+
attributes['zipcode'] = attributes.delete('postal_code') if attributes.key?('postal_code')
|
|
122
|
+
|
|
123
|
+
# Not built through the association: that would validate and save it a
|
|
124
|
+
# second time, under a generic "stock locations is invalid".
|
|
125
|
+
location = returns_location ||
|
|
126
|
+
Spree::StockLocation.new(active: true, returns_enabled: true, propagate_all_variants: false)
|
|
127
|
+
# A state belongs to its country, so a new country drops the old state
|
|
128
|
+
# unless one comes with it.
|
|
129
|
+
if attributes.key?('country_code') && attributes['country_code'].to_s.upcase != location.country_code.to_s.upcase
|
|
130
|
+
attributes = { 'state_code' => nil, 'state_name' => nil }.merge(attributes)
|
|
131
|
+
end
|
|
132
|
+
location.assign_attributes(attributes)
|
|
133
|
+
@returns_location = @pending_returns_location = location
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
validate :validate_returns_location, if: -> { @pending_returns_location }
|
|
137
|
+
after_save :save_returns_location, if: -> { @pending_returns_location }
|
|
138
|
+
|
|
109
139
|
# Where this seller keeps stock, and so where their returns land. Released
|
|
110
140
|
# rather than destroyed for the same reason products are: the operator
|
|
111
141
|
# decides what becomes of a departed seller's inventory.
|
|
@@ -428,14 +458,32 @@ module Spree
|
|
|
428
458
|
|
|
429
459
|
# @return [Integer] how many products this seller lists
|
|
430
460
|
def products_count
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
siblings = lazy_preload_context&.records&.grep(self.class)
|
|
434
|
-
return @products_count = products.count if siblings.blank?
|
|
461
|
+
products_count_by_status.values.sum
|
|
462
|
+
end
|
|
435
463
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
464
|
+
# How many of this seller's products sit in each status, every status
|
|
465
|
+
# present: what the operator reads off a seller to see what is live and
|
|
466
|
+
# what is waiting on them. On a list, one grouped query serves every
|
|
467
|
+
# seller on the page.
|
|
468
|
+
#
|
|
469
|
+
# @return [Hash{String => Integer}]
|
|
470
|
+
def products_count_by_status
|
|
471
|
+
return @products_count_by_status if @products_count_by_status
|
|
472
|
+
|
|
473
|
+
blank = Spree::Product::ALL_STATUSES.index_with(0)
|
|
474
|
+
# An unsaved seller has no id, and `seller_id IS NULL` would count the
|
|
475
|
+
# operator's own catalog.
|
|
476
|
+
return blank if new_record?
|
|
477
|
+
|
|
478
|
+
siblings = (lazy_preload_context&.records&.grep(self.class).presence || [self]).select(&:persisted?)
|
|
479
|
+
counts = Spree::Product.where(seller_id: siblings.map(&:id)).group(:seller_id, :status).count
|
|
480
|
+
by_seller = counts.group_by { |(seller_id, _status), _count| seller_id }
|
|
481
|
+
siblings.each do |seller|
|
|
482
|
+
seller.preloaded_products_count_by_status = blank.merge(
|
|
483
|
+
by_seller.fetch(seller.id, []).to_h { |(_seller_id, status), count| [status, count] }
|
|
484
|
+
)
|
|
485
|
+
end
|
|
486
|
+
@products_count_by_status
|
|
439
487
|
end
|
|
440
488
|
|
|
441
489
|
# Where this seller stands against the marketplace's checklist
|
|
@@ -483,12 +531,47 @@ module Spree
|
|
|
483
531
|
|
|
484
532
|
protected
|
|
485
533
|
|
|
486
|
-
def
|
|
487
|
-
@
|
|
534
|
+
def preloaded_products_count_by_status=(counts)
|
|
535
|
+
@products_count_by_status = counts
|
|
488
536
|
end
|
|
489
537
|
|
|
490
538
|
private
|
|
491
539
|
|
|
540
|
+
# Reported under the names the client sent, so a 422 lands on the field.
|
|
541
|
+
def validate_returns_location
|
|
542
|
+
location = @pending_returns_location
|
|
543
|
+
# Filled in here rather than in the writer: on create, the name and the
|
|
544
|
+
# store may be assigned after the address. The id rather than the
|
|
545
|
+
# record, which would add the location to `stock_locations` and have it
|
|
546
|
+
# validated a second time.
|
|
547
|
+
location.seller_id = id
|
|
548
|
+
location.store ||= store
|
|
549
|
+
location.name ||= name
|
|
550
|
+
return if location.valid?
|
|
551
|
+
|
|
552
|
+
# Before the seller has an id, uniqueness compares against the
|
|
553
|
+
# operator's own locations; the save after the seller re-checks it.
|
|
554
|
+
location.errors.delete(:name, :taken) if new_record?
|
|
555
|
+
return if location.errors.empty?
|
|
556
|
+
|
|
557
|
+
location.errors.each do |error|
|
|
558
|
+
field = error.attribute == :zipcode ? :postal_code : error.attribute
|
|
559
|
+
errors.import(error, attribute: :"returns_address.#{field}")
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
def save_returns_location
|
|
564
|
+
location = @pending_returns_location
|
|
565
|
+
@pending_returns_location = nil
|
|
566
|
+
location.seller_id = id
|
|
567
|
+
return unless location.new_record? || location.changed?
|
|
568
|
+
|
|
569
|
+
location.save!
|
|
570
|
+
# A new location is not in a list loaded before it existed, and the
|
|
571
|
+
# returns location is read from that list once the memo clears.
|
|
572
|
+
stock_locations.reset
|
|
573
|
+
end
|
|
574
|
+
|
|
492
575
|
# The same choice as the query in {#returns_location}, made over the
|
|
493
576
|
# preloaded locations.
|
|
494
577
|
def returns_location_from_loaded
|
|
@@ -10,16 +10,35 @@ module Spree
|
|
|
10
10
|
preference :require_about, :boolean, default: true
|
|
11
11
|
preference :require_logo, :boolean, default: true
|
|
12
12
|
preference :require_cover_photo, :boolean, default: false
|
|
13
|
-
preference :require_contact_email, :boolean, default:
|
|
13
|
+
preference :require_contact_email, :boolean, default: false
|
|
14
14
|
|
|
15
15
|
def met_by_seller?(seller)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
missing_fields(seller).empty?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The checklist line only says "complete your profile", so the seller
|
|
20
|
+
# is told which ticked fields are still empty — the contact email in
|
|
21
|
+
# particular lives on a different settings page than the rest, so when
|
|
22
|
+
# it is the only gap the state says so and a panel can link there.
|
|
23
|
+
def blocker(seller)
|
|
24
|
+
missing = missing_fields(seller)
|
|
25
|
+
return nil if missing.empty? || latest_submission(seller)&.waived?
|
|
26
|
+
|
|
27
|
+
fields = missing.map { |field| I18n.t("spree.seller_requirement_types.complete_profile.fields.#{field}") }
|
|
28
|
+
state = missing == [:contact_email] ? 'contact_email_missing' : 'incomplete'
|
|
29
|
+
{ state: state, message: I18n.t('spree.seller_requirement_types.complete_profile.missing', fields: fields.to_sentence) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
21
33
|
|
|
22
|
-
|
|
34
|
+
def missing_fields(seller)
|
|
35
|
+
[
|
|
36
|
+
(:name if seller.name.blank?),
|
|
37
|
+
(:about if preferred_require_about && seller.about.blank?),
|
|
38
|
+
(:logo if preferred_require_logo && !seller.logo.attached?),
|
|
39
|
+
(:cover_photo if preferred_require_cover_photo && !seller.cover_photo.attached?),
|
|
40
|
+
(:contact_email if preferred_require_contact_email && seller.contact_email.blank?)
|
|
41
|
+
].compact
|
|
23
42
|
end
|
|
24
43
|
end
|
|
25
44
|
end
|
|
@@ -70,10 +70,39 @@ module Spree
|
|
|
70
70
|
scope :with_incoming, -> { where(arel_table[:incoming_count].gt(0)) }
|
|
71
71
|
scope :with_reserved, -> { where(arel_table[:reserved_count].gt(0)) }
|
|
72
72
|
|
|
73
|
+
# The same two questions asked of a variant rather than one shelf: every
|
|
74
|
+
# level of a variant whose stock, summed over all active locations, has run
|
|
75
|
+
# out or is running low. A variant with plenty in one warehouse is not out
|
|
76
|
+
# of stock because another location holds none of it. Untracked variants
|
|
77
|
+
# have no count to run out of, so they never match; a backorderable one
|
|
78
|
+
# still does, since selling on backorder is when restocking matters most.
|
|
79
|
+
scope :out_of_stock_overall, -> { where(variant_id: variant_ids_by_total_purchasable { |total| total.lteq(0) }) }
|
|
80
|
+
scope :low_stock_overall, lambda { |threshold = nil|
|
|
81
|
+
ceiling = threshold || Spree::Current.store&.preferred_low_stock_threshold || 0
|
|
82
|
+
where(variant_id: variant_ids_by_total_purchasable { |total| total.gt(0).and(total.lteq(ceiling)) })
|
|
83
|
+
}
|
|
84
|
+
|
|
73
85
|
# What the Inventory page's stock-status filter can ask for. Each names a
|
|
74
86
|
# scope above; `STOCK_STATUS_SCOPES` is the allowlist that keeps a request
|
|
75
87
|
# from naming any other method.
|
|
76
|
-
STOCK_STATUS_SCOPES = %w[in_stock low_stock out_of_stock
|
|
88
|
+
STOCK_STATUS_SCOPES = %w[in_stock low_stock out_of_stock low_stock_overall out_of_stock_overall
|
|
89
|
+
with_incoming with_reserved].freeze
|
|
90
|
+
|
|
91
|
+
# Ids of tracked variants whose purchasable units across
|
|
92
|
+
# every active location satisfy the condition the block builds. Unscoped,
|
|
93
|
+
# so the sum covers all locations even when the caller has narrowed the
|
|
94
|
+
# list to one.
|
|
95
|
+
#
|
|
96
|
+
# @yieldparam total [Arel::Nodes::Node] the summed purchasable units
|
|
97
|
+
# @return [ActiveRecord::Relation] a `variant_id` subquery
|
|
98
|
+
def self.variant_ids_by_total_purchasable
|
|
99
|
+
levels = unscoped.where(deleted_at: nil)
|
|
100
|
+
levels.joins(:stock_location).merge(Spree::StockLocation.active).
|
|
101
|
+
joins(:variant).where(spree_variants: { track_inventory: true, deleted_at: nil }).
|
|
102
|
+
group(:variant_id).
|
|
103
|
+
having(yield(Arel::Nodes::Sum.new([purchasable_arel]))).
|
|
104
|
+
select(:variant_id)
|
|
105
|
+
end
|
|
77
106
|
|
|
78
107
|
# Rows in any of the named states — "out of stock, or with units on the
|
|
79
108
|
# way" is one question a merchant asks, not two filters they combine.
|
|
@@ -58,6 +58,7 @@ module Spree
|
|
|
58
58
|
validates :quantity, numericality: { greater_than: 0 }, allow_nil: true,
|
|
59
59
|
if: -> { allocated? || released? || shipped? }
|
|
60
60
|
validates :reason, presence: true, if: :adjusted?
|
|
61
|
+
validates :reason, length: { maximum: 255 }, allow_blank: true
|
|
61
62
|
|
|
62
63
|
scope :recent, -> { order(created_at: :desc) }
|
|
63
64
|
KINDS.each do |movement_kind|
|
|
@@ -3,23 +3,39 @@ module Spree
|
|
|
3
3
|
class Destroy
|
|
4
4
|
prepend Spree::ServiceModule::Base
|
|
5
5
|
|
|
6
|
+
# Destroys a deletable cart while serializing against checkout completion.
|
|
7
|
+
# Live completion claims leave the cart and its dependent records intact.
|
|
8
|
+
#
|
|
9
|
+
# @param cart [Spree::Cart, nil] cart to abandon
|
|
10
|
+
# @param order [Spree::Cart, Spree::Order, nil] deprecated input alias
|
|
11
|
+
# @return [Spree::ServiceModule::Result] deletion outcome
|
|
6
12
|
def call(cart: nil, order: nil)
|
|
7
13
|
if order
|
|
8
14
|
Spree::Deprecation.warn('Calling Spree::Carts::Destroy with order: is deprecated and will be removed in Spree 6.1. Pass cart: instead.')
|
|
9
15
|
cart ||= order
|
|
10
16
|
end
|
|
11
17
|
rewrite_input!(remove: [:order], cart: cart)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
return failure(false, I18n.t('spree.cannot_be_destroyed')) unless cart
|
|
19
|
+
|
|
20
|
+
cart.with_lock do
|
|
21
|
+
run :check_if_can_be_destroyed
|
|
22
|
+
run :cancel_shipments
|
|
23
|
+
run :release_gift_card
|
|
24
|
+
run :void_payments
|
|
25
|
+
run :clear_addresses
|
|
26
|
+
run :destroy_order
|
|
27
|
+
end
|
|
18
28
|
end
|
|
19
29
|
|
|
20
30
|
private
|
|
21
31
|
|
|
32
|
+
# Refuses live completion claims and settled purchases before destructive steps.
|
|
33
|
+
#
|
|
34
|
+
# @param cart [Spree::Cart, Spree::Order] purchase being checked
|
|
35
|
+
# @return [Spree::ServiceModule::Result] deletion eligibility
|
|
22
36
|
def check_if_can_be_destroyed(cart:)
|
|
37
|
+
return failure(false, I18n.t('spree.cart_completion_in_progress')) if cart.is_a?(Spree::Cart) && cart.completion_claimed?
|
|
38
|
+
|
|
23
39
|
return failure(false, I18n.t('spree.cannot_be_destroyed')) unless cart&.can_be_deleted?
|
|
24
40
|
|
|
25
41
|
success(cart: cart)
|
|
@@ -12,24 +12,36 @@ module Spree
|
|
|
12
12
|
return if collection.destroyed?
|
|
13
13
|
return if collection.manual?
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
# Loaded rather than plucked: a row dropped below is gone by the time
|
|
16
|
+
# its deleted event can be published, so it has to be held.
|
|
17
|
+
previous_rows = collection.product_collections.order(position: :asc).to_a
|
|
18
|
+
previous_products_ids = previous_rows.map(&:product_id)
|
|
19
|
+
matching_ids = collection.products_matching_rules.ids
|
|
20
|
+
matching = matching_ids.to_set
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Spree::ProductCollection.where(collection_id: collection.id).delete_all
|
|
22
|
+
retained_rows = previous_rows.select { |row| matching.include?(row.product_id) }
|
|
23
|
+
removed_rows = previous_rows - retained_rows
|
|
24
|
+
added_ids = matching_ids - previous_products_ids
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
# Hard-delete (no callbacks). Class-level delete rather than the
|
|
27
|
+
# association proxy, which under `dependent: :destroy_async` doesn't
|
|
28
|
+
# reliably remove rows here.
|
|
29
|
+
Spree::ProductCollection.where(id: removed_rows.map(&:id)).delete_all if removed_rows.any?
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
# Close the gaps the removals left, keeping the survivors in the order
|
|
32
|
+
# they were already in. One statement rather than one per row:
|
|
33
|
+
# dropping an early member shifts every row behind it.
|
|
34
|
+
moved = retained_rows.each_with_index.filter_map do |row, index|
|
|
35
|
+
next if row.position == index + 1
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
{ id: row.id, collection_id: collection.id, product_id: row.product_id,
|
|
38
|
+
position: index + 1, created_at: row.created_at, updated_at: Time.current }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
Spree::ProductCollection.upsert_all(moved, unique_by: (:id unless Spree.mysql?)) if moved.any?
|
|
32
42
|
|
|
43
|
+
if added_ids.any?
|
|
44
|
+
records_to_insert = added_ids.map.with_index(retained_rows.size + 1) do |product_id, position|
|
|
33
45
|
product_collection_attributes(product_id, collection, position)
|
|
34
46
|
end
|
|
35
47
|
|
|
@@ -38,19 +50,29 @@ module Spree
|
|
|
38
50
|
|
|
39
51
|
# Expire cache + reindex the union of previous and new members, so products
|
|
40
52
|
# dropped when the match set empties also refresh their stale collection_ids.
|
|
41
|
-
products = Spree::Product.where(id: (previous_products_ids +
|
|
53
|
+
products = Spree::Product.where(id: (previous_products_ids + matching_ids).uniq)
|
|
42
54
|
products.touch_all
|
|
43
55
|
products.each(&:enqueue_search_index)
|
|
44
56
|
|
|
45
57
|
# counter caches — bulk insert/delete bypass the ProductCollection counter_cache callbacks
|
|
46
58
|
Spree::Collection.reset_counters(collection.id, :product_collections)
|
|
47
|
-
Spree::Product.reset_counters_in_bulk(previous_products_ids +
|
|
59
|
+
Spree::Product.reset_counters_in_bulk(previous_products_ids + matching_ids, :product_collections)
|
|
60
|
+
|
|
61
|
+
publish_membership_events(collection, removed_rows, added_ids)
|
|
48
62
|
|
|
49
63
|
success(collection)
|
|
50
64
|
end
|
|
51
65
|
|
|
52
66
|
private
|
|
53
67
|
|
|
68
|
+
def publish_membership_events(collection, removed_rows, added_ids)
|
|
69
|
+
removed_rows.each { |row| row.publish_event('product_collection.deleted') }
|
|
70
|
+
return if added_ids.empty?
|
|
71
|
+
|
|
72
|
+
Spree::ProductCollection.where(collection_id: collection.id, product_id: added_ids).
|
|
73
|
+
find_each { |row| row.publish_event('product_collection.created') }
|
|
74
|
+
end
|
|
75
|
+
|
|
54
76
|
def product_collection_attributes(product_id, collection, position)
|
|
55
77
|
{
|
|
56
78
|
product_id: product_id,
|
|
@@ -11,7 +11,10 @@ module Spree
|
|
|
11
11
|
prepend Spree::ServiceModule::Base
|
|
12
12
|
|
|
13
13
|
# @param company [Spree::Company]
|
|
14
|
-
# @param email [String]
|
|
14
|
+
# @param email [String, nil] who to add, when no +customer+ is given
|
|
15
|
+
# @param customer [Spree.customer_class, nil] an existing customer, who
|
|
16
|
+
# becomes a member immediately — for a caller that picked them rather
|
|
17
|
+
# than typed an address
|
|
15
18
|
# @param inviter [Object, nil] the inviting customer, nil when staff
|
|
16
19
|
# @param metadata [Hash, nil] extension payload — carried on the
|
|
17
20
|
# invitation only, where Enterprise reads it back on acceptance
|
|
@@ -26,9 +29,11 @@ module Spree
|
|
|
26
29
|
# whoever happens to hold that email.
|
|
27
30
|
# @return [Spree::ServiceModule::Result] value is the created
|
|
28
31
|
# Spree::CompanyMembership or Spree::CompanyInvitation
|
|
29
|
-
def call(company:, email
|
|
30
|
-
normalized_email = email.to_s.strip.downcase
|
|
31
|
-
customer
|
|
32
|
+
def call(company:, email: nil, customer: nil, inviter: nil, metadata: nil, require_acceptance: false)
|
|
33
|
+
normalized_email = (email.presence || customer&.email).to_s.strip.downcase
|
|
34
|
+
# Acceptance outranks a customer handed in: they are invited by their
|
|
35
|
+
# address like anyone else, so only the holder can agree to join.
|
|
36
|
+
customer = require_acceptance ? nil : (customer || Spree.customer_class.find_by(email: normalized_email))
|
|
32
37
|
|
|
33
38
|
record =
|
|
34
39
|
if customer
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
class Diagnostics
|
|
3
|
+
# The most recent Spree errors, for the diagnostics report. Subscribed to
|
|
4
|
+
# the Rails error reporter, so it sees what Spree reports with
|
|
5
|
+
# +Rails.error.report+ as well as unhandled request and job errors.
|
|
6
|
+
#
|
|
7
|
+
# Only the exception class and where it was raised are kept, never the
|
|
8
|
+
# message: messages carry customer data (emails, addresses, values echoed
|
|
9
|
+
# from a row) and the report ends up in a public issue. Frames from the
|
|
10
|
+
# host application become +app/[redacted]+ and frames from private gems
|
|
11
|
+
# +gem/[redacted]+, so the merchant's own code is not shared either.
|
|
12
|
+
#
|
|
13
|
+
# Entries live in Rails.cache, so a cache clear empties the log. With a
|
|
14
|
+
# per-process memory store a restart does too, and each process keeps its
|
|
15
|
+
# own.
|
|
16
|
+
class ErrorLog
|
|
17
|
+
CACHE_KEY = 'spree/diagnostics/errors'.freeze
|
|
18
|
+
LIMIT = 50
|
|
19
|
+
FRAME_LIMIT = 10
|
|
20
|
+
HOST_FRAME = 'app/[redacted]'.freeze
|
|
21
|
+
PRIVATE_FRAME = 'gem/[redacted]'.freeze
|
|
22
|
+
RECORDING = :spree_diagnostics_error_log_recording
|
|
23
|
+
|
|
24
|
+
# One backtrace line, as the report shows it.
|
|
25
|
+
Frame = Struct.new(:text, :spree, :host)
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
# @return [Array<Hash>] newest first
|
|
29
|
+
def entries
|
|
30
|
+
Array(Rails.cache.read(CACHE_KEY)).sort_by { |entry| entry[:last_seen_at] }.reverse
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @return [void]
|
|
34
|
+
def clear
|
|
35
|
+
Rails.cache.delete(CACHE_KEY)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Gem directories and the application's, longest first, so a path is
|
|
39
|
+
# matched by the most specific root: a gem vendored inside the app is
|
|
40
|
+
# that gem, and an app inside a gem's directory (a test dummy app) is
|
|
41
|
+
# the app. Built once per process, since every reported error needs it.
|
|
42
|
+
#
|
|
43
|
+
# @return [Array<Array(String, Proc)>] path prefix and the frame it makes
|
|
44
|
+
def roots
|
|
45
|
+
@roots ||= Gem.loaded_specs.values.map { |spec| [spec.full_gem_path + File::SEPARATOR, gem_frame(spec)] }.
|
|
46
|
+
push([Rails.root.to_s + File::SEPARATOR, ->(_) { Frame.new(HOST_FRAME, false, true) }]).
|
|
47
|
+
sort_by { |root, _| -root.length }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def gem_frame(spec)
|
|
53
|
+
return ->(_) { Frame.new(PRIVATE_FRAME, false, false) } unless Stack.public_gem?(spec)
|
|
54
|
+
|
|
55
|
+
spree = Stack.spree_gem?(spec)
|
|
56
|
+
->(relative) { Frame.new("#{spec.name}-#{spec.version}/#{relative}", spree, false) }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Rails error subscriber interface. Leaves out refused requests: an
|
|
61
|
+
# error Rails answers with a 4xx (an invalid record, a missing one, a
|
|
62
|
+
# bad parameter) is the client's, not a bug. Never raises, and never
|
|
63
|
+
# recurses: a cache store that reports its own failure to Rails.error
|
|
64
|
+
# would otherwise call back in here.
|
|
65
|
+
def report(error, context:, source: nil, **)
|
|
66
|
+
return if Thread.current[RECORDING] || client_error?(error)
|
|
67
|
+
|
|
68
|
+
recording do
|
|
69
|
+
frames = frames_for(error)
|
|
70
|
+
record(error.class.name, source, frames, request_id(context)) if spree_error?(source, frames)
|
|
71
|
+
end
|
|
72
|
+
rescue StandardError
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# Only the outermost call clears the flag: a nested call that returned
|
|
79
|
+
# early would otherwise re-arm recursion for the rest of the outer one.
|
|
80
|
+
def recording
|
|
81
|
+
Thread.current[RECORDING] = true
|
|
82
|
+
yield
|
|
83
|
+
ensure
|
|
84
|
+
Thread.current[RECORDING] = nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def record(error_class, source, frames, request_id)
|
|
88
|
+
now = Time.current.iso8601
|
|
89
|
+
fingerprint = [error_class, (frames.find(&:spree) || frames.first)&.text].join('|')
|
|
90
|
+
entries = Array(Rails.cache.read(CACHE_KEY))
|
|
91
|
+
entry = entries.find { |existing| existing[:fingerprint] == fingerprint }
|
|
92
|
+
|
|
93
|
+
if entry
|
|
94
|
+
entry.merge!(count: entry[:count] + 1, last_seen_at: now, request_id: request_id || entry[:request_id])
|
|
95
|
+
else
|
|
96
|
+
entries << { fingerprint: fingerprint, error_class: error_class, source: source, frames: frames.map(&:text),
|
|
97
|
+
request_id: request_id, count: 1, first_seen_at: now, last_seen_at: now }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
kept = entries.sort_by { |existing| existing[:last_seen_at] }.last(LIMIT)
|
|
101
|
+
Rails.cache.write(CACHE_KEY, kept)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Spree's when Spree reported it, or when Spree code ran between the
|
|
105
|
+
# raise and the host application. Every request also passes through
|
|
106
|
+
# Spree's middleware further out, so frames past the first host
|
|
107
|
+
# application frame don't count: an error raised in the merchant's own
|
|
108
|
+
# controller is theirs.
|
|
109
|
+
def spree_error?(source, frames)
|
|
110
|
+
source.to_s.start_with?('spree') || frames.take_while { |frame| !frame.host }.any?(&:spree)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Where the error was raised, then every Spree frame and the host
|
|
114
|
+
# application's (collapsed), so the Spree line that led to a library
|
|
115
|
+
# error isn't pushed out by the library's own frames. Ruby's own frames
|
|
116
|
+
# are dropped.
|
|
117
|
+
def frames_for(error)
|
|
118
|
+
origin, *rest = Array(error.backtrace).filter_map { |line| frame(line) }
|
|
119
|
+
[origin, *rest.select { |frame| frame.spree || frame.host }].compact.
|
|
120
|
+
chunk_while { |a, b| a.host && b.host }.map(&:first).first(FRAME_LIMIT)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def frame(line)
|
|
124
|
+
path = line.sub(/:in .*\z/, '')
|
|
125
|
+
root, make = self.class.roots.find { |prefix, _| path.start_with?(prefix) }
|
|
126
|
+
make&.call(path.delete_prefix(root))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def client_error?(error)
|
|
130
|
+
ActionDispatch::ExceptionWrapper.status_code_for_exception(error.class.name) < 500
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def request_id(context)
|
|
134
|
+
controller = context[:controller]
|
|
135
|
+
context[:request_id] || (controller.request.request_id if controller.respond_to?(:request))
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
class Diagnostics
|
|
3
|
+
# One part of the diagnostics report. A subclass names its rows; each row
|
|
4
|
+
# is read separately and reports "unavailable" when reading it fails, since
|
|
5
|
+
# a half-broken installation is exactly when the report is needed.
|
|
6
|
+
#
|
|
7
|
+
# Everything a row returns ends up in a public GitHub issue, so a row adds
|
|
8
|
+
# one deliberately chosen fact: never a name, URL, email or credential, and
|
|
9
|
+
# records only by prefixed ID.
|
|
10
|
+
#
|
|
11
|
+
# class MyGem::DiagnosticsSection < Spree::Diagnostics::Section
|
|
12
|
+
# def title = 'My gem'
|
|
13
|
+
#
|
|
14
|
+
# def rows
|
|
15
|
+
# { 'Mode' => -> { MyGem.config.mode } }
|
|
16
|
+
# end
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# Spree.diagnostics_sections << 'MyGem::DiagnosticsSection'
|
|
20
|
+
class Section
|
|
21
|
+
UNAVAILABLE = 'unavailable'.freeze
|
|
22
|
+
|
|
23
|
+
attr_reader :store
|
|
24
|
+
|
|
25
|
+
# @param store [Spree::Store]
|
|
26
|
+
def initialize(store)
|
|
27
|
+
@store = store
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @return [String]
|
|
31
|
+
def key
|
|
32
|
+
self.class.name.demodulize.underscore
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @return [String]
|
|
36
|
+
def title
|
|
37
|
+
raise NotImplementedError
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [Hash{String => Proc}] label => reader
|
|
41
|
+
def rows
|
|
42
|
+
raise NotImplementedError
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @return [Array<Hash>] `{ label:, value: }`, values as strings
|
|
46
|
+
def to_rows
|
|
47
|
+
rows.filter_map do |label, reader|
|
|
48
|
+
value = read(reader)
|
|
49
|
+
{ label: label, value: value } unless value.nil?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def read(reader)
|
|
56
|
+
format_value(reader.call)
|
|
57
|
+
rescue StandardError
|
|
58
|
+
UNAVAILABLE
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def format_value(value)
|
|
62
|
+
case value
|
|
63
|
+
when nil then nil
|
|
64
|
+
when true then 'yes'
|
|
65
|
+
when false then 'no'
|
|
66
|
+
when Array then value.compact_blank.join(', ').presence || 'none'
|
|
67
|
+
else value.to_s
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|