Skip to content

Commit

Permalink
backend: Fix goods category can't be deleted if the booth is not in p…
Browse files Browse the repository at this point in the history
…ublished status
  • Loading branch information
somnisomni committed Aug 21, 2024
1 parent 78a0982 commit 9b41dd8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions projects/Admin/src/lib/api-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ export default class AdminAPI extends BaseAdminAPI {
return await this.apiCallWrapper<CT.ISuccessResponse>(() => this.API.DELETE(`goods/${goodsId}/image?bId=${boothId}`));
}

static async deleteGoodsCategory(categoryId: number) {
return await this.apiCallWrapper<CT.ISuccessResponse>(() => this.API.DELETE(`goods/category/${categoryId}`));
static async deleteGoodsCategory(categoryId: number, boothId: number) {
return await this.apiCallWrapper<CT.ISuccessResponse>(() => this.API.DELETE(`goods/category/${categoryId}?bId=${boothId}`));
}

static async deleteGoodsCombination(combinationId: number, boothId: number) {
Expand Down
2 changes: 1 addition & 1 deletion projects/Admin/src/plugins/stores/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ const useAdminAPIStore = defineStore("admin-api", () => {

async function deleteGoodsCategory(categoryId: number): Promise<true | C.ErrorCodes> {
return await simplifyAPICall(
() => AdminAPI.deleteGoodsCategory(categoryId),
() => AdminAPI.deleteGoodsCategory(categoryId, $adminStore.currentBooth.booth!.id),
async () => {
delete $adminStore.currentBooth.goodsCategories![categoryId];
await fetchGoodsOfCurrentBooth();
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
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";
import { GoodsCategoryInfoUpdateFailedException, GoodsCategoryParentBoothNotFoundException } from "./goods-category.exception";

@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();
Expand Down Expand Up @@ -64,8 +63,8 @@ export class GoodsCategoryService {
return category;
}

async remove(id: number): Promise<ISuccessResponse> {
const category = await this.publicGoodsCategoryService.findOne(id);
async remove(id: number, boothId: number, callerAccountId: number): Promise<ISuccessResponse> {
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 } })) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { BoothNotPublishedException } from "../booth/booth.exception";

@Injectable()
export class PublicGoodsCategoryService {
async findOne(id: number, isAdmin: boolean = false): Promise<GoodsCategory> {
async findOne(id: number): Promise<GoodsCategory> {
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;
Expand Down

0 comments on commit 9b41dd8

Please sign in to comment.