feat: enforce SKU validity, use SKU as handle

This commit enforces proper SKU validation, uses the SKU as the Shopify handle, and implements ID-based product updates to allow renaming. It also extracts the IShop interface for TDD.
This commit is contained in:
Ben Miller
2025-12-25 04:54:55 -07:00
parent 2672d47203
commit 7cb469ccf9
8 changed files with 251 additions and 9 deletions

View File

@ -37,6 +37,18 @@ To avoid hitting Shopify API rate limits and Google Apps Script execution time l
- Reads `pendingEdits`.
- Filters for edits older than `BATCH_INTERVAL_MS` (30s) to allow for multiple quick edits to the same SKU.
- Iterates through valid edits and calls `Product.UpdateShopifyProduct`.
- **SKU Validation**: Before any action, checks if the SKU is valid (not empty, `?`, or `n`). Aborts if invalid.
### 2. Product Lifecycle Logic
- **Creation**:
- Uses the **SKU** as the Shopify **Handle** (URL slug).
- Prevents creation if the SKU is a placeholder.
- **Updates**:
- Prioritizes **ID-based lookup**.
- If the `shopify_id` column is populated, the system trusts this ID to locate the product in Shopify, even if the SKU has changed in the sheet.
- As a result, changing a SKU in the sheet and syncing will **rename** the existing product (handle/SKU) rather than creating a duplicate.
### 2. Shopify Integration (`src/shopifyApi.ts`)
@ -45,7 +57,9 @@ The project uses a hybrid approach for the Shopify Admin API:
- **REST API**: Used primarily for fetching Orders (legacy support).
- **GraphQL API**: Used for fetching and updating Products and Inventory.
The `Shop` class handles authentication using credentials stored in the "vars" sheet.
- **GraphQL API**: Used for fetching and updating Products and Inventory.
The `Shop` class implements the `IShop` interface, handling authentication using credentials stored in the "vars" sheet. The interface decoupling facilitates robust unit testing via `MockShop`.
### 3. Configuration (`src/config.ts`)