feat(inventory): implement numeric stock inventory across fullstack and improve prescription UX handoff
This commit is contained in:
1 parent
e9582430e6
commit
741365f04c
13 files changed
+177
-47
No files matched your search
@@ -61,23 +61,26 @@ export const useCartStore = create<CartStore>()(
|
||||
orders: [],
|
||||
addItem: (product, quantity, dose) => {
|
||||
// Prevent adding out-of-stock products
|
||||
const maxStock = typeof product.stock === 'number' ? product.stock : (product.stockStatus === 'OUT_OF_STOCK' ? 0 : 999);
|
||||
const isOutOfStock =
|
||||
product.stockStatus === 'OUT_OF_STOCK' ||
|
||||
product.stockStatus === 'DISCONTINUED' ||
|
||||
(typeof product.packageSize === 'number' && product.packageSize <= 0);
|
||||
maxStock <= 0;
|
||||
if (isOutOfStock || quantity <= 0) {
|
||||
return;
|
||||
}
|
||||
const items = get().items;
|
||||
const existing = items.find((i) => i.product.id === product.id);
|
||||
if (existing) {
|
||||
const newQty = Math.min(existing.quantity + quantity, maxStock);
|
||||
set({
|
||||
items: items.map((i) =>
|
||||
i.product.id === product.id ? { ...i, quantity: i.quantity + quantity } : i
|
||||
i.product.id === product.id ? { ...i, quantity: newQty } : i
|
||||
),
|
||||
});
|
||||
} else {
|
||||
set({ items: [...items, { product, quantity, calculatedDose: dose }] });
|
||||
const initialQty = Math.min(quantity, maxStock);
|
||||
set({ items: [...items, { product, quantity: initialQty, calculatedDose: dose }] });
|
||||
}
|
||||
},
|
||||
removeItem: (productId) => {
|
||||
@@ -88,9 +91,12 @@ export const useCartStore = create<CartStore>()(
|
||||
get().removeItem(productId);
|
||||
return;
|
||||
}
|
||||
const currentItem = get().items.find((i) => i.product.id === productId);
|
||||
const maxStock = currentItem && typeof currentItem.product.stock === 'number' ? currentItem.product.stock : 999;
|
||||
const boundedQty = Math.min(quantity, maxStock);
|
||||
set({
|
||||
items: get().items.map((i) =>
|
||||
i.product.id === productId ? { ...i, quantity } : i
|
||||
i.product.id === productId ? { ...i, quantity: boundedQty } : i
|
||||
),
|
||||
});
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user