fix bug in coupon
This commit is contained in:
@@ -19,8 +19,8 @@ export class CouponService {
|
|||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(restId: string, createCouponDto: CreateCouponDto): Promise<Coupon> {
|
async create(shopId: string, createCouponDto: CreateCouponDto): Promise<Coupon> {
|
||||||
const shop = await this.shopRepository.findOne({ id: restId });
|
const shop = await this.shopRepository.findOne({ id: shopId });
|
||||||
if (!shop) {
|
if (!shop) {
|
||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -76,8 +76,8 @@ export class CouponService {
|
|||||||
return coupon;
|
return coupon;
|
||||||
}
|
}
|
||||||
|
|
||||||
findAll(restId: string, dto: FindCouponsDto) {
|
findAll(shopId: string, dto: FindCouponsDto) {
|
||||||
return this.couponRepository.findAllPaginated(restId, dto);
|
return this.couponRepository.findAllPaginated(shopId, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findById(id: string): Promise<Coupon> {
|
async findById(id: string): Promise<Coupon> {
|
||||||
@@ -88,8 +88,8 @@ export class CouponService {
|
|||||||
return coupon;
|
return coupon;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByCode(code: string, restId: string): Promise<Coupon> {
|
async findByCode(code: string, shopId: string): Promise<Coupon> {
|
||||||
const coupon = await this.couponRepository.findOne({ code, shop: { id: restId } });
|
const coupon = await this.couponRepository.findOne({ code, shop: { id: shopId } });
|
||||||
if (!coupon) {
|
if (!coupon) {
|
||||||
throw new NotFoundException(CouponMessage.NOT_FOUND);
|
throw new NotFoundException(CouponMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -160,7 +160,7 @@ export class CouponService {
|
|||||||
*/
|
*/
|
||||||
async validateCoupon(
|
async validateCoupon(
|
||||||
code: string,
|
code: string,
|
||||||
restId: string,
|
shopId: string,
|
||||||
orderAmount: number,
|
orderAmount: number,
|
||||||
userPhone: string,
|
userPhone: string,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
@@ -169,7 +169,7 @@ export class CouponService {
|
|||||||
message?: string;
|
message?: string;
|
||||||
}> {
|
}> {
|
||||||
const coupon = await this.couponRepository.findOne(
|
const coupon = await this.couponRepository.findOne(
|
||||||
{ code, shop: { id: restId } },
|
{ code, shop: { id: shopId } },
|
||||||
{ populate: ['shop'] },
|
{ populate: ['shop'] },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -245,7 +245,7 @@ export class CouponService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateShortCode(restaurantSlug: string, length = 5) {
|
generateShortCode(shopSlug: string, length = 5) {
|
||||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||||
const bytes = randomBytes(length);
|
const bytes = randomBytes(length);
|
||||||
let result = '';
|
let result = '';
|
||||||
@@ -253,19 +253,19 @@ export class CouponService {
|
|||||||
for (let i = 0; i < bytes.length; i++) {
|
for (let i = 0; i < bytes.length; i++) {
|
||||||
result += chars[bytes[i] % chars.length];
|
result += chars[bytes[i] % chars.length];
|
||||||
}
|
}
|
||||||
const prefix = restaurantSlug.slice(0, 2).toUpperCase();
|
const prefix = shopSlug.slice(0, 2).toUpperCase();
|
||||||
return prefix + '-' + result;
|
return prefix + '-' + result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async generateCouponCode(restId: string): Promise<string> {
|
async generateCouponCode(shopId: string): Promise<{ code: string }> {
|
||||||
const shop = await this.shopRepository.findOne({ id: restId });
|
const shop = await this.shopRepository.findOne({ id: shopId });
|
||||||
if (!shop) {
|
if (!shop) {
|
||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
const code = this.generateShortCode(shop.slug);
|
const code = this.generateShortCode(shop.slug);
|
||||||
const existing = await this.couponRepository.findOne({ code, shop: { id: restId } });
|
const existing = await this.couponRepository.findOne({ code, shop: { id: shopId } });
|
||||||
|
|
||||||
if (existing) return this.generateCouponCode(restId);
|
if (existing) return this.generateCouponCode(shopId);
|
||||||
return code;
|
return { code }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user