This commit is contained in:
2025-12-06 22:20:28 +03:30
parent f36125764d
commit 3978065400
@@ -235,12 +235,23 @@ export class CouponService {
} }
} }
async generateCouponCode(restId: string): Promise<string> { generateShortCode(length = 8) {
const code = randomBytes(6).toString('hex'); const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const existingCoupon = await this.couponRepository.findOne({ code, restaurant: { id: restId } }); const bytes = randomBytes(length);
if (existingCoupon) { let result = '';
return this.generateCouponCode(restId);
for (let i = 0; i < bytes.length; i++) {
result += chars[bytes[i] % chars.length];
} }
return result;
}
async generateCouponCode(restId: string): Promise<string> {
const code = this.generateShortCode(8);
const existing = await this.couponRepository.findOne({ code, restaurant: { id: restId } });
if (existing) return this.generateCouponCode(restId);
return code; return code;
} }
} }