normalize phone
This commit is contained in:
@@ -235,7 +235,7 @@ export class CouponService {
|
||||
}
|
||||
}
|
||||
|
||||
generateShortCode(length = 8) {
|
||||
generateShortCode(restaurantSlug: string, length = 5) {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const bytes = randomBytes(length);
|
||||
let result = '';
|
||||
@@ -243,12 +243,16 @@ export class CouponService {
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
result += chars[bytes[i] % chars.length];
|
||||
}
|
||||
|
||||
return result;
|
||||
const prefix = restaurantSlug.slice(0, 2);
|
||||
return prefix + '-' + result;
|
||||
}
|
||||
|
||||
async generateCouponCode(restId: string): Promise<string> {
|
||||
const code = this.generateShortCode(8);
|
||||
const restaurant = await this.restRepository.findOne({ id: restId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
const code = this.generateShortCode(restaurant.slug);
|
||||
const existing = await this.couponRepository.findOne({ code, restaurant: { id: restId } });
|
||||
|
||||
if (existing) return this.generateCouponCode(restId);
|
||||
|
||||
Reference in New Issue
Block a user