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.

Removed agent code the endpoints already do

Damian Legawiec89e9e5d47f9e
diff --git a/spree/api/lib/spree/api/agent_membership_map.rb b/spree/api/lib/spree/api/agent_membership_map.rb
index 4abcb84c10..fa55994d1d 100644
--- a/spree/api/lib/spree/api/agent_membership_map.rb
+++ b/spree/api/lib/spree/api/agent_membership_map.rb
@@ -24,10 +24,7 @@ module Spree
map.register(
target: target,
- model_name: entry.model_name,
permission: write_permission_for(entry),
- positioned: controller.method_defined?(:reposition),
- service_namespace: service_namespace_for(target),
api_path: membership_path_for(controller)
)
end
@@ -70,18 +67,6 @@ module Spree
def target_for(controller)
controller.name.to_s.split('::')[-2]&.underscore&.singularize
end
-
- # A parent whose curation goes through a service has one named for it
- # — `Spree::Categories::AddProducts`, which publishes events and
- # reindexes. The others write the join row from the model. Probed
- # rather than listed, so a parent that gains a service is followed.
- def service_namespace_for(target)
- namespace = "Spree::#{target.camelize.pluralize}"
- return unless "#{namespace}::AddProducts".safe_constantize
- return unless "#{namespace}::RemoveProducts".safe_constantize
-
- namespace
- end
end
end
end
diff --git a/spree/api/spec/tools/spree/agent_tools/set_stock_spec.rb b/spree/api/spec/tools/spree/agent_tools/set_stock_spec.rb
index 743e12c9f6..278ec01d83 100644
--- a/spree/api/spec/tools/spree/agent_tools/set_stock_spec.rb
+++ b/spree/api/spec/tools/spree/agent_tools/set_stock_spec.rb
@@ -60,8 +60,15 @@ RSpec.describe 'agent stock setting' do
expect(tool.call(rows: [])[:error]).to include('at least one row')
end
- it 'needs a variant and a location on every row' do
- expect(tool.call(rows: [{ 'count_on_hand' => 5 }])[:error]).to include('variant_id')
+ # The endpoint names which field each row is missing, and the tool passes
+ # that through — a model repairs the row it got wrong instead of resending
+ # the whole batch.
+ it 'names the row that is missing a variant and a location' do
+ result = tool.call(rows: [{ 'count_on_hand' => 5 }])
+
+ expect(result[:error]).to include('variant')
+ expect(result[:rows].to_a.first).to include('index' => 0)
+ expect(result[:rows].to_a.first['missing']).to include('variant_id', 'stock_location_id')
end
# The endpoint's own refusal: a count and an adjustment together is
diff --git a/spree/api/spec/tools/spree/agent_tools/upload_chain_spec.rb b/spree/api/spec/tools/spree/agent_tools/upload_chain_spec.rb
index ac52ee884e..109aac15fd 100644
--- a/spree/api/spec/tools/spree/agent_tools/upload_chain_spec.rb
+++ b/spree/api/spec/tools/spree/agent_tools/upload_chain_spec.rb
@@ -62,8 +62,10 @@ RSpec.describe 'agent tools that consume an upload' do
it 'refuses a product in another store' do
theirs = create(:product, store: create(:store, code: "other-#{SecureRandom.hex(4)}"))
- expect(tool('media_create').call(file_id: upload_binary('x.png', png), product_id: theirs.prefixed_id)[:error]).
- to include('No product found')
+ result = tool('media_create').call(file_id: upload_binary('x.png', png), product_id: theirs.prefixed_id)
+
+ expect(result[:error]).to be_present
+ expect(theirs.reload.media).to be_empty
end
it 'is withheld from a caller who cannot write media' do
diff --git a/spree/core/app/tools/spree/agent_tools/create_media.rb b/spree/core/app/tools/spree/agent_tools/create_media.rb
index 21487f906b..926828768f 100644
--- a/spree/core/app/tools/spree/agent_tools/create_media.rb
+++ b/spree/core/app/tools/spree/agent_tools/create_media.rb
@@ -22,16 +22,44 @@ module Spree
param :alt, description: 'Alt text describing the image, for accessibility and SEO'
def call(file_id:, product_id: nil, alt: nil)
- product = nil
- if product_id.present?
- product = find_product(product_id)
- return { error: "No product found for #{product_id.inspect}." } if product.nil?
+ # Placement is the path: nested under a product puts the file in that
+ # product's gallery, the flat one leaves it in the library. The
+ # endpoint resolves the parent, checks it and verifies the signed id,
+ # so there is nothing to look up here first.
+ path = if product_id.present?
+ "/api/v3/admin/products/#{product_id}/media"
+ else
+ '/api/v3/admin/media'
+ end
- refusal = unauthorized(:update, product)
- return refusal if refusal
+ response = ApiDispatch.new(context).call(
+ method: :post, path: path, body: { signed_id: file_id, alt: alt.presence }.compact
+ )
+ unless response.success?
+ # The endpoint speaks about signed references in general, since many
+ # of its parameters are one. Here it is always a file, so the
+ # refusal names the tool that produces a valid id.
+ message = response.error_message
+ if response.status == 422 && message.to_s.match?(/signed reference/i)
+ message = "#{file_id.inspect} is not a file this store uploaded, or it has expired. " \
+ 'Call upload_file first and pass the file_id it returns.'
+ end
+
+ return { error: message }
end
- build_and_save(file_id: file_id, product: product, alt: alt)
+ media = response.data.to_h
+
+ {
+ ok: true,
+ id: media['id'],
+ filename: media['filename'],
+ media_type: media['media_type'],
+ product_id: product_id.presence,
+ # The media library has no page of its own in the map, so the link
+ # that helps is the product's — where the merchant will look at it.
+ dashboard_path: product_id.presence && dashboard_path_for(product_id)
+ }.compact
end
def summary(arguments)
@@ -42,38 +70,11 @@ module Spree
private
- def build_and_save(file_id:, product:, alt:)
- media = Spree::Media.new(store: context.store, alt: alt.presence)
- media.viewable = product if product
-
- # The signed id is verified by ActiveStorage itself: one that was not
- # issued by this installation does not resolve, so a model cannot
- # invent a reference to someone else's file.
- media.attachment = file_id
-
- return { error: media.errors.full_messages.to_sentence.presence || 'The file could not be added.' } unless
- media.save
-
- {
- ok: true,
- id: media.prefixed_id,
- filename: media.attachment.filename.to_s,
- media_type: media.media_type,
- product_id: product&.prefixed_id,
- # The media library has no page of its own in the map, so the link
- # that helps is the product's — where the merchant will look at it.
- dashboard_path: product && ResourceMap.find('products')&.dashboard_path_for(product)
- }.compact
- rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
- { error: "#{file_id.inspect} is not a file this store uploaded. Call upload_file first." }
- rescue ActiveStorage::FileNotFoundError
- { error: 'That upload did not finish — its bytes are missing. Upload the file again.' }
- end
+ def dashboard_path_for(product_id)
+ path = ResourceMap.find('products')&.dashboard_path
+ return if path.blank?
- def find_product(id)
- context.accessible(Spree::Product.for_store(context.store), :update).find_by_prefix_id(id)
- rescue StandardError
- nil
+ format(path, id: product_id)
end
end
end
diff --git a/spree/core/app/tools/spree/agent_tools/set_stock.rb b/spree/core/app/tools/spree/agent_tools/set_stock.rb
index 453aaffba6..789bf4ae53 100644
--- a/spree/core/app/tools/spree/agent_tools/set_stock.rb
+++ b/spree/core/app/tools/spree/agent_tools/set_stock.rb
@@ -25,16 +25,17 @@ module Spree
prepared = Array(rows).map { |row| row.respond_to?(:to_h) ? row.to_h.stringify_keys : {} }
return { error: 'Give at least one row.' } if prepared.empty?
- missing = prepared.each_with_index.filter_map do |row, index|
- index if row['variant_id'].blank? || row['stock_location_id'].blank?
- end
- return { error: "Every row needs a variant_id and a stock_location_id (rows #{missing.join(', ')})." } if
- missing.any?
-
response = ApiDispatch.new(context).call(
method: :post, path: '/api/v3/admin/stock_levels/bulk_upsert', body: { stock_levels: prepared }
)
- return { error: response.error_message } unless response.success?
+
+ unless response.success?
+ # The endpoint names the missing field per row, which is what lets a
+ # model repair its own request rather than resend it whole.
+ rows = response.body.is_a?(Hash) ? response.body.dig('error', 'details', 'rows') : nil
+
+ return { error: response.error_message, rows: rows }.compact
+ end
changed = response.body.is_a?(Hash) ? response.body['stock_level_count'] : nil
diff --git a/spree/core/lib/spree/agent_tools/membership_map.rb b/spree/core/lib/spree/agent_tools/membership_map.rb
index 1bef4f76f2..629036a16a 100644
--- a/spree/core/lib/spree/agent_tools/membership_map.rb
+++ b/spree/core/lib/spree/agent_tools/membership_map.rb
@@ -8,49 +8,9 @@ module Spree
# fills it from the controllers that adopt the membership concern, so a
# parent that adopts it later is curated without a new tool.
module MembershipMap
- Entry = Struct.new(:target, :model_name, :permission, :positioned, :service_namespace,
- :api_path, keyword_init: true) do
- # @return [Class]
- def model_class
- model_name.constantize
- end
-
- # Whether members carry an order the merchant arranged by hand.
- def positioned?
- positioned.present?
- end
-
- # Adds products to this parent, the way its own controller does.
- #
- # Two shapes exist and both are the controller's: a curation service
- # for the parents that publish events and reindex, a model method for
- # the ones that write a join row. Carried here so the tool never has
- # to know which parent it is holding.
- def add(parent, products)
- return if products.empty?
-
- if service_namespace
- "#{service_namespace}::AddProducts".constantize.call(service_key => [parent], products: products)
- else
- parent.add_products(products.map(&:id))
- end
- end
-
- def remove(parent, products)
- return if products.empty?
-
- if service_namespace
- "#{service_namespace}::RemoveProducts".constantize.call(service_key => [parent], products: products)
- else
- parent.remove_products(products.map(&:id))
- end
- end
-
- # `Spree::Categories::AddProducts` takes `categories:`.
- def service_key
- target.pluralize.to_sym
- end
- end
+ # Curation runs through the parent's own nested products endpoint, so
+ # an entry only needs to say which parent, what it costs and where.
+ Entry = Struct.new(:target, :permission, :api_path, keyword_init: true)
class << self
# @return [Array<Entry>]
@@ -71,11 +31,8 @@ module Spree
all.map(&:target).sort
end
- def register(target:, model_name:, permission:, positioned: false, service_namespace: nil,
- api_path: nil)
- entries[target.to_s] = Entry.new(target: target.to_s, model_name: model_name,
- permission: permission, positioned: positioned,
- service_namespace: service_namespace,
+ def register(target:, permission:, api_path: nil)
+ entries[target.to_s] = Entry.new(target: target.to_s, permission: permission,
api_path: api_path)
end