up
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user