refactor coupon module

This commit is contained in:
2026-02-09 20:34:06 +03:30
parent 586588c7f1
commit 8ba45c5d82
6 changed files with 21 additions and 51 deletions
+1 -1
View File
@@ -5,5 +5,5 @@ export enum ContactStatusEnum {
export enum ContactScope {
APPLICATION = 'application',
RESTAURANT = 'shop',
SHOP = 'shop',
}
@@ -22,7 +22,7 @@ export class ContactService {
let shop: Shop | null = null;
if ((restId || slug) && dto.scope === ContactScope.RESTAURANT) {
if ((restId || slug) && dto.scope === ContactScope.SHOP) {
shop = await this.em.findOne(Shop, {
...(restId ? { id: restId } : {})
, ...(slug ? { slug: slug } : {})
@@ -49,7 +49,7 @@ export class ContactService {
orderBy: dto.orderBy,
order: dto.order,
status: dto.status,
scope: ContactScope.RESTAURANT,
scope: ContactScope.SHOP,
restaurantId
});
}
@@ -6,12 +6,6 @@ import { FindCouponsDto } from '../dto/find-coupons.dto';
import {
ApiTags,
ApiOperation,
ApiCreatedResponse,
ApiOkResponse,
ApiNotFoundResponse,
ApiQuery,
ApiBody,
ApiParam,
ApiBearerAuth,
ApiHeader,
} from '@nestjs/swagger';
@@ -24,89 +18,65 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
@ApiTags('coupons')
@Controller()
@ApiBearerAuth()
export class CouponController {
constructor(private readonly couponService: CouponService) { }
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/coupons/me')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Get paginated list of shop coupons' })
@ApiHeader(API_HEADER_SLUG)
@ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number })
@ApiQuery({ name: 'search', required: false, type: String })
@ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
@ApiQuery({ name: 'type', required: false, enum: ['PERCENTAGE', 'FIXED'] })
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
getMyCoupons(@Query() dto: FindCouponsDto, @ShopId() restId: string) {
return this.couponService.findAll(restId, dto);
getMyCoupons(@Query() dto: FindCouponsDto, @ShopId() shopId: string) {
return this.couponService.findAll(shopId, dto);
}
/*** Admin ***/
//========================== Admin =======================
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Post('admin/coupons')
@ApiOperation({ summary: 'Create a new coupon' })
@ApiCreatedResponse({ description: 'The coupon has been successfully created.', type: CreateCouponDto })
@ApiBody({ type: CreateCouponDto })
create(@Body() createCouponDto: CreateCouponDto, @ShopId() restId: string) {
return this.couponService.create(restId, createCouponDto);
create(@Body() createCouponDto: CreateCouponDto, @ShopId() shopId: string) {
return this.couponService.create(shopId, createCouponDto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Get('admin/coupons')
@ApiOperation({ summary: 'Get a paginated list of coupons' })
@ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number })
@ApiQuery({ name: 'search', required: false, type: String })
@ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
@ApiQuery({ name: 'type', required: false, enum: ['PERCENTAGE', 'FIXED'] })
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
findAll(@Query() dto: FindCouponsDto, @ShopId() restId: string) {
return this.couponService.findAll(restId, dto);
findAll(@Query() dto: FindCouponsDto, @ShopId() shopId: string) {
return this.couponService.findAll(shopId, dto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Get('admin/coupons/:id')
@ApiOperation({ summary: 'Get a coupon by id' })
@ApiParam({ name: 'id', required: true })
findById(@Param('id') id: string) {
return this.couponService.findById(id);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Patch('admin/coupons/:id')
@ApiOperation({ summary: 'Update a coupon' })
@ApiParam({ name: 'id', required: true })
@ApiBody({ type: UpdateCouponDto })
update(@Param('id') id: string, @Body() updateCouponDto: UpdateCouponDto) {
return this.couponService.update(id, updateCouponDto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Delete('admin/coupons/:id')
@ApiOperation({ summary: 'Delete (soft) a coupon' })
@ApiParam({ name: 'id', required: true })
remove(@Param('id') id: string) {
return this.couponService.remove(id);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.MANAGE_COUPONS)
@Get('admin/coupons/generate-code')
@ApiOperation({ summary: 'generate a coupon code' })
generateCouponCode(@ShopId() restId: string) {
return this.couponService.generateCouponCode(restId);
generateCouponCode(@ShopId() shopId: string) {
return this.couponService.generateCouponCode(shopId);
}
}
+1 -1
View File
@@ -93,7 +93,7 @@ export class CreateCouponDto {
description: 'Array of product category IDs. If empty, coupon applies to all categories.',
type: [String],
})
foodCategories?: string[];
productCategories?: string[];
@IsOptional()
@IsArray()
@@ -53,7 +53,7 @@ export class Coupon extends BaseEntity {
isActive: boolean = true;
@Property({ type: 'json', nullable: true })
foodCategories?: string[]; // Array of category IDs
productCategories?: string[]; // Array of category IDs
@Property({ type: 'json', nullable: true })
products?: string[]; // Array of product IDs
@@ -17,7 +17,7 @@ export class CouponService {
private readonly couponRepository: CouponRepository,
private readonly shopRepository: ShopRepository,
private readonly em: EntityManager,
) {}
) { }
async create(restId: string, createCouponDto: CreateCouponDto): Promise<Coupon> {
const shop = await this.shopRepository.findOne({ id: restId });
@@ -62,7 +62,7 @@ export class CouponService {
endDate: createCouponDto.endDate ? new Date(createCouponDto.endDate) : undefined,
isActive: createCouponDto.isActive ?? true,
usedCount: 0,
foodCategories: createCouponDto.foodCategories,
productCategories: createCouponDto.productCategories,
products: createCouponDto.products,
userPhone: createCouponDto.userPhone,
};
@@ -138,7 +138,7 @@ export class CouponService {
if (dto.startDate !== undefined) coupon.startDate = dto.startDate ? new Date(dto.startDate) : undefined;
if (dto.endDate !== undefined) coupon.endDate = dto.endDate ? new Date(dto.endDate) : undefined;
if (dto.isActive !== undefined) coupon.isActive = dto.isActive;
if (dto.foodCategories !== undefined) coupon.foodCategories = dto.foodCategories;
if (dto.productCategories !== undefined) coupon.productCategories = dto.productCategories;
if (dto.products !== undefined) coupon.products = dto.products;
if (dto.userPhone !== undefined) coupon.userPhone = dto.userPhone;