Refine media state handling and fix CellImageBuilder errors

- Update MediaService delegation tests in src/mediaHandlers.test.ts to use mock.results for more reliable instance retrieval.
- Fix CellImageBuilder failure in src/mediaHandlers.ts by using public Shopify thumbnail URLs for synced items and direct Drive thumbnail endpoints for non-synced items.
- Fallback to IMAGE() formula in the spreadsheet for Drive items to avoid authentication issues with native cell images.
- Add test_*.txt to .gitignore to keep the workspace clean.
- Ensure all tests pass with updated log expectations and mock data.
This commit is contained in:
Ben Miller
2025-12-31 04:21:46 -07:00
parent f25fb359e8
commit dc33390650
3 changed files with 140 additions and 11 deletions

View File

@ -84,7 +84,54 @@ export function saveMediaChanges(sku: string, finalState: any[]) {
throw new Error("Product must be synced to Shopify before saving media changes.")
}
return mediaService.processMediaChanges(sku, finalState, product.shopify_id)
const logs = mediaService.processMediaChanges(sku, finalState, product.shopify_id)
// Update Sheet Thumbnail (Top of Gallery)
try {
// Refresh state to get Shopify CDN URLs
const latestState = mediaService.getUnifiedMediaState(sku, product.shopify_id);
const sorted = latestState.sort((a, b) => (a.galleryOrder || 0) - (b.galleryOrder || 0));
const firstItem = sorted[0];
if (firstItem) {
const ss = new GASSpreadsheetService();
const row = ss.getRowNumberByColumnValue("product_inventory", "sku", sku);
if (row) {
// Decide on the most reliable URL for the spreadsheet
// 1. If it's a synced Shopify item, use the Shopify preview image URL (public)
// 2. Otherwise (Drive item or adoption), use the dedicated Drive thumbnail endpoint
const isShopifyThumb = firstItem.thumbnail && firstItem.thumbnail.startsWith('http');
const driveThumbUrl = `https://drive.google.com/thumbnail?id=${firstItem.driveId}&sz=w400`;
const thumbUrl = isShopifyThumb ? firstItem.thumbnail : driveThumbUrl;
// Use CellImageBuilder for native in-cell image (Shopify only)
try {
// CellImageBuilder is picky about URLs and often fails with Drive's redirects/auth
// even if the file is public. Formula-based IMAGE() is more robust for Drive.
if (!isShopifyThumb) throw new Error("Use formula for Drive thumbnails");
const image = SpreadsheetApp.newCellImage()
.setSourceUrl(thumbUrl)
.setAltTextTitle(sku)
.setAltTextDescription(`Thumbnail for ${sku}`)
.build();
ss.setCellValueByColumnName("product_inventory", row, "thumbnail", image);
logs.push(`Updated sheet thumbnail for SKU ${sku}`);
} catch (builderErr) {
// Fallback to formula
ss.setCellValueByColumnName("product_inventory", row, "thumbnail", `=IMAGE("${thumbUrl}")`);
logs.push(`Updated sheet thumbnail (Formula) for SKU ${sku}`);
}
} else {
logs.push(`Warning: Could not find row for SKU ${sku} to update thumbnail.`);
}
}
} catch (e) {
console.warn("Failed to update sheet thumbnail", e);
logs.push(`Warning: Failed to update sheet thumbnail: ${e.message}`);
}
return logs
}