coupon save on order
This commit is contained in:
@@ -18,11 +18,12 @@ import { Delivery } from '../../delivery/entities/delivery.entity';
|
|||||||
import { DeliveryMethodEnum } from '../../delivery/interface/delivery';
|
import { DeliveryMethodEnum } from '../../delivery/interface/delivery';
|
||||||
import { Cart, CartItem } from '../interfaces/cart.interface';
|
import { Cart, CartItem } from '../interfaces/cart.interface';
|
||||||
import { CouponService } from 'src/modules/coupons/providers/coupon.service';
|
import { CouponService } from 'src/modules/coupons/providers/coupon.service';
|
||||||
import { OrderCouponDetail } from 'src/modules/orders/interface/order.interface';
|
import { OrderCouponDetail, OrderStatus } from 'src/modules/orders/interface/order.interface';
|
||||||
import { CouponType } from 'src/modules/coupons/interface/coupon';
|
import { CouponType } from 'src/modules/coupons/interface/coupon';
|
||||||
import { UserWallet } from 'src/modules/users/entities/user-wallet.entity';
|
import { UserWallet } from 'src/modules/users/entities/user-wallet.entity';
|
||||||
import { PaymentMethodEnum } from 'src/modules/payments/interface/payment';
|
import { PaymentMethodEnum } from 'src/modules/payments/interface/payment';
|
||||||
import { SetAllCartParmsDto } from '../dto/set-all-cart-params.dto';
|
import { SetAllCartParmsDto } from '../dto/set-all-cart-params.dto';
|
||||||
|
import { Order } from 'src/modules/orders/entities/order.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CartService {
|
export class CartService {
|
||||||
@@ -817,18 +818,25 @@ export class CartService {
|
|||||||
private async assertCouponUsageLimit(userId: string, couponId: string, maxUsesPerUser?: number): Promise<void> {
|
private async assertCouponUsageLimit(userId: string, couponId: string, maxUsesPerUser?: number): Promise<void> {
|
||||||
if (!maxUsesPerUser) return;
|
if (!maxUsesPerUser) return;
|
||||||
|
|
||||||
const knex = this.em.getKnex();
|
// const knex = this.em.getKnex();
|
||||||
const result = await knex.raw(
|
// const result = await knex.raw(
|
||||||
`SELECT COUNT(*)::int as count
|
// `SELECT COUNT(*)::int as count
|
||||||
FROM orders
|
// FROM orders
|
||||||
WHERE user_id = ?
|
// WHERE user_id = ?
|
||||||
AND coupon_detail IS NOT NULL
|
// AND coupon_detail IS NOT NULL
|
||||||
AND coupon_detail->>'couponId' = ?
|
// AND coupon_detail->>'couponId' = ?
|
||||||
AND deleted_at IS NULL`,
|
// AND deleted_at IS NULL`,
|
||||||
[userId, couponId],
|
// [userId, couponId],
|
||||||
);
|
// );
|
||||||
const userCouponUsageCount = result.rows?.[0]?.count ?? result[0]?.count ?? 0;
|
const ordersThatUSedTheCouponCount = await this.em.count(Order, {
|
||||||
if (userCouponUsageCount >= maxUsesPerUser) {
|
user: { id: userId },
|
||||||
|
couponDetail: { couponId: couponId },
|
||||||
|
status: { $ne: OrderStatus.CANCELED },
|
||||||
|
deletedAt: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('ordersThatUSedTheCouponCount', ordersThatUSedTheCouponCount);
|
||||||
|
if (ordersThatUSedTheCouponCount >= maxUsesPerUser) {
|
||||||
throw new BadRequestException(`You have reached the maximum number of uses (${maxUsesPerUser}) for this coupon`);
|
throw new BadRequestException(`You have reached the maximum number of uses (${maxUsesPerUser}) for this coupon`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export class OrdersService {
|
|||||||
carAddress: validated.carAddress,
|
carAddress: validated.carAddress,
|
||||||
paymentMethod: validated.paymentMethod,
|
paymentMethod: validated.paymentMethod,
|
||||||
couponDiscount: cart.couponDiscount || 0,
|
couponDiscount: cart.couponDiscount || 0,
|
||||||
|
couponDetail: cart.coupon,
|
||||||
itemsDiscount: cart.itemsDiscount || 0,
|
itemsDiscount: cart.itemsDiscount || 0,
|
||||||
totalDiscount: cart.totalDiscount || 0,
|
totalDiscount: cart.totalDiscount || 0,
|
||||||
subTotal: cart.subTotal || 0,
|
subTotal: cart.subTotal || 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user