# 05 - Shopify to BigOrder Grouping

**Status: NOT STARTED**

## Summary

Group all BigOrders that share the same Shopify customer, so staff can see the complete picture of what a customer has ordered and coordinate shipments that could be combined.

## Current State

BigOrders are linked to Shopify via `shopifyOrderId`, `shopifyCustomerId`, and `shopifyOrderName`. The `shopifyCustomerId` field enables grouping but is not currently utilized for this purpose.

## Requirements

1. Automatically link BigOrders that share the same Shopify customer
2. Display grouped orders on the order detail page as a "Related Orders" section
3. Allow staff to see if a customer has multiple pending orders
4. Help coordinate shipments that could potentially be combined
5. Add filter/grouping option by customer in the orders list

## Files to Modify

| File | Change |
|------|--------|
| `api/src/fabricator/FabricatorAdminApi.ts` | Add endpoint to get related orders by customer ID |
| `api/src/fabricator/FabricatorDatabase.ts` | Add query method `getBigOrdersByShopifyCustomerId` |
| `webapps/src/components/BigOrders/BigOrderComponent.jsx` | Add "Related Orders" section showing other orders from same customer |
| `webapps/src/components/BigOrders/BigOrderTableCells.jsx` | Add indicator if customer has multiple orders |
| `webapps/src/components/BigOrders/BigOrdersList.jsx` | Add filter/grouping option by customer |

## API Endpoints

| Method | Endpoint | Purpose |
|--------|----------|---------|
| GET | `/api/admin/shop/orders/by-customer/:customerId` | Get all BigOrders for a Shopify customer |
| GET | `/api/admin/shop/orders/:id/related` | Get related orders for a specific BigOrder |

## UI Mockup

On the BigOrder detail page, add a new section:

```
Related Orders from This Customer
---------------------------------
| Order      | Date       | Status              | Items           |
|------------|------------|---------------------|-----------------|
| #BS12340   | 2024-01-10 | Shipped             | Beyond 2        |
| #BS12345   | 2024-01-15 | ReadyToShip         | Custom Cushion  |
| #BS12350   | 2024-01-18 | WaitingForInventory | Prescription Lens|
```

## Implementation Notes

- No new database tables needed; queries use existing `shopifyCustomerId` column
- Consider caching or limiting results to active/recent orders to avoid performance issues
