Class: Maintenance::AmazonReturnsNoteReconciler

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/maintenance/amazon_returns_note_reconciler.rb

Overview

One-time, dry-run-first repair for Amazon return notes written to the legacy
"most recent RMA" target before Merchant RMA ID was honored.

Defined Under Namespace

Classes: Result, UnsafeCorrection

Constant Summary collapse

AMAZON_PARTNERS =
%w[amazon_seller_central_us amazon_seller_central_ca].freeze
PROCESSOR_WHODUNNIT =
'Edi::Amazon::ReturnsReportProcessor'
LEGACY_WHODUNNIT_CUTOFF =

PR #1775 began tagging system-driven RMA mutations. Before its commit
timestamp, processor-created notes have a PaperTrail create version but a
nil whodunnit. Retain that narrowly bounded legacy provenance path.

Time.utc(2026, 7, 29, 9, 41, 28).freeze
NOTE_PREFIX =
'Amazon customer return reason:'
ROW_IDENTITY_KEYS =
%i[partner order_id seller_sku return_reason amazon_rma_id merchant_rma_id return_request_date].freeze

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger

Constructor Details

#initialize(edi_logs: nil, dry_run: true, **options) ⇒ AmazonReturnsNoteReconciler

Configures the retained logs and dry-run mode.

Parameters:

  • edi_logs (Enumerable<EdiCommunicationLog>, EdiCommunicationLog, nil) (defaults to: nil)

    explicit retained logs; defaults to all Amazon return batches.

  • dry_run (Boolean) (defaults to: true)

    defaults true; false performs verified moves.

  • options (Hash)

    BaseService options, including :logger.

Options Hash (**options):

  • :logger (Logger)

    logger for diagnostic output.



38
39
40
41
42
# File 'app/services/maintenance/amazon_returns_note_reconciler.rb', line 38

def initialize(edi_logs: nil, dry_run: true, **options)
  @edi_logs = edi_logs
  @dry_run = dry_run
  super(options)
end

Instance Method Details

#callResult

Finds notes written to the legacy fallback RMA and optionally reassigns
them to the report's verified Merchant RMA ID target.

A correction is eligible only when the Merchant RMA belongs to the
reported order lineage, the note matches the report identity, the intended
RMA does not already carry that note, and PaperTrail proves the Amazon
returns processor created the source activity.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/maintenance/amazon_returns_note_reconciler.rb', line 53

def call
  rows = report_rows
  context = load_context(rows)
  analysis = analyze(rows, context)
  corrections, candidates = verify_provenance(analysis.delete(:candidate_groups), analysis[:ambiguous])
  apply_candidates!(candidates) unless @dry_run

  result = Result.new(
    dry_run: @dry_run,
    rows_scanned: rows.size,
    merchant_rows: analysis[:merchant_rows],
    already_correct: analysis[:already_correct],
    corrections:,
    conflicts: analysis[:conflicts],
    ambiguous: analysis[:ambiguous],
    moved: @dry_run ? 0 : candidates.size
  )
  log_result(result)
  result
end