108 lines
4.1 KiB
TypeScript
108 lines
4.1 KiB
TypeScript
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 {
|
|
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()
|
|
@Get('public/coupons/me')
|
|
@ApiOperation({ summary: 'Get paginated list of restaurant coupons' })
|
|
@ApiOkResponse({ description: 'Paginated list of restaurant 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 })
|
|
getMyCoupons(@Query() dto: FindCouponsDto, @RestId() restId: string) {
|
|
return this.couponService.findAll(restId, dto);
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Get('admin/coupons/generate-code')
|
|
@ApiOperation({ summary: 'generate a coupon code' })
|
|
generateCouponCode(@RestId() restId: string) {
|
|
return this.couponService.generateCouponCode(restId);
|
|
}
|
|
}
|