update: add delete and update route for the dicsount
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
import { Body, Controller, Get, Post, Query } from "@nestjs/common";
|
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from "@nestjs/common";
|
||||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||||
|
|
||||||
import { CreateDiscountDto } from "./DTO/create-discount.dto";
|
import { CreateDiscountDto } from "./DTO/create-discount.dto";
|
||||||
import { SearchDiscountQueryDto } from "./DTO/search-discount-query.dto";
|
import { SearchDiscountQueryDto } from "./DTO/search-discount-query.dto";
|
||||||
|
import { UpdateDiscountDto } from "./DTO/update-discount.dto";
|
||||||
import { DiscountsService } from "./providers/discounts.service";
|
import { DiscountsService } from "./providers/discounts.service";
|
||||||
import { AdminRoute } from "../../common/decorators/admin.decorator";
|
import { AdminRoute } from "../../common/decorators/admin.decorator";
|
||||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||||
import { PermissionsDec } from "../../common/decorators/permission.decorator";
|
import { PermissionsDec } from "../../common/decorators/permission.decorator";
|
||||||
|
import { ParamDto } from "../../common/DTO/param.dto";
|
||||||
import { PermissionEnum } from "../users/enums/permission.enum";
|
import { PermissionEnum } from "../users/enums/permission.enum";
|
||||||
|
|
||||||
@ApiTags("Discounts")
|
@ApiTags("Discounts")
|
||||||
@@ -32,4 +34,37 @@ export class DiscountsController {
|
|||||||
getDiscountsForAdmin(@Query() queryDto: SearchDiscountQueryDto) {
|
getDiscountsForAdmin(@Query() queryDto: SearchDiscountQueryDto) {
|
||||||
return this.discountsService.getDiscountsForAdmin(queryDto);
|
return this.discountsService.getDiscountsForAdmin(queryDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ summary: "Get discount by id" })
|
||||||
|
@AdminRoute()
|
||||||
|
@PermissionsDec(PermissionEnum.DISCOUNTS)
|
||||||
|
@Get(":id")
|
||||||
|
@ApiResponse({ status: 200, description: "Discount fetched successfully" })
|
||||||
|
getDiscountById(@Param() paramDto: ParamDto) {
|
||||||
|
return this.discountsService.getDiscountById(paramDto.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ summary: "Toggle discount status" })
|
||||||
|
@AdminRoute()
|
||||||
|
@PermissionsDec(PermissionEnum.DISCOUNTS)
|
||||||
|
@Post(":id/toggle-status")
|
||||||
|
toggleDiscountStatus(@Param() paramDto: ParamDto) {
|
||||||
|
return this.discountsService.toggleDiscountStatus(paramDto.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ summary: "Delete discount" })
|
||||||
|
@AdminRoute()
|
||||||
|
@PermissionsDec(PermissionEnum.DISCOUNTS)
|
||||||
|
@Delete(":id")
|
||||||
|
deleteDiscount(@Param() paramDto: ParamDto) {
|
||||||
|
return this.discountsService.softDeleteDiscount(paramDto.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ summary: "Update discount" })
|
||||||
|
@AdminRoute()
|
||||||
|
@PermissionsDec(PermissionEnum.DISCOUNTS)
|
||||||
|
@Patch(":id")
|
||||||
|
updateDiscount(@Param() paramDto: ParamDto, @Body() updateDiscountDto: UpdateDiscountDto) {
|
||||||
|
return this.discountsService.updateDiscount(paramDto.id, updateDiscountDto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,20 @@ export class DiscountsService {
|
|||||||
return this.discountRepository.findById(id);
|
return this.discountRepository.findById(id);
|
||||||
}
|
}
|
||||||
//************************************ */
|
//************************************ */
|
||||||
|
|
||||||
|
async toggleDiscountStatus(id: string) {
|
||||||
|
const discount = await this.discountRepository.findById(id);
|
||||||
|
if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
|
||||||
|
|
||||||
|
discount.isActive = !discount.isActive;
|
||||||
|
await this.discountRepository.save(discount);
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: DiscountMessage.UPDATED,
|
||||||
|
discount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
//************************************ */
|
||||||
async updateDiscount(id: string, updateDiscountDto: UpdateDiscountDto) {
|
async updateDiscount(id: string, updateDiscountDto: UpdateDiscountDto) {
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ export class DiscountRepository extends Repository<Discount> {
|
|||||||
|
|
||||||
async findById(id: string) {
|
async findById(id: string) {
|
||||||
return this.findOne({
|
return this.findOne({
|
||||||
where: { id, isActive: true },
|
where: { id },
|
||||||
|
relations: {
|
||||||
|
subscriptionPlans: true,
|
||||||
|
},
|
||||||
withDeleted: false,
|
withDeleted: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user