fix bug in coupon
This commit is contained in:
@@ -19,8 +19,8 @@ export class CouponService {
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(restId: string, createCouponDto: CreateCouponDto): Promise<Coupon> {
|
||||
const shop = await this.shopRepository.findOne({ id: restId });
|
||||
async create(shopId: string, createCouponDto: CreateCouponDto): Promise<Coupon> {
|
||||
const shop = await this.shopRepository.findOne({ id: shopId });
|
||||
if (!shop) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
@@ -76,8 +76,8 @@ export class CouponService {
|
||||
return coupon;
|
||||
}
|
||||
|
||||
findAll(restId: string, dto: FindCouponsDto) {
|
||||
return this.couponRepository.findAllPaginated(restId, dto);
|
||||
findAll(shopId: string, dto: FindCouponsDto) {
|
||||
return this.couponRepository.findAllPaginated(shopId, dto);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Coupon> {
|
||||
@@ -88,8 +88,8 @@ export class CouponService {
|
||||
return coupon;
|
||||
}
|
||||
|
||||
async findByCode(code: string, restId: string): Promise<Coupon> {
|
||||
const coupon = await this.couponRepository.findOne({ code, shop: { id: restId } });
|
||||
async findByCode(code: string, shopId: string): Promise<Coupon> {
|
||||
const coupon = await this.couponRepository.findOne({ code, shop: { id: shopId } });
|
||||
if (!coupon) {
|
||||
throw new NotFoundException(CouponMessage.NOT_FOUND);
|
||||
}
|
||||
@@ -160,7 +160,7 @@ export class CouponService {
|
||||
*/
|
||||
async validateCoupon(
|
||||
code: string,
|
||||
restId: string,
|
||||
shopId: string,
|
||||
orderAmount: number,
|
||||
userPhone: string,
|
||||
): Promise<{
|
||||
@@ -169,7 +169,7 @@ export class CouponService {
|
||||
message?: string;
|
||||
}> {
|
||||
const coupon = await this.couponRepository.findOne(
|
||||
{ code, shop: { id: restId } },
|
||||
{ code, shop: { id: shopId } },
|
||||
{ populate: ['shop'] },
|
||||
);
|
||||
|
||||
@@ -245,7 +245,7 @@ export class CouponService {
|
||||
}
|
||||
}
|
||||
|
||||
generateShortCode(restaurantSlug: string, length = 5) {
|
||||
generateShortCode(shopSlug: string, length = 5) {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const bytes = randomBytes(length);
|
||||
let result = '';
|
||||
@@ -253,19 +253,19 @@ export class CouponService {
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
result += chars[bytes[i] % chars.length];
|
||||
}
|
||||
const prefix = restaurantSlug.slice(0, 2).toUpperCase();
|
||||
const prefix = shopSlug.slice(0, 2).toUpperCase();
|
||||
return prefix + '-' + result;
|
||||
}
|
||||
|
||||
async generateCouponCode(restId: string): Promise<string> {
|
||||
const shop = await this.shopRepository.findOne({ id: restId });
|
||||
async generateCouponCode(shopId: string): Promise<{ code: string }> {
|
||||
const shop = await this.shopRepository.findOne({ id: shopId });
|
||||
if (!shop) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
const code = this.generateShortCode(shop.slug);
|
||||
const existing = await this.couponRepository.findOne({ code, shop: { id: restId } });
|
||||
const existing = await this.couponRepository.findOne({ code, shop: { id: shopId } });
|
||||
|
||||
if (existing) return this.generateCouponCode(restId);
|
||||
return code;
|
||||
if (existing) return this.generateCouponCode(shopId);
|
||||
return { code }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user