Class: Warranty::OrderLookup

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/services/warranty/order_lookup.rb

Overview

Public order lookup for warranty registration (Warranty Registration v2,
Phase 2). Matches what a customer typed against every order-number column
we key retailer and direct orders by.

result = Warranty::OrderLookup.call(query: 'so123456', email: 'a@b.com')
result.order # => Order or nil
result.verified # => true when the email/ZIP verifier matched

Access model (business decision 2026-07-16): the order number alone is
enough to see the order's PRODUCTS (so marketplace buyers whose email/ZIP
we don't have can still register). The optional email/ZIP verifier only
unlocks prefilling the customer's own contact details — verified tells
the controller which of the two signed-token scopes to issue, so no PII
is ever exposed on a bare order number.

Defined Under Namespace

Classes: Result

Constant Summary collapse

GENERIC_ERROR =

Generic error.

"We couldn't find an order with that number."

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Service

#attributes_used, #build_result, call, #logger

Methods included from Service::Initializer

#initialize

Class Method Details

.match(query) ⇒ Order?

Best order for a typed order number WITHOUT identity verification —
CRM-side matching only (the public flow must keep the verifier).
Prefers a match with registrable products, so a retailer number that
hits both the purchase and a later credit lands on the purchase.

Parameters:

  • query (String)

    order number as typed (SO…, Amazon 114-…, INV…)

Returns:



44
45
46
# File 'app/services/warranty/order_lookup.rb', line 44

def self.match(query)
  new(query: query).best_match
end

Instance Method Details

#best_matchOrder?

Returns:



49
50
51
52
# File 'app/services/warranty/order_lookup.rb', line 49

def best_match
  candidates = candidate_orders.to_a
  candidates.detect { |candidate| Warranty.registrable_line_items(candidate).any? } || candidates.first
end

#callResult

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/warranty/order_lookup.rb', line 55

def call
  return Result.new(success: false, error: 'Please enter an order number.') if normalized_query.blank?

  # Among matches, prefer one that actually has registrable products: a
  # retailer order number often matches both the purchase (SO…) and a
  # later credit/return (CO…, negative quantities) — newest-first alone
  # would land on the credit.
  candidates = candidate_orders.to_a
  matching = candidates.select { |candidate| verifier_matches?(candidate) }
  pool = matching.presence || candidates
  order = pool.detect { |candidate| Warranty.registrable_line_items(candidate).any? } || pool.first
  return Result.new(success: false, error: GENERIC_ERROR) unless order

  Result.new(success: true, order: order, verified: matching.any?)
end