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