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
@@ -141,18 +141,23 @@ export class OrdersService {
|
||||
|
||||
const productMap = new Map(products.map((p) => [p.id, p]));
|
||||
|
||||
for (const productId of productIds) {
|
||||
const product = productMap.get(productId);
|
||||
for (const item of createOrderDto.items) {
|
||||
const product = productMap.get(item.productId);
|
||||
if (!product) {
|
||||
throw new NotFoundException(`محصول یافت نشد`);
|
||||
}
|
||||
const isOutOfStock =
|
||||
product.stockStatus === 'OUT_OF_STOCK' ||
|
||||
product.stockStatus === 'DISCONTINUED' ||
|
||||
(product.packageSize !== null && Number(product.packageSize) <= 0);
|
||||
product.stock <= 0;
|
||||
if (isOutOfStock) {
|
||||
throw new BadRequestException(
|
||||
`محصول «${product.nameFa || product.name}» در حال حاضر ناموجود است و امکان ثبت سفارش آن وجود ندارد`,
|
||||
`محصول «${product.nameFa || (product as any).name}» در حال حاضر ناموجود است و امکان ثبت سفارش آن وجود ندارد`,
|
||||
);
|
||||
}
|
||||
if (item.quantity > product.stock) {
|
||||
throw new BadRequestException(
|
||||
`تعداد درخواستی برای محصول «${product.nameFa || (product as any).name}» (${item.quantity} عدد) بیشتر از موجودی انبار (${product.stock} عدد) است`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -272,6 +277,22 @@ export class OrdersService {
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
// Deduct inventory atomically for wallet orders
|
||||
for (const item of createOrderDto.items) {
|
||||
const updated = await tx.product.update({
|
||||
where: { id: item.productId },
|
||||
data: {
|
||||
stock: { decrement: item.quantity },
|
||||
},
|
||||
});
|
||||
if (updated.stock <= 0) {
|
||||
await tx.product.update({
|
||||
where: { id: item.productId },
|
||||
data: { stockStatus: 'OUT_OF_STOCK' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return newOrder;
|
||||
});
|
||||
|
||||
@@ -289,27 +310,49 @@ export class OrdersService {
|
||||
const isOnline = createOrderDto.paymentMethod === 'online';
|
||||
const initialStatus = isOnline ? 'pending_payment' : 'processing';
|
||||
|
||||
const createdOrder = await this.prisma.order.create({
|
||||
data: {
|
||||
userId,
|
||||
couponId,
|
||||
totalAmount: Number(finalAmount),
|
||||
charityDonation: Number(charityAmount),
|
||||
isRefill: Boolean(createOrderDto.isRefill),
|
||||
refillIntervalDays: createOrderDto.refillIntervalDays || 60,
|
||||
trackingNumber,
|
||||
status: initialStatus,
|
||||
paymentMethod: createOrderDto.paymentMethod || 'card',
|
||||
shippingAddress: createOrderDto.shippingAddress,
|
||||
orderItems: {
|
||||
create: orderItemsData,
|
||||
const createdOrder = await this.prisma.$transaction(async (tx) => {
|
||||
const order = await tx.order.create({
|
||||
data: {
|
||||
userId,
|
||||
couponId,
|
||||
totalAmount: Number(finalAmount),
|
||||
charityDonation: Number(charityAmount),
|
||||
isRefill: Boolean(createOrderDto.isRefill),
|
||||
refillIntervalDays: createOrderDto.refillIntervalDays || 60,
|
||||
trackingNumber,
|
||||
status: initialStatus,
|
||||
paymentMethod: createOrderDto.paymentMethod || 'card',
|
||||
shippingAddress: createOrderDto.shippingAddress,
|
||||
orderItems: {
|
||||
create: orderItemsData,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true },
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Deduct inventory immediately for non-online orders (card to card)
|
||||
if (!isOnline) {
|
||||
for (const item of createOrderDto.items) {
|
||||
const updated = await tx.product.update({
|
||||
where: { id: item.productId },
|
||||
data: {
|
||||
stock: { decrement: item.quantity },
|
||||
},
|
||||
});
|
||||
if (updated.stock <= 0) {
|
||||
await tx.product.update({
|
||||
where: { id: item.productId },
|
||||
data: { stockStatus: 'OUT_OF_STOCK' },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return order;
|
||||
});
|
||||
|
||||
// Send Order Confirmation SMS (for card-to-card or non-online orders, online orders get SMS on verify)
|
||||
|
||||
Reference in new issue
Block a user