Refine Media Manager Save Logic and UI

- Add failing global function verification test (GlobalFunctions.test.ts) and fix missing exports in global.ts.
- Refactor MediaManager.html UI:
    - Implement 
enderPlanHtml to standardize Plan (Details) and Execution views.
    - Show 'Skipped' state for empty save phases.
    - Visually decouple 'Sheet Update' from 'Reorder' phase.
    - Separate 'Manual Link' operations into their own 'Linking' section in the plan view, distinct from Adoptions.
    - Fix TypeErrors in 
enderPlanHtml (undefined actions) and 
enderMatch (missing DOM elements).
- Update MediaService.test.ts to match new filename constraints on reorder.
- Update mediaHandlers.test.ts to correctly spy on loose MediaService instances.
- Ensure all tests pass.
This commit is contained in:
Ben Miller
2026-01-01 08:04:06 -07:00
parent 8d780d2fcb
commit 2c01693271
10 changed files with 863 additions and 307 deletions

View File

@ -114,14 +114,33 @@ export function saveMediaChanges(sku: string, finalState: any[], jobId: string |
const logs = mediaService.processMediaChanges(sku, finalState, product.shopify_id, jobId)
// Update Sheet Thumbnail (Top of Gallery)
updateSpreadsheetThumbnail(sku);
return logs
}
export function updateSpreadsheetThumbnail(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)
const ss = new GASSpreadsheetService();
const product = new Product(sku);
// Need Shopify ID for accurate state logic?
// getUnifiedMediaState uses it.
try { product.MatchToShopifyProduct(shop); } catch(e) {}
try {
// Refresh state to get Shopify CDN URLs
const latestState = mediaService.getUnifiedMediaState(sku, product.shopify_id);
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
@ -143,22 +162,61 @@ export function saveMediaChanges(sku: string, finalState: any[], jobId: string |
.setAltTextDescription(`Thumbnail for ${sku}`)
.build();
ss.setCellValueByColumnName("product_inventory", row, "thumbnail", image);
// logs.push(`Updated sheet thumbnail for SKU ${sku}`); // Logs array is static now, won't stream this unless we refactor sheet update to use log() too. User cares mostly about main process.
} 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}`);
throw new Error("Sheet Update Failed: " + e.message);
}
}
export function getMediaSavePlan(sku: string, finalState: any[]) {
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)
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);
}
return logs
if (!product.shopify_id) {
throw new Error("Product must be synced to Shopify before saving media changes.")
}
return mediaService.calculatePlan(sku, finalState, product.shopify_id);
}
export function executeSavePhase(sku: string, phase: string, planData: any, jobId: string | null = null) {
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)
const product = new Product(sku)
try {
product.MatchToShopifyProduct(shop);
} catch (e) {
console.warn("MatchToShopifyProduct failed", e);
}
if (!product.shopify_id) {
throw new Error("Product must be synced to Shopify before saving media changes.")
}
return mediaService.executeSavePhase(sku, phase, planData, product.shopify_id, jobId);
}
export function pollJobLogs(jobId: string): string[] {