cart and coupon

This commit is contained in:
2025-12-06 20:26:37 +03:30
parent a7d28ab882
commit e342c00e25
9 changed files with 114 additions and 122 deletions
@@ -154,12 +154,10 @@ export class CouponService {
async validateCoupon(
code: string,
restId: string,
orderAmount: number = 0,
userId?: string,
orderAmount: number,
): Promise<{
valid: boolean;
coupon?: Coupon;
discount: number;
message?: string;
}> {
const coupon = await this.couponRepository.findOne(
@@ -170,7 +168,6 @@ export class CouponService {
if (!coupon) {
return {
valid: false,
discount: 0,
message: CouponMessage.NOT_FOUND,
};
}
@@ -179,7 +176,6 @@ export class CouponService {
if (!coupon.isActive) {
return {
valid: false,
discount: 0,
message: CouponMessage.COUPON_INACTIVE,
};
}
@@ -189,7 +185,6 @@ export class CouponService {
if (coupon.startDate && now < coupon.startDate) {
return {
valid: false,
discount: 0,
message: CouponMessage.COUPON_NOT_STARTED,
};
}
@@ -197,7 +192,6 @@ export class CouponService {
if (coupon.endDate && now > coupon.endDate) {
return {
valid: false,
discount: 0,
message: CouponMessage.COUPON_EXPIRED,
};
}
@@ -206,7 +200,6 @@ export class CouponService {
if (coupon.maxUses && coupon.usedCount >= coupon.maxUses) {
return {
valid: false,
discount: 0,
message: CouponMessage.COUPON_MAX_USES_REACHED,
};
}
@@ -215,29 +208,13 @@ export class CouponService {
if (coupon.minOrderAmount && orderAmount < coupon.minOrderAmount) {
return {
valid: false,
discount: 0,
message: CouponMessage.MIN_ORDER_AMOUNT_NOT_MET,
};
}
// Calculate discount
let discount = 0;
if (coupon.type === CouponType.PERCENTAGE) {
discount = (orderAmount * coupon.value) / 100;
if (coupon.maxDiscount && discount > coupon.maxDiscount) {
discount = coupon.maxDiscount;
}
} else if (coupon.type === CouponType.FIXED) {
discount = coupon.value;
if (discount > orderAmount) {
discount = orderAmount;
}
}
return {
valid: true,
coupon,
discount: Math.round(discount),
};
}
@@ -253,7 +230,7 @@ export class CouponService {
}
async generateCouponCode(restId: string): Promise<string> {
const code = randomBytes(8).toString('hex');
const code = randomBytes(6).toString('hex');
const existingCoupon = await this.couponRepository.findOne({ code, restaurant: { id: restId } });
if (existingCoupon) {
return this.generateCouponCode(restId);