Просмотр исходного кода

fix(menu): normalize parentId in MenuDto to handle empty strings

Dave 1 месяц назад
Родитель
Сommit
e45356697e
1 измененных файлов с 8 добавлено и 0 удалено
  1. 8 0
      apps/box-mgnt-api/src/mgnt-backend/core/menu/menu.service.ts

+ 8 - 0
apps/box-mgnt-api/src/mgnt-backend/core/menu/menu.service.ts

@@ -247,6 +247,12 @@ export class MenuService {
     return null;
   }
 
+  private normalizeParentOfDto(dto: MenuDto) {
+    if (dto.parentId == null) return;
+    const trimmedId = dto.parentId.trim();
+    dto.parentId = trimmedId === '' ? undefined : trimmedId;
+  }
+
   private ensureValidHierarchy(id: string | null, dto: MenuDto) {
     if (id !== null && id === dto.parentId) {
       throw new BadRequestException('父级菜单不能是自己');
@@ -275,12 +281,14 @@ export class MenuService {
   }
 
   async create(menuDto: MenuDto) {
+    this.normalizeParentOfDto(menuDto);
     await this.checkMenuDto(null, menuDto);
     const data = this.toCreateData(menuDto);
     await this.mongoPrismaService.sysMenu.create({ data });
   }
 
   async update(id: string, menuDto: MenuDto) {
+    this.normalizeParentOfDto(menuDto);
     this.ensureValidHierarchy(id, menuDto);
 
     if (menuDto.type !== MenuType.DIRECTORY) {