fix(inventory): enforce out-of-stock validation in cartStore, ordersService, and JSON-LD availability
This commit is contained in:
1 parent
11dd549c7c
commit
cae22ff7c9
3 files changed
+27
-5
No files matched your search
@@ -142,9 +142,19 @@ export class OrdersService {
|
||||
const productMap = new Map(products.map((p) => [p.id, p]));
|
||||
|
||||
for (const productId of productIds) {
|
||||
if (!productMap.has(productId)) {
|
||||
const product = productMap.get(productId);
|
||||
if (!product) {
|
||||
throw new NotFoundException(`محصول یافت نشد`);
|
||||
}
|
||||
const isOutOfStock =
|
||||
product.stockStatus === 'OUT_OF_STOCK' ||
|
||||
product.stockStatus === 'DISCONTINUED' ||
|
||||
(product.packageSize !== null && Number(product.packageSize) <= 0);
|
||||
if (isOutOfStock) {
|
||||
throw new BadRequestException(
|
||||
`محصول «${product.nameFa || product.name}» در حال حاضر ناموجود است و امکان ثبت سفارش آن وجود ندارد`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let cartTotal = new Prisma.Decimal(0);
|
||||
|
||||
@@ -122,14 +122,18 @@ export default async function ShopProductPage({ params }: { params: Promise<{ sl
|
||||
const productUrl = `${siteUrl}/shop/${resolvedParams.slug}`;
|
||||
const priceInRial = product.priceValue * 10;
|
||||
|
||||
// Map stock status to Schema.org availability
|
||||
// Map stock status and packageSize to Schema.org availability
|
||||
const isOutOfStock =
|
||||
product.stockStatus === 'OUT_OF_STOCK' ||
|
||||
(typeof product.packageSize === 'number' && product.packageSize <= 0);
|
||||
|
||||
let availabilityUrl = 'https://schema.org/InStock';
|
||||
if (product.stockStatus === 'OUT_OF_STOCK') {
|
||||
if (product.stockStatus === 'DISCONTINUED') {
|
||||
availabilityUrl = 'https://schema.org/Discontinued';
|
||||
} else if (isOutOfStock) {
|
||||
availabilityUrl = 'https://schema.org/OutOfStock';
|
||||
} else if (product.stockStatus === 'PRE_ORDER') {
|
||||
availabilityUrl = 'https://schema.org/PreOrder';
|
||||
} else if (product.stockStatus === 'DISCONTINUED') {
|
||||
availabilityUrl = 'https://schema.org/Discontinued';
|
||||
}
|
||||
|
||||
// Collect unique images
|
||||
|
||||
@@ -60,6 +60,14 @@ export const useCartStore = create<CartStore>()(
|
||||
coupon: null,
|
||||
orders: [],
|
||||
addItem: (product, quantity, dose) => {
|
||||
// Prevent adding out-of-stock products
|
||||
const isOutOfStock =
|
||||
product.stockStatus === 'OUT_OF_STOCK' ||
|
||||
product.stockStatus === 'DISCONTINUED' ||
|
||||
(typeof product.packageSize === 'number' && product.packageSize <= 0);
|
||||
if (isOutOfStock || quantity <= 0) {
|
||||
return;
|
||||
}
|
||||
const items = get().items;
|
||||
const existing = items.find((i) => i.product.id === product.id);
|
||||
if (existing) {
|
||||
|
||||
Reference in new issue
Block a user