Class: Spree::TaxIdentifiers::Validate

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/tax_identifiers/validate.rb

Overview

The only writer of the validation columns. Writes with update_columns so recording a verdict cannot re-trigger the callback that asked for it.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(tax_identifier:) ⇒ Spree::TaxIdentifier

Parameters:

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/spree/tax_identifiers/validate.rb', line 12

def call(tax_identifier:)
  # What this run is answering about. The buyer can change the number while
  # a check is in flight, and a verdict about the old one must not be
  # written over the new one — see #persist.
  @checked = tax_identifier.slice(:kind, :value)

  # Absent, or named by an entry that no longer loads — a gem dropped
  # while a check was already queued. Both mean the same thing here as a
  # deregistration does, and neither is a registry that failed to answer,
  # so they must not be recorded as an outage.
  validator_class = Spree.tax_identifier_validators[tax_identifier.kind].presence&.to_s&.safe_constantize
  return persist(tax_identifier, unsupported_result) if validator_class.nil?

  # Nothing should enqueue a check for a format-only validator, since
  # TaxIdentifier#validatable? reports false for one. Belt and braces all
  # the same: it must not surface as a job that crashes. A validator
  # predating the predicate is assumed to ask, as it always did.
  if validator_class.respond_to?(:checks_registry?) && !validator_class.checks_registry?
    return persist(tax_identifier, unsupported_result)
  end

  result = validator_class.new.call(tax_identifier: tax_identifier)
  persist(tax_identifier, result)
rescue StandardError => error
  # A number nobody could check is not a number known to be wrong, so it
  # is recorded as unanswered rather than rejected — and recorded rather
  # than left on the `pending` the enqueue stamped. Reported rather than
  # raised, as a failed geocode is.
  Rails.error.report(
    ValidationError.new("Cannot validate tax identifier ID: #{tax_identifier.id} (#{error.message})"),
    handled: false,
    context: { tax_identifier_id: tax_identifier.id, kind: tax_identifier.kind },
    source: 'spree.core'
  )
  persist(tax_identifier, unavailable_result(error.message))
  failure(tax_identifier, error.message)
end