generate code

This commit is contained in:
2025-12-06 16:40:26 +03:30
parent 7440d4db90
commit 8768cd9961
3 changed files with 19 additions and 3 deletions
@@ -8,6 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData } from '@mikro-orm/core';
import { Coupon, CouponType } from '../entities/coupon.entity';
import { CouponMessage, RestMessage } from 'src/common/enums/message.enum';
import { randomBytes } from 'node:crypto';
@Injectable()
export class CouponService {
@@ -250,4 +251,13 @@ export class CouponService {
await this.em.persistAndFlush(coupon);
}
}
async generateCouponCode(restId: string): Promise<string> {
const code = randomBytes(8).toString('hex');
const existingCoupon = await this.couponRepository.findOne({ code, restaurant: { id: restId } });
if (existingCoupon) {
return this.generateCouponCode(restId);
}
return code;
}
}