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> {
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<string> {
const code = this.generateShortCode(8);
const existing = await this.couponRepository.findOne({ code, restaurant: { id: restId } });
if (existing) return this.generateCouponCode(restId);
return code;
}
}