coupon module
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { CouponService } from '../providers/coupon.service';
|
||||
import { CreateCouponDto } from '../dto/create-coupon.dto';
|
||||
import { UpdateCouponDto } from '../dto/update-coupon.dto';
|
||||
import { FindCouponsDto } from '../dto/find-coupons.dto';
|
||||
import { ValidateCouponDto } from '../dto/validate-coupon.dto';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiNotFoundResponse,
|
||||
ApiQuery,
|
||||
ApiBody,
|
||||
ApiParam,
|
||||
ApiBearerAuth,
|
||||
} from '@nestjs/swagger';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { RestId } from 'src/common/decorators';
|
||||
|
||||
@ApiTags('coupons')
|
||||
@Controller()
|
||||
export class CouponController {
|
||||
constructor(private readonly couponService: CouponService) {}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@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, @RestId() restId: string) {
|
||||
return this.couponService.create(restId, createCouponDto);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('admin/coupons')
|
||||
@ApiOperation({ summary: 'Get a paginated list of coupons' })
|
||||
@ApiOkResponse({ description: '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, @RestId() restId: string) {
|
||||
return this.couponService.findAll(restId, dto);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('admin/coupons/:id')
|
||||
@ApiOperation({ summary: 'Get a coupon by id' })
|
||||
@ApiParam({ name: 'id', required: true })
|
||||
@ApiOkResponse({ description: 'The coupon', type: CreateCouponDto })
|
||||
@ApiNotFoundResponse({ description: 'Coupon not found' })
|
||||
findById(@Param('id') id: string) {
|
||||
return this.couponService.findById(id);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Patch('admin/coupons/:id')
|
||||
@ApiOperation({ summary: 'Update a coupon' })
|
||||
@ApiParam({ name: 'id', required: true })
|
||||
@ApiBody({ type: UpdateCouponDto })
|
||||
@ApiOkResponse({ description: 'The updated coupon', type: UpdateCouponDto })
|
||||
update(@Param('id') id: string, @Body() updateCouponDto: UpdateCouponDto) {
|
||||
return this.couponService.update(id, updateCouponDto);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Delete('admin/coupons/:id')
|
||||
@ApiOperation({ summary: 'Delete (soft) a coupon' })
|
||||
@ApiParam({ name: 'id', required: true })
|
||||
@ApiResponse({ status: 200, description: 'Coupon deleted' })
|
||||
remove(@Param('id') id: string) {
|
||||
return this.couponService.remove(id);
|
||||
}
|
||||
|
||||
@Post('public/coupons/validate')
|
||||
@ApiOperation({ summary: 'Validate a coupon code' })
|
||||
@ApiBody({ type: ValidateCouponDto })
|
||||
@ApiOkResponse({ description: 'Coupon validation result' })
|
||||
@ApiNotFoundResponse({ description: 'Coupon not found or invalid' })
|
||||
validateCoupon(@Body() validateCouponDto: ValidateCouponDto, @RestId() restId: string) {
|
||||
return this.couponService.validateCoupon(
|
||||
validateCouponDto.code,
|
||||
restId,
|
||||
validateCouponDto.orderAmount || 0,
|
||||
validateCouponDto.userId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user