Refactor Media Manager sync logic and fix duplication bugs

This major refactor addresses improper image matching and duplication:

- Implemented strict ID-based matching in 'MediaService', removing the greedy filename matching fallback.

- Redesigned synchronization pipeline to treat Google Drive as the Source of Truth, supporting orphan adoption (Shopify -> Drive) and secure uploads.

- Implemented 'gallery_order' using Drive file properties (supporting both v2 and v3 APIs) for stable, drag-and-drop global ordering.

- Added conditional file renaming using timestamps to enforce '_' naming convention without unnecessary renames.

- Fixed runtime errors in 'MediaService' loops and updated 'ShopifyMediaService' GraphQL mutations to match correctly schema.

- Rewrote 'MediaService.test.ts' with robust test cases for strict matching, adoption, sorting, and reordering.
This commit is contained in:
Ben Miller
2025-12-28 12:25:13 -07:00
parent 6e1222cec9
commit 7c35817313
14 changed files with 1299 additions and 505 deletions

View File

@ -47,6 +47,13 @@ export function getMediaForSku(sku: string): any[] {
// Resolve Product ID (Best Effort)
const product = new Product(sku)
// Ensure we have the latest correct ID from Shopify, repairing the sheet if needed
try {
product.MatchToShopifyProduct(shop);
} catch (e) {
console.warn("MatchToShopifyProduct failed", e);
}
const shopifyId = product.shopify_id || ""
return mediaService.getUnifiedMediaState(sku, shopifyId)
@ -61,6 +68,13 @@ export function saveMediaChanges(sku: string, finalState: any[]) {
const mediaService = new MediaService(driveService, shopifyMediaService, networkService, config)
const product = new Product(sku)
// Ensure we have the latest correct ID from Shopify
try {
product.MatchToShopifyProduct(shop);
} catch (e) {
console.warn("MatchToShopifyProduct failed", e);
}
if (!product.shopify_id) {
// Allow saving Drive-only changes? No, we need Shopify context for "Staging" usually.
// But if we just rename drive files, we could?
@ -68,7 +82,30 @@ export function saveMediaChanges(sku: string, finalState: any[]) {
throw new Error("Product must be synced to Shopify before saving media changes.")
}
mediaService.processMediaChanges(sku, finalState, product.shopify_id)
return mediaService.processMediaChanges(sku, finalState, product.shopify_id)
}
export function getMediaDiagnostics(sku: string) {
const config = new Config()
const driveService = new GASDriveService()
const shop = new Shop()
const shopifyMediaService = new ShopifyMediaService(shop)
const networkService = new GASNetworkService()
const mediaService = new MediaService(driveService, shopifyMediaService, networkService, config)
// Resolve Product ID
const product = new Product(sku)
// Ensure we have the latest correct ID from Shopify
try {
product.MatchToShopifyProduct(shop);
} catch (e) {
console.warn("MatchToShopifyProduct failed", e);
}
const shopifyId = product.shopify_id || ""
return mediaService.getDiagnostics(sku, shopifyId)
}
export function saveFileToDrive(sku: string, filename: string, mimeType: string, base64Data: string) {