diff --git a/src/modules/cart/providers/cart.service.ts b/src/modules/cart/providers/cart.service.ts index 464f5e2..d6cb382 100644 --- a/src/modules/cart/providers/cart.service.ts +++ b/src/modules/cart/providers/cart.service.ts @@ -19,6 +19,7 @@ import { Cart, CartItem } from '../interfaces/cart.interface'; import { CouponService } from 'src/modules/coupons/providers/coupon.service'; import { OrderCouponDetail } from 'src/modules/orders/interface/order-status'; import { CouponType } from 'src/modules/coupons/interface/coupon'; +import { Order } from '../../orders/entities/order.entity'; @Injectable() export class CartService { @@ -399,17 +400,19 @@ export class CartService { // Check maxUsesPerUser limit by counting how many orders this user has with this coupon if (coupon.maxUsesPerUser) { - const result = await this.em.getConnection().execute<{ count: string }>( - `SELECT COUNT(*) as count + // Use native query for JSONB field access with proper parameter binding + const knex = this.em.getKnex(); + const result = await knex.raw( + `SELECT COUNT(*)::int as count FROM orders - WHERE user_id = $1 + WHERE user_id = ? AND coupon_detail IS NOT NULL - AND coupon_detail->>'couponId' = $2 + AND coupon_detail->>'couponId' = ? AND deleted_at IS NULL`, [userId, coupon.id], ); - const userCouponUsageCount = parseInt((result[0] as { count: string })?.count || '0', 10); + const userCouponUsageCount = result.rows?.[0]?.count ?? result[0]?.count ?? 0; if (userCouponUsageCount >= coupon.maxUsesPerUser) { throw new BadRequestException( @@ -594,11 +597,7 @@ export class CartService { /** * Set description for cart */ - async setDescription( - userId: string, - restaurantId: string, - setDescriptionDto: SetDescriptionDto, - ): Promise { + async setDescription(userId: string, restaurantId: string, setDescriptionDto: SetDescriptionDto): Promise { const cart = await this.findOneOrFail(userId, restaurantId); cart.description = setDescriptionDto.description;