fix on shelf

This commit is contained in:
2026-08-31 15:50:59 +05:30
parent b4e77fd6ea
commit 55ed13c284
6 changed files with 167 additions and 9 deletions

View File

@@ -34,6 +34,13 @@ export interface ShelveResult {
skipped: number;
/** Kept so the caller can say which ones, and why. */
plan: StockPlan;
/**
* Brands the catalogue could not be read for, if any.
*
* Named rather than counted, because the answer is always "ask about this
* brand" and a number does not say which.
*/
failedBrands: string[];
}
/**
@@ -57,11 +64,23 @@ export async function shelveBatch(
// One catalogue read per BRAND rather than per product. A 500-row sheet would
// otherwise open 500 requests from a shop's connection.
//
// A brand that cannot be read does NOT fail the batch. It used to: a sheet of
// twenty products naming one brand the catalogue could not resolve threw on
// the first lookup and shelved nothing, so nineteen products the shop was
// entitled to sell stayed unpriced because of the twentieth. Its products now
// fall through to `unresolved`, which is already reported product by product,
// and the brand is named so the cause is not left to guesswork.
const brands = [...new Set(plan.matched.map((entry) => entry.product.brand))];
const catalogueIds = new Map<string, number>();
const failedBrands: string[] = [];
for (const brand of brands) {
for (const [imageId, id] of await catalogueApi.idsByImageId(brand)) {
catalogueIds.set(imageId, id);
try {
for (const [imageId, id] of await catalogueApi.idsByImageId(brand)) {
catalogueIds.set(imageId, id);
}
} catch {
failedBrands.push(brand);
}
}
@@ -77,5 +96,10 @@ export async function shelveBatch(
// the stock ledger entry together.
if (requests.length > 0) await productsApi.importFromCatalogue(requests);
return { shelved: requests.length, skipped: unresolved.length + unpriced.length, plan };
return {
shelved: requests.length,
skipped: unresolved.length + unpriced.length,
plan,
failedBrands,
};
}