fix: switcher respects commercetools inventory settings#245
Conversation
| } | ||
| stateRef.current.expectedServerQuantity = newQuantity | ||
| const clampedQuantity = | ||
| max !== undefined ? Math.min(newQuantity, max) : newQuantity |
There was a problem hiding this comment.
max !== undefined ? Math.min(v, max) : v is repeated at lines 62–63, 116–117, and 129. Maybe would be good to extract it as shared function to change it once later if clamping policy changes?
| const validatedCart = CartApiResponseSchema.parse(responseBody) | ||
| const mappedCart = mapCartToResponse(validatedCart, currentLanguage) | ||
| return CartResponseSchema.parse(mappedCart) | ||
| const enrichedCart = await this.enrichWithInventory(mappedCart) |
There was a problem hiding this comment.
processCartResponse always awaits enrichWithInventory, so every cart op pays for an inventory query even though maxQuantity is only read by the quantity switcher on /cart (checkout uses CartItemCompact with preview={true}, which shows static text and never touches it). So getActiveCart on page loads and the address/shipping mutations each fire a wasted inventory().get(), and a quantity change becomes three serialized round-trips (getCartVersion GET → cart POST → inventory GET).
Could we only enrich where the switcher consumes it? getCart + the line-item mutations (addToCart/updateCartItem/removeCartItem) need it since their responses land in the shared ['cart'] cache and re-render the switcher. setBillingAddress/setShippingAddress/setShippingMethod/getActiveCart can skip it — the switcher is hidden in checkout and useCart has staleTime: 0, so /cart refetches an enriched getCart on return. createCart is a no-op anyway.
|
|
||
| return { | ||
| ...item, | ||
| maxQuantity: Math.max(item.quantity, availableQuantity), |
There was a problem hiding this comment.
When current cart quantity already exceeds availability, max snaps up to the cart quantity, so the field stops meaning "available stock."
| } | ||
| } | ||
|
|
||
| private buildInventoryWhereClause(skus: string[]): string { |
There was a problem hiding this comment.
buildInventoryWhereClause + escapeWhereStringValue re-implement the field in ("a","b") predicate that already exists at integrations/commercetools-api/src/helpers/fetch-projections.ts:25:
const wherePredicate = `id in (${ids.map((id) => `"${id}"`).join(',')})`Failure scenario: three independent in (...) builders now exist (this one, fetch-projections.ts:25, wishlist.ts:42) and only the new one escapes quotes/backslashes. A SKU/id containing " breaks the others but works here. The fix exists but isn't reachable from the other call sites. Extract a shared buildInClause(field, values) (with escaping) into src/helpers/ and call it from all three.
No description provided.