Class: SpreeCmCommissioner::CurrencyRates::Manage

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/currency_rates/manage.rb

Overview

Manages currency rate creation and updates for vendors. Handles rate deduplication using floating-point tolerance and maintains a history of rate changes by marking old rates as inactive.

Example:

response = Manage.new(
  vendor_id: 1,
  from_currency: 'USD',
  to_currency: 'KHR',
  rate: 4100.50
).call

if response.success?
  puts response.currency_rate.rate  # => 4100.50
else
  puts response.error  # => "Rate must be greater than 0"
end

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manage

Returns a new instance of Manage.



25
26
27
28
29
30
31
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 25

def initialize(options = {})
  @options = options
  @vendor_id = options[:vendor_id]
  @from_currency = options[:from_currency]
  @to_currency = options[:to_currency]
  @rate = options[:rate]
end

Instance Attribute Details

#from_currencyObject (readonly)

Returns the value of attribute from_currency.



23
24
25
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 23

def from_currency
  @from_currency
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 23

def options
  @options
end

#rateObject (readonly)

Returns the value of attribute rate.



23
24
25
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 23

def rate
  @rate
end

#to_currencyObject (readonly)

Returns the value of attribute to_currency.



23
24
25
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 23

def to_currency
  @to_currency
end

#vendor_idObject (readonly)

Returns the value of attribute vendor_id.



23
24
25
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 23

def vendor_id
  @vendor_id
end

Instance Method Details

#callObject



33
34
35
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
# File 'app/services/spree_cm_commissioner/currency_rates/manage.rb', line 33

def call
  return failure_response('Vendor ID is required') if vendor_id.blank?
  return failure_response('From currency is required') if from_currency.blank?
  return failure_response('To currency is required') if to_currency.blank?
  return failure_response('Rate is required') if rate.blank?
  return failure_response('Rate must be greater than 0') if rate.to_f <= 0

  existing = existing_rates.first

  # Deduplication: If a rate already exists with the same value (within floating-point tolerance),
  # return the existing rate without creating a new one.
  # This prevents duplicate entries when the same rate is submitted multiple times.
  #
  # Example:
  #   Existing rate: 4100.0
  #   New rate:      4100.00001  (difference: 0.00001, which is < 0.0001 tolerance)
  #   Result:        Returns existing rate, no new record created
  #
  # The tolerance of 0.0001 accounts for floating-point precision issues
  # and minor rounding differences in currency calculations.
  return success_response(existing) if existing && (existing.rate - rate.to_f).abs < 0.0001

  ActiveRecord::Base.transaction do
    override_existing_rates!
    new_rate = create_new_rate
    success_response(new_rate)
  end
rescue ActiveRecord::RecordInvalid => e
  failure_response(e.message)
end