Executive Summary
This RFC proposes refactoring thesync-amazon-orders.ts workflow (1,836 lines) as preparation for the Integration Adapter Pattern migration. Rather than creating Amazon-specific abstractions that will be discarded, we’ll refactor toward the patterns already proven in PayPal and aligned with the planned adapter interface.
Goals:
- Reduce
sync-amazon-orders.tsfrom 1,836 lines to ~400 lines - Eliminate DRY violations between Invoice/Credit Note processing
- Eliminate duplication between API mode and CSV mode
- Create helper functions that map directly to the adapter
transformToDocuments()pattern - Make the eventual adapter extraction trivial (copy-paste ready)
- Creating Amazon-specific abstractions that don’t align with the adapter pattern
- Changing the workflow’s external behavior
- Modifying activity interfaces
Problem Statement
Current State
The Amazon orders workflow has the worst DRY violations of all integrations:Specific DRY Violations in Amazon
Total estimated duplicated code: ~800+ lines
Alignment with Integration Adapter Pattern RFC
The adapter RFC (seeintegration-adapter-pattern.mdx) defines this interface:
Proposed Solution
Phase 1: Consolidate Document Type Processing
Create a configuration-driven approach (like Shopify’screateSalesDocuments):
Phase 2: Create Batch Processing Pipeline (Follow PayPal Pattern)
Phase 3: Simplify Main Workflow
After phases 1-2, the main workflow becomes:Phase 4: Unified CSV/API Data Fetching
Alignment: How This Enables Adapter Migration
After this refactoring, creating the Amazon adapter becomes trivial copy-paste:File Structure After Refactoring
Migration Plan
Phase 1: Document Type Config (Week 1)
- Create
document-type-config.ts - Consolidate duplicate helper functions in
order-processing.ts - No changes to main workflow yet
- Add unit tests for consolidated helpers
Phase 2: Batch Processing Helper (Week 2)
- Create
amazon-batch-processing.tsfollowing PayPal pattern - Extract batch processing from main workflow
- Parallel testing: old vs new produce same results
Phase 3: Unified Data Fetching (Week 3)
- Create
data-fetching.ts - Unify CSV/API mode data retrieval
- Simplify main workflow’s mode branching
Phase 4: Main Workflow Simplification (Week 4)
- Rewrite main workflow using new helpers
- Full regression testing
- Deploy to staging
Phase 5: Adapter Extraction (Future - Part of Adapter RFC)
- Copy refactored helpers into adapter
- Register Amazon adapter
- Deprecate standalone workflow