Skip to content

Task 7: Square Sales Data Synchronization #21

@akisma

Description

@akisma

Priority: P0 - Critical
Estimate: 4 days
Dependencies: Task 6 (Issue #20)

Product Acceptance Criteria

(Customer Value)

User Story: As a restaurant manager, I want to manually trigger Square sales data synchronization with CostFX, so that I can control when sales data is imported and validate the sync process step-by-step.

Acceptance Scenarios:

  • Manual Sales Sync: Given my restaurant has sales in Square, When I trigger a manual sales sync through the API, Then all transactions from the specified date range should be imported for cost analysis
  • Large Volume Handling: Given busy days with 1000+ transactions, When CostFX syncs sales data, Then all transactions should be processed without timeout errors or missing data
  • Menu Item Mapping: Given my Square sales show specific menu items sold, When CostFX processes this data, Then each menu item should be properly linked to its recipe ingredients for usage calculations
  • Duplicate Prevention: Given the same transaction might be synced multiple times, When CostFX imports sales data, Then duplicate transactions should be ignored to prevent inflated usage calculations
  • Historical Backfill: Given I'm setting up CostFX for the first time, When I connect my Square account, Then I should be able to manually import sales history from a specified date range for immediate analysis
  • Stepped Sync Process: Given I want to validate each sync phase, When I trigger a sales sync, Then I should be able to run raw data sync separately from transformation (like inventory sync)

Technical Implementation Checklist

(Developer Tasks)

Description:
Implement manual Square sales transaction synchronization with menu item mapping, following the same stepped pattern as SquareInventorySyncService.

Technical Acceptance Criteria:

  • Manual sync endpoint for Square order transactions (no automatic scheduling)
  • Two-phase sync: (1) Raw data to Tier 1, (2) Optional transform to Tier 2
  • Map orders to menu items and ingredients
  • Handle refunds and modifications
  • Sales data aggregation by time periods
  • Menu item to recipe mapping logic
  • Performance optimization for high-volume locations
  • Historical data backfill capability (date range parameters)

Technical Details:

// New files:
src/services/SquareSalesSyncService.js  // Orchestrates sync+transform (like SquareInventorySyncService)
src/services/SquareMenuMapping.js       // Menu item → recipe mapping
backend/migrations/TIMESTAMP_create-sales-transactions.js  // Tier 2 unified table

// Updated files:
src/adapters/SquareAdapter.js           // Implement syncSales() method (replace TODO stub)
src/services/POSDataTransformer.js      // Add squareOrderToSalesTransactions()
src/controllers/POSConnectionsController.js  // Add POST /:id/sync-sales endpoint

Phase: 3 - Square Data Synchronization (2 weeks)
Epic: Square-First Multi-POS Integration


📋 Additional Downstream Dependencies

This issue now has additional dependencies beyond the original theoretical usage calculations:

New Dependencies (Recipe Variance Analysis)

Why These Depend on Issue #21

The recipe variance analysis system (requested by Dave for the halibut vendor example) requires the sales_transactions table to calculate revenue impact:

variance_per_plate = $1.875 (actual cost - theoretical cost)
sales_count = 100 plates (from sales_transactions)
revenue_loss = $1.875 × 100 = $187.50

Data Flow for Recipe Variance

square_orders + square_order_items (Tier 1)
    ↓
POSDataTransformer
    ↓
sales_transactions (Tier 2) ← Issue #21 creates this
    ↓
Recipe variance queries by menu item + date range
    ↓
Calculate revenue impact (variance × sales_count)

What Recipe Variance Issues Need from Issue #21

sales_transactions table with proper schema (already defined)
✅ Link to inventory_item_id via source tracking (already planned)
source_pos_item_id for linking to menu items (already in schema)
✅ Date range query support with indexes (already planned)
✅ Sales count aggregation capability (query pattern, not new code)

No Scope Changes Required

The current Issue #21 scope already provides everything needed for recipe variance. The recipe variance issues (41-43) will:

  • Use the same sales_transactions table
  • Query via recipe_menu_item_linkssquare_menu_itemssales_transactions
  • Aggregate sales counts for variance × volume calculations

This comment is for awareness and priority planning only - no changes to Issue #21 deliverables needed.


Timeline Impact:

Related Documentation:

  • See docs/RECIPE_VARIANCE_ANALYSIS_PLAN.md for full architecture
  • See docs/RECIPE_VARIANCE_DEPENDENCY_ANALYSIS.md for dependency details

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions