From 9b41dd8d5012ec8515a241bd0c82e6dbaf64e692 Mon Sep 17 00:00:00 2001 From: somni Date: Wed, 21 Aug 2024 14:38:13 +0900 Subject: [PATCH] backend: Fix goods category can't be deleted if the booth is not in published status --- projects/Admin/src/lib/api-admin.ts | 4 ++-- projects/Admin/src/plugins/stores/api.ts | 2 +- .../admin/goods-category/goods-category.controller.ts | 6 +++--- .../admin/goods-category/goods-category.service.ts | 11 +++++------ .../public/goods-category/goods-category.service.ts | 8 +++----- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/projects/Admin/src/lib/api-admin.ts b/projects/Admin/src/lib/api-admin.ts index 9b3f5bed..4cdc2a7e 100644 --- a/projects/Admin/src/lib/api-admin.ts +++ b/projects/Admin/src/lib/api-admin.ts @@ -203,8 +203,8 @@ export default class AdminAPI extends BaseAdminAPI { return await this.apiCallWrapper(() => this.API.DELETE(`goods/${goodsId}/image?bId=${boothId}`)); } - static async deleteGoodsCategory(categoryId: number) { - return await this.apiCallWrapper(() => this.API.DELETE(`goods/category/${categoryId}`)); + static async deleteGoodsCategory(categoryId: number, boothId: number) { + return await this.apiCallWrapper(() => this.API.DELETE(`goods/category/${categoryId}?bId=${boothId}`)); } static async deleteGoodsCombination(combinationId: number, boothId: number) { diff --git a/projects/Admin/src/plugins/stores/api.ts b/projects/Admin/src/plugins/stores/api.ts index 532badd9..d299f614 100644 --- a/projects/Admin/src/plugins/stores/api.ts +++ b/projects/Admin/src/plugins/stores/api.ts @@ -454,7 +454,7 @@ const useAdminAPIStore = defineStore("admin-api", () => { async function deleteGoodsCategory(categoryId: number): Promise { return await simplifyAPICall( - () => AdminAPI.deleteGoodsCategory(categoryId), + () => AdminAPI.deleteGoodsCategory(categoryId, $adminStore.currentBooth.booth!.id), async () => { delete $adminStore.currentBooth.goodsCategories![categoryId]; await fetchGoodsOfCurrentBooth(); diff --git a/projects/Backend/src/modules/admin/goods-category/goods-category.controller.ts b/projects/Backend/src/modules/admin/goods-category/goods-category.controller.ts index 41d2eb80..52a1141f 100644 --- a/projects/Backend/src/modules/admin/goods-category/goods-category.controller.ts +++ b/projects/Backend/src/modules/admin/goods-category/goods-category.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, UseInterceptors, ClassSerializerInterceptor } from "@nestjs/common"; +import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, UseInterceptors, ClassSerializerInterceptor, Query } from "@nestjs/common"; import { PublicGoodsCategoryService } from "@/modules/public/goods-category/goods-category.service"; import { AuthData, AdminAuthGuard, SuperAdmin } from "../auth/auth.guard"; import { IAuthPayload } from "../auth/jwt"; @@ -27,8 +27,8 @@ export class GoodsCategoryController { } @Delete(":id") - remove(@Param("id") id: string) { - return this.goodsCategoryService.remove(+id); + remove(@Param("id") id: string, @Query("bId") boothId: string, @AuthData() authData: IAuthPayload) { + return this.goodsCategoryService.remove(+id, +boothId, authData.id); } /* SuperAdmin routes */ diff --git a/projects/Backend/src/modules/admin/goods-category/goods-category.service.ts b/projects/Backend/src/modules/admin/goods-category/goods-category.service.ts index ab4acd04..aa9ceccf 100644 --- a/projects/Backend/src/modules/admin/goods-category/goods-category.service.ts +++ b/projects/Backend/src/modules/admin/goods-category/goods-category.service.ts @@ -1,11 +1,10 @@ import { Injectable } from "@nestjs/common"; import { ISuccessResponse } from "@myboothmanager/common"; import GoodsCategory from "@/db/models/goods-category"; -import { create, removeTarget } from "@/lib/common-functions"; +import { create, findOneByPk, removeTarget } from "@/lib/common-functions"; import Booth from "@/db/models/booth"; import Goods from "@/db/models/goods"; import { DuplicatedEntityException, EntityNotFoundException, NoAccessException } from "@/lib/exceptions"; -import { PublicGoodsCategoryService } from "@/modules/public/goods-category/goods-category.service"; import GoodsCombination from "@/db/models/goods-combination"; import { CreateGoodsCategoryRequestDto } from "./dto/create-goods-category.dto"; import { UpdateGoodsCategoryRequestDto } from "./dto/update-goods-category.dto"; @@ -13,10 +12,10 @@ import { GoodsCategoryInfoUpdateFailedException, GoodsCategoryParentBoothNotFoun @Injectable() export class GoodsCategoryService { - constructor(private readonly publicGoodsCategoryService: PublicGoodsCategoryService) { } + constructor() { } async getGoodsCategoryAndParentBooth(categoryId: number, boothId: number, callerAccountId: number): Promise<{ booth: Booth, category: GoodsCategory }> { - const category = await this.publicGoodsCategoryService.findOne(categoryId, true); + const category = await findOneByPk(GoodsCategory, categoryId); if(!category) throw new EntityNotFoundException(); else if(category.boothId !== boothId) throw new NoAccessException(); @@ -64,8 +63,8 @@ export class GoodsCategoryService { return category; } - async remove(id: number): Promise { - const category = await this.publicGoodsCategoryService.findOne(id); + async remove(id: number, boothId: number, callerAccountId: number): Promise { + const category = await this.findGoodsCategoryBelongsToBooth(id, boothId, callerAccountId); // Find goods/combinations by category and set to default(uncategorized) for(const g of await Goods.findAll({ where: { categoryId: id } })) { diff --git a/projects/Backend/src/modules/public/goods-category/goods-category.service.ts b/projects/Backend/src/modules/public/goods-category/goods-category.service.ts index 419a2655..0e905a49 100644 --- a/projects/Backend/src/modules/public/goods-category/goods-category.service.ts +++ b/projects/Backend/src/modules/public/goods-category/goods-category.service.ts @@ -7,13 +7,11 @@ import { BoothNotPublishedException } from "../booth/booth.exception"; @Injectable() export class PublicGoodsCategoryService { - async findOne(id: number, isAdmin: boolean = false): Promise { + async findOne(id: number): Promise { const category = await findOneByPk(GoodsCategory, id); - if(!isAdmin) { - if(!await PublicCommon.isBoothPublicilyAccessible(category.boothId)) { - throw new BoothNotPublishedException(); - } + if(!await PublicCommon.isBoothPublicilyAccessible(category.boothId)) { + throw new BoothNotPublishedException(); } return category;