Class: Warranty::RegistrationParams

Inherits:
Object
  • Object
show all
Defined in:
app/services/warranty/registration_params.rb

Overview

Normalizes a public warranty-registration request (Warranty Registration
v2) into the payloads PublicRegistration consumes: the
matched-order per-row fields, the manual-flow product repeater, the
installers repeater, the shared purchase-card fields, and the Uppy upload
ids. Extracted from the controller so it stays thin, and reused on a
failed submission to re-render the form with everything the customer
already typed (see #group_values / #manual_product_entries /
#installer_entries).

Constant Summary collapse

GROUP_BASE_FIELDS =

Installation fields common to every product's panel; per-line extras
(ohm readings, floor type, …) are defined in LINE_EXTRAS.

%w[installed_on installation_type control_type notes].freeze
GROUP_EXTRA_FIELDS =
Warranty::LINE_EXTRAS.values.flatten.map { |field| field[:key].to_s }.uniq.freeze
MANUAL_PRODUCT_FIELDS =

Manual (unmatched) flow: per-product repeater fields, and the
purchase-card fields shared by every product on the submission.

%w[registered_skus product_line product_model installed_on].freeze
MANUAL_SHARED_FIELDS =
%w[external_order_number retailer_name purchased_on installed_on notes
seller_name seller_phone seller_email seller_address].freeze
INSTALLER_FIELDS =

The Installers step repeater — collected once for the whole submission
and stamped onto every created warranty row.

%w[self_install installer_name installer_company installer_phone].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params, order:) ⇒ RegistrationParams

Returns a new instance of RegistrationParams.

Parameters:

  • params (ActionController::Parameters)

    the request params

  • order (Order, nil)

    the token-verified order, when present



29
30
31
32
# File 'app/services/warranty/registration_params.rb', line 29

def initialize(params, order:)
  @params = params
  @order = order
end

Instance Method Details

#group_valuesHash{String => Hash}

Submitted per-row values keyed like the form fields
("<line_item_id>" / "<line_item_id>-<unit>") — lets a failed
submission re-render each product row with what was typed.

Returns:

  • (Hash{String => Hash})


74
75
76
# File 'app/services/warranty/registration_params.rb', line 74

def group_values
  grouped_warranty_params
end

#installer_entriesArray<Warranty>

Returns one unsaved row per submitted installer entry.

Returns:

  • (Array<Warranty>)

    one unsaved row per submitted installer entry



90
91
92
# File 'app/services/warranty/registration_params.rb', line 90

def installer_entries
  installers_params.map { |attributes| Warranty.new(attributes) }
end

#manual_product_entriesArray<Warranty::Product>

Unsaved Product objects mirroring the submitted
manual-product entries (per-line extras flattened onto their store
accessors) — for re-rendering the repeater after a failed submission.

Returns:



83
84
85
86
87
# File 'app/services/warranty/registration_params.rb', line 83

def manual_product_entries
  manual_products_params.map do |product|
    Warranty::Product.new(flatten_manual_product(product).except('installed_on'))
  end
end

#registrant_attributesHash

Returns:

  • (Hash)


57
58
59
60
# File 'app/services/warranty/registration_params.rb', line 57

def registrant_attributes
  params.expect(registrant: %i[first_name last_name email phone
                               street1 street2 city state_code postal_code country_iso]).to_h
end

#shared_warranty_attributesHash

Shared purchase-card fields + the installers list, merged into every
created row by PublicRegistration.

Returns:

  • (Hash)


52
53
54
# File 'app/services/warranty/registration_params.rb', line 52

def shared_warranty_attributes
  params[:warranty].present? ? warranty_params.to_h.merge(installers_attributes) : {}
end

#upload_idsArray<Integer>

Order-wide documents from the contact-step uploader.

Returns:

  • (Array<Integer>)


65
66
67
# File 'app/services/warranty/registration_params.rb', line 65

def upload_ids
  parse_upload_ids(params.dig(:warranty_uploads, :upload_ids))
end

#warranties_attributesArray<Hash>?

Returns one attributes hash per warranty row.

Returns:

  • (Array<Hash>, nil)

    one attributes hash per warranty row



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/warranty/registration_params.rb', line 35

def warranties_attributes
  # A matched order with registrable line items accepts ONLY the per-row
  # grouped fields — otherwise a token holder could smuggle arbitrary
  # manual products past the order's own line-item allowlist. Orders
  # without registrable items legitimately use the manual repeater (the
  # form renders it), as does the no-order flow.
  if order && Warranty.registrable_line_items(order).any?
    grouped_warranties_attributes.presence
  else
    manual_warranties_attributes.presence
  end
end