Prechádzať zdrojové kódy

fix(category): optimize refreshTagsMetadata to load tags directly from the tag table

Dave 1 mesiac pred
rodič
commit
9ca68551ac

+ 11 - 19
apps/box-mgnt-api/src/mgnt-backend/feature/category/category.service.ts

@@ -274,38 +274,30 @@ export class CategoryService {
   async refreshTagsMetadata(categoryId?: string | null): Promise<void> {
     if (!categoryId) return;
 
-    // 1. Get tagIds bound to this category
-    const categoryTags = await this.mongoPrismaService.categoryTag.findMany({
-      where: { categoryId },
-      orderBy: { tagId: 'asc' }, // stable order, real order comes from Tag.seq
-      select: { tagId: true },
+    // Load tags via tag.categoryId (authoritative source)
+    const tags = await this.mongoPrismaService.tag.findMany({
+      where: {
+        status: CommonStatus.enabled,
+        categoryId,
+      },
+      orderBy: { seq: 'asc' },
+      select: { id: true, name: true },
     });
 
-    if (!categoryTags.length) {
+    if (!tags.length) {
       // No tags under this category → clear derived metadata
       await this.mongoPrismaService.category.update({
         where: { id: categoryId },
         data: {
           tags: [],
           tagNames: [],
+          updateAt: this.now(),
         },
       });
       return;
     }
 
-    const tagIds = categoryTags.map((ct) => ct.tagId);
-
-    // 2. Load tags (authoritative source)
-    const tags = await this.mongoPrismaService.tag.findMany({
-      where: {
-        id: { in: tagIds },
-        status: CommonStatus.enabled,
-      },
-      orderBy: { seq: 'asc' },
-      select: { id: true, name: true },
-    });
-
-    // 3. Rebuild derived payload
+    // Rebuild derived payload
     await this.rebuildCategoryTagPayload(categoryId, tags);
   }