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

@@ -11,7 +11,7 @@ import { TextInput } from '@astryxdesign/core/TextInput';
import { Token } from '@astryxdesign/core/Token';
import { VStack } from '@astryxdesign/core/VStack';
import { Funnel, PackageSearch, Search, SearchX } from 'lucide-react';
import { catalogueKey } from '@/api/catalogue';
import { catalogueKey, catalogueKeysOf } from '@/api/catalogue';
import { productsApi } from '@/api/products';
import type { CatalogueProduct, ImportCatalogueProductRequest } from '@/api/types';
import { queryKeys } from '@/queries/keys';
@@ -188,7 +188,10 @@ export function CatalogueBrowser({
const knownTotal = !category && !debounced ? brandTotal : undefined;
const importedKeys = useMemo(() => {
const set = new Set((imported.data ?? []).map(catalogueKey));
// Both keys per ref, not one. During the changeover a ref can carry the
// stable key it has just acquired AND the id it was imported under, and a
// product that has not been relinked yet is still genuinely imported.
const set = new Set((imported.data ?? []).flatMap(catalogueKeysOf));
for (const key of justImported) set.add(key);
return set;
}, [imported.data, justImported]);

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,
};
}

View File

@@ -417,9 +417,17 @@ function ShelveAction({
skipped: result.skipped,
});
setNote(
result.skipped > 0
? `${result.shelved} on the shelf. ${result.skipped} were left out — the sheet did not price them.`
: `${result.shelved} priced, shelved and stocked.`,
[
`${result.shelved} priced, shelved and stocked.`,
result.skipped > 0 ? `${result.skipped} were left out.` : "",
// Named, because "left out" alone sends someone looking at their
// spreadsheet for a problem that is in the catalogue.
result.failedBrands.length > 0
? `The catalogue could not be read for: ${result.failedBrands.join(", ")}.`
: "",
]
.filter(Boolean)
.join(" "),
);
onDone();
} catch (cause) {