Merge pull request #34 from Danakcorp/mrtz

fix : cart total_discount
This commit is contained in:
morteza-mortezai
2025-10-28 17:27:37 +03:30
committed by GitHub
3 changed files with 10 additions and 5 deletions
+1 -1
View File
@@ -209,7 +209,7 @@ class CouponService {
cartShipmentItem.totalCouponDiscount = discount; cartShipmentItem.totalCouponDiscount = discount;
cartShipmentItem.totalPaymentPrice = cartShipmentItem.totalPaymentPrice - discount; cartShipmentItem.totalPaymentPrice = cartShipmentItem.totalPaymentPrice - discount;
userCart.coupon_discount = discount; userCart.coupon_discount = discount;
userCart.total_discount += discount;
coupon.usageCount += 1; coupon.usageCount += 1;
await validCartShipmentItem.save({ session }); await validCartShipmentItem.save({ session });
+5
View File
@@ -310,11 +310,16 @@ class CartRepository extends BaseRepository<ICart> {
}, },
total_discount: { total_discount: {
$sum: { $sum: {
$add: [
{
$subtract: [ $subtract: [
{ $multiply: ["$items.quantity", "$variantDetails.price.retailPrice"] }, { $multiply: ["$items.quantity", "$variantDetails.price.retailPrice"] },
{ $multiply: ["$items.quantity", "$variantDetails.price.selling_price"] }, { $multiply: ["$items.quantity", "$variantDetails.price.selling_price"] },
], ],
}, },
{ $divide: ["$coupon_discount", { $literal: 1 }] }, // safely adds coupon_discount
],
},
}, },
coupon_discount: { $first: "$coupon_discount" }, coupon_discount: { $first: "$coupon_discount" },
isWholeSale: { $first: "$variantDetails.isWholeSale" }, isWholeSale: { $first: "$variantDetails.isWholeSale" },
+1 -1
View File
@@ -25,8 +25,8 @@ class CartService {
async getCartS(userId: string) { async getCartS(userId: string) {
const doc = (await this.cartRepo.getCartWithAggregate(userId))[0] as ICart; const doc = (await this.cartRepo.getCartWithAggregate(userId))[0] as ICart;
const cart = CartDTO.transformCart(doc); const cart = CartDTO.transformCart(doc);
let cartShipmentItems; let cartShipmentItems;
if (doc?._id) { if (doc?._id) {
cartShipmentItems = await this.cartShipItemRepo.getCartShipmentItems(doc._id.toString()); cartShipmentItems = await this.cartShipItemRepo.getCartShipmentItems(doc._id.toString());
} }