Class: SpreeCmCommissioner::QueueDraftOrders::PublishStatus

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

Overview

Writes a queue-draft-order status document's current state and appends a histories entry in the same call, so no call site can update one without the other.

Firestore layout:

statuses/draft_orders/{date}/{document_id}                     — current status + terminal fields
statuses/draft_orders/{date}/{document_id}/histories/{auto_id} — one entry per transition

Unlike VotingSessions::PublishToFirestore (which fails soft — the dashboard has a working pull-to-refresh fallback), a genuine write failure here is returned as a failure, not swallowed: this document is the only channel the app has to learn a queued draft order ever completed, so QueueDraftOrders::Enqueue treats a failed initial write as fatal instead of enqueueing a job the app has no way to observe.

Instance Method Summary collapse

Methods included from FirestoreConnection

#firestore, #firestore_available?, #service_account

Instance Method Details

#call(document_path:, status:, previous_status: nil, message: nil, fields: {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/spree_cm_commissioner/queue_draft_orders/publish_status.rb', line 19

def call(document_path:, status:, previous_status: nil, message: nil, fields: {})
  return success(:skipped) unless firestore_available?

  now = Time.zone.now
  document = firestore.doc(document_path)
  document.set(fields.merge(status: status, updated_at: now), merge: true)
  document.col('histories').add(
    previous_status: previous_status,
    next_status: status,
    message: message,
    created_at: now
  )

  success(document_path)
rescue StandardError => e
  Rails.logger.error("[QueueDraftOrders::PublishStatus] document=#{document_path} #{e.class}: #{e.message}")
  failure(nil, e.message)
end