cart and coupon
This commit is contained in:
@@ -3,7 +3,6 @@ import { CouponService } from '../providers/coupon.service';
|
||||
import { CreateCouponDto } from '../dto/create-coupon.dto';
|
||||
import { UpdateCouponDto } from '../dto/update-coupon.dto';
|
||||
import { FindCouponsDto } from '../dto/find-coupons.dto';
|
||||
import { ValidateCouponDto } from '../dto/validate-coupon.dto';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
@@ -24,20 +23,6 @@ import { RestId } from 'src/common/decorators';
|
||||
export class CouponController {
|
||||
constructor(private readonly couponService: CouponService) {}
|
||||
|
||||
@Post('public/coupons/validate')
|
||||
@ApiOperation({ summary: 'Validate a coupon code' })
|
||||
@ApiBody({ type: ValidateCouponDto })
|
||||
@ApiOkResponse({ description: 'Coupon validation result' })
|
||||
@ApiNotFoundResponse({ description: 'Coupon not found or invalid' })
|
||||
validateCoupon(@Body() validateCouponDto: ValidateCouponDto, @RestId() restId: string) {
|
||||
return this.couponService.validateCoupon(
|
||||
validateCouponDto.code,
|
||||
restId,
|
||||
validateCouponDto.orderAmount || 0,
|
||||
validateCouponDto.userId,
|
||||
);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Post('admin/coupons')
|
||||
@@ -96,8 +81,6 @@ export class CouponController {
|
||||
return this.couponService.remove(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Get('admin/coupons/generate-code')
|
||||
@ApiOperation({ summary: 'generate a coupon code' })
|
||||
generateCouponCode(@RestId() restId: string) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user