Class: Spree::AgentTools::CreateExport
- Inherits:
-
Spree::AgentTool
- Object
- Spree::AgentTool
- Spree::AgentTools::CreateExport
- Defined in:
- app/tools/spree/agent_tools/create_export.rb
Overview
Exports records to a CSV the merchant can download.
Mutating not because it changes the store — it does not — but because it produces a file of store data and emails a link to it. That leaves the building, so a merchant decides, not the model. Additive, since nothing in the store is overwritten.
Constant Summary collapse
- BEARER_EXPORTS =
Exports whose rows carry a bearer instrument — a code that spends money for whoever reads it. Refused outright rather than stripped, because an export of gift cards without their codes answers nothing the merchant asked for.
Spree::Mcp::Exportsreads this list too, so a file made before this existed stays unreadable. %w[gift_cards coupon_codes].freeze
Constants inherited from Spree::AgentTool
Spree::AgentTool::PARENT_ID_DESCRIPTION
Instance Attribute Summary
Attributes inherited from Spree::AgentTool
Instance Method Summary collapse
Methods inherited from Spree::AgentTool
additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name
Constructor Details
This class inherits a constructor from Spree::AgentTool
Instance Method Details
#call(resource:, filters: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/tools/spree/agent_tools/create_export.rb', line 36 def call(resource:, filters: nil) export_class = export_class_for(resource) return unknown_resource(resource) if export_class.nil? required = "read_#{export_class.required_scope}" unless context.holds?(required) return { error: "You do not have permission to export #{resource.to_s.tr('_', ' ')}." } end # Refused here rather than at read, so the merchant is not left with a # generated file of live codes waiting to be collected. if Spree::AgentTools::CreateExport::BEARER_EXPORTS.include?(resource.to_s) return { error: "#{resource.to_s.tr('_', ' ').capitalize} carry codes that spend money for " \ 'whoever reads them, so they cannot be exported here. Download them from ' \ 'the dashboard instead.' } end export = export_class.new( store: context.store, # For a signed-in admin the export builds an ability from this user, # so the file holds exactly the records that admin could see, # record-level rules included. For an API key there is no user and # no ability: the scope check above is the whole filter, which is # how the Admin API treats key requests everywhere else. user: context.user, format: 'csv', search_params: filters.presence ) refusal = (:create, export) return refusal if refusal # Saving publishes `export.created`, which is what actually enqueues # generation. (`generate_async` hands a raw id to a job that expects a # prefixed one — the subscriber is the working path.) return { error: export.errors..to_sentence } unless export.save { ok: true, id: export.prefixed_id, resource: resource.to_s, # Generation is a background job, so there is no file yet. Naming # the tool that finishes the job keeps the model from sending the # merchant to the dashboard for something it can fetch itself. # Naming the MCP resource matters: a client that can read one will # otherwise never learn the file is there, and the model reports a # download path to a merchant instead of reading the rows itself. resource_uri: "spree+export://#{export.prefixed_id}", message: 'The export is being prepared. Once it is ready you can read its contents ' \ "from the MCP resource spree+export://#{export.prefixed_id}, or call " \ "get_resource (resource: \"exports\", id: \"#{export.prefixed_id}\") for its " \ 'status and a download_url to hand the merchant.' } end |
#summary(arguments) ⇒ Object
91 92 93 94 95 96 97 |
# File 'app/tools/spree/agent_tools/create_export.rb', line 91 def summary(arguments) label = arguments[:resource].to_s.tr('_', ' ') return "Export all #{label}" if arguments[:filters].blank? filters = arguments[:filters].keys.map { |key| key.to_s.tr('_', ' ') }.join(', ') "Export #{label} filtered by #{filters}" end |