diff --git a/src/modules/coupons/providers/coupon.service.ts b/src/modules/coupons/providers/coupon.service.ts index 645875a..fc88f09 100644 --- a/src/modules/coupons/providers/coupon.service.ts +++ b/src/modules/coupons/providers/coupon.service.ts @@ -235,12 +235,23 @@ export class CouponService { } } - async generateCouponCode(restId: string): Promise { - const code = randomBytes(6).toString('hex'); - const existingCoupon = await this.couponRepository.findOne({ code, restaurant: { id: restId } }); - if (existingCoupon) { - return this.generateCouponCode(restId); + generateShortCode(length = 8) { + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + const bytes = randomBytes(length); + let result = ''; + + for (let i = 0; i < bytes.length; i++) { + result += chars[bytes[i] % chars.length]; } + + return result; + } + + async generateCouponCode(restId: string): Promise { + const code = this.generateShortCode(8); + const existing = await this.couponRepository.findOne({ code, restaurant: { id: restId } }); + + if (existing) return this.generateCouponCode(restId); return code; } }