From cae22ff7c94fc156223dff130b5e7bf383793bab Mon Sep 17 00:00:00 2001 From: devops Date: Mon, 12 Oct 2026 18:42:47 +0000 Subject: [PATCH] fix(inventory): enforce out-of-stock validation in cartStore, ordersService, and JSON-LD availability --- backend/src/orders/orders.service.ts | 12 +++++++++++- frontend/application/app/shop/[slug]/page.tsx | 12 ++++++++---- frontend/application/lib/store/cartStore.ts | 8 ++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/backend/src/orders/orders.service.ts b/backend/src/orders/orders.service.ts index 8b73efee..c14d644e 100644 --- a/backend/src/orders/orders.service.ts +++ b/backend/src/orders/orders.service.ts @@ -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); diff --git a/frontend/application/app/shop/[slug]/page.tsx b/frontend/application/app/shop/[slug]/page.tsx index 62931fff..f9263e34 100644 --- a/frontend/application/app/shop/[slug]/page.tsx +++ b/frontend/application/app/shop/[slug]/page.tsx @@ -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 diff --git a/frontend/application/lib/store/cartStore.ts b/frontend/application/lib/store/cartStore.ts index 68c2c591..62a0b293 100644 --- a/frontend/application/lib/store/cartStore.ts +++ b/frontend/application/lib/store/cartStore.ts @@ -60,6 +60,14 @@ export const useCartStore = create()( 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) {