Class: SpreeCmCommissioner::Integrations::Base::SyncResult

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/base/sync_result.rb

Overview

Base class for sync results Provides standardized tracking methods for API calls and record changes

Persists to database as JSONB in CmIntegrationSyncSession#sync_result column Enables comprehensive sync monitoring and debugging:

- Track success/failure status
- Monitor API call counts and patterns
- Audit record creation/update counts
- Capture error details with automatic error codes
- Generate sync performance reports
- Identify patterns in failed syncs

Example persisted results:

Success:

{
  "metrics": { "match": { "created": 2, "updated": 1, "total": 3 } },
  "api_calls": { "get_matches": 1, "get_match_details": 3 }
}

Failure:

{
  "metrics": { "match": { "created": 1, "total": 1 } },
  "api_calls": { "get_matches": 1 },
  "error": { "message": "Failed to sync matches: Connection timeout", "code": "ExternalClientError" }
}

Defined Under Namespace

Classes: RecordTracker

Instance Method Summary collapse

Constructor Details

#initializeSyncResult

Returns a new instance of SyncResult.



29
30
31
32
33
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 29

def initialize
  @metrics = {}
  @api_calls = {}
  @error = nil
end

Instance Method Details

#increment_metric(type, action, amount = 1) ⇒ Object

Increment a metric counter Useful for tracking actions that don’t fit the created/updated pattern

Example:

increment_metric(:match, :archived)                              # increment by 1
increment_metric(:inventory, :total_quantity_adjusted, 50)       # increment by 50

Parameters:

  • type (Symbol, String)

    Type of metric (e.g., :match, :inventory)

  • action (Symbol, String)

    Action performed (e.g., :archived, :adjustments)

  • amount (Integer) (defaults to: 1)

    Amount to increment by (default: 1)



154
155
156
157
158
159
160
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 154

def increment_metric(type, action, amount = 1)
  @metrics[type.to_sym] ||= {}
  @metrics[type.to_sym][action.to_sym] ||= 0
  @metrics[type.to_sym][action.to_sym] += amount

  update_metric_total(type)
end

#record_error(error) ⇒ Object

Record error

Parameters:

  • error (StandardError)

    The error that occurred



164
165
166
167
168
169
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 164

def record_error(error)
  @error = {
    message: error.message,
    code: error.class.name.split('::').last
  }
end

#success?Boolean

Check if sync was successful

Returns:

  • (Boolean)

    True if no error, false otherwise



173
174
175
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 173

def success?
  @error.nil?
end

#to_hHash

Serialize to hash for JSONB persistence

Returns:

  • (Hash)

    Serialized representation



179
180
181
182
183
184
185
186
187
188
189
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 179

def to_h
  {
    metrics: @metrics,
    api_calls: @api_calls,
    error: @error
  }.tap do |hash|
    hash.delete(:error) if @error.nil?
    hash.delete(:metrics) if @metrics.empty?
    hash.delete(:api_calls) if @api_calls.empty?
  end
end

#track(type, record) { ... } ⇒ Object

Track and sync record with explicit tracker object Provides a tracker object with helper methods for explicit save/tracking operations

Example:

track(:zone, product) do |tracker|
  product.assign_attributes(name: 'Zone A', price: 100)
  tracker.save_if_changed!(product)
end

Parameters:

  • type (Symbol, String)

    Type of record (e.g., :league, :match, :zone, :variant)

  • record (Object)

    The record being synced (must respond to new_record? and saved_changes)

Yields:

  • Block receiving tracker object for explicit operations

Returns:

  • Result of the block



89
90
91
92
93
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 89

def track(type, record)
  tracker = RecordTracker.new(self, type, record)
  yield tracker
  tracker
end

#track_api_call(call_name) { ... } ⇒ Object

Track API call with block Automatically increments the call count and executes the block Failed calls (exceptions) are not counted

Example:

external_matches = track_api_call('get_matches') { @client.get_matches! }

Parameters:

  • call_name (String)

    Name of the API call (e.g., ‘get_matches’, ‘get_zones’)

Yields:

  • Block containing the API call

Returns:

  • Result of the block



45
46
47
48
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 45

def track_api_call(call_name)
  @api_calls[call_name] = (@api_calls[call_name] || 0) + 1
  yield
end

#track_synced_record(type, record) { ... } ⇒ Object

Track synced record with block Automatically detects if record was created or updated based on new_record? and saved_changes

Example:

track_synced_record(:match, match_taxon) do
  match_taxon.assign_attributes(name: 'ISI Dangkorsenchey FC vs ANGKOR TIGER FC')
  match_taxon.save!
end

Parameters:

  • type (Symbol, String)

    Type of record (e.g., :match, :zone)

  • record (Object)

    The record being synced (must respond to new_record? and saved_changes)

Yields:

  • Block containing the save/update logic

Returns:

  • Result of the block



63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 63

def track_synced_record(type, record)
  was_new = record.new_record?
  result = yield

  if was_new
    increment_metric(type, :created)
  elsif record.saved_changes.any?
    increment_metric(type, :updated)
  end

  result
end