Class: SpreeCmCommissioner::QueueDraftOrders::Enqueue

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/queue_draft_orders/enqueue.rb

Overview

Accepts a request to create a draft order asynchronously: writes the Firestore status document (see PublishStatus), then schedules QueueDraftOrders::ProcessJob to actually build the order.

Deliberately does not check stock (or anything else DraftOrders::Create validates) here — that would just duplicate a check the job runs a moment later anyway. This only rejects a request too malformed to even queue (no line_items at all), mirroring DraftOrders::Create's own first guard clause. Every shape-valid request reaches the job; the job's own DraftOrders::Create call is the single source of truth for whether the order can actually be created (stock, promotions, everything else).

Attributes:

  • create_type: ['generic'] which Create service QueueDraftOrders::Process should dispatch to — not the resolved Spree product_type (an order can mix line-item product_types; this is cheaper to know up front and is all the job's dispatch needs). Only 'generic' exists so far; transit/intercity_taxi add their own create_type value and a dispatch branch later. Passed straight through to the job as a plain argument — not written onto the Firestore document itself, since nothing reads it back from there.
  • creation_params: [Hash] the exact kwargs DraftOrders::Create.call needs (line_items:, user:, store:, currency:, options:) — passed through untouched.
  • locale: [String] the request's I18n locale, so the job renders any user-facing validation message (e.g. the localized "out of stock" copy) in the same language the request was in, not whatever locale the job worker defaults to.

Instance Method Summary collapse

Instance Method Details

#call(create_type:, creation_params:, locale: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/spree_cm_commissioner/queue_draft_orders/enqueue.rb', line 29

def call(create_type:, creation_params:, locale: nil)
  line_items = creation_params[:line_items]
  return failure(nil, I18n.t('spree_cm_commissioner.draft_order.line_items_missing')) if line_items.blank?

  document_id = SecureRandom.uuid
  document_path = "statuses/draft_orders/#{Time.zone.today.iso8601}/#{document_id}"

  publish_result = publish_queued(document_path, creation_params)
  return failure(nil, publish_result.error.to_s) if publish_result.failure?

  SpreeCmCommissioner::QueueDraftOrders::ProcessJob.perform_later(
    document_path, create_type, creation_params, locale
  )

  success(document_id: document_id, document_path: document_path)
end