From 8b1fa2650e92bd95f2c092bb28166003e11a02f1 Mon Sep 17 00:00:00 2001 From: J-Hoplin Date: Wed, 31 Jul 2024 23:32:19 +0900 Subject: [PATCH] Fix: Fix ai classification logic --- .../ai-classification.service.ts | 40 +++++++++++++------ src/modules/folders/folders.repository.ts | 8 ++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/modules/ai-classification/ai-classification.service.ts b/src/modules/ai-classification/ai-classification.service.ts index f60f77e..802af78 100644 --- a/src/modules/ai-classification/ai-classification.service.ts +++ b/src/modules/ai-classification/ai-classification.service.ts @@ -26,16 +26,15 @@ export class AiClassificationService { // Map - (Folder Name):(Folder ID) const folderMapper = {}; - const folderNames = payload.folderList.map((folder) => { + payload.folderList.forEach((folder) => { folderMapper[folder.name] = folder.id; - return folder.name; }); // NOTE: AI 요약 요청 const start = process.hrtime(); const summarizeUrlContent = await this.aiService.summarizeLinkContent( payload.postContent, - folderNames, + Object.keys(folderMapper), payload.url, ); @@ -59,17 +58,33 @@ export class AiClassificationService { post = await this.postRepository.findPostByIdForAIClassification(postId); - const classification = - await this.classificationRepository.createClassification( - post.url, - summarizeUrlContent.response.summary, - summarizeUrlContent.response.keywords, - folderId, - ); - classificationId = classification._id.toString(); + // 기존에 디폴트 폴더 안막은것들이 있어서... 우선은 다 불러와서 필터링 하는 방식으로 했어용 + const defaultFolders = await this.folderRepository.getDefaultFolders( + post.userId.toString(), + ); + const defaultFolderIds = defaultFolders.map((folder) => + folder._id.toString(), + ); + const isDefaultFolder = defaultFolderIds.includes( + post.folderId.toString(), + ); postAiStatus = PostAiStatus.SUCCESS; + // 디폴트 폴더인 경우에만 classification 생성 + if (!isDefaultFolder) { + const classification = + await this.classificationRepository.createClassification( + post.url, + summarizeUrlContent.response.summary, + summarizeUrlContent.response.keywords, + folderId, + ); + // Post에 추가하기 위한 classificaiton ID + classificationId = classification._id.toString(); + } + + // Keyword는 성공여부 상관없이 생성 const keywords = await this.keywordsRepository.createMany( summarizeUrlContent.response.keywords, ); @@ -95,10 +110,9 @@ export class AiClassificationService { classificationId, summarizeUrlContent.response.summary, ); - return summarizeUrlContent; } catch (error: unknown) { - return { success: fail, error }; + return { success: false, error }; } } } diff --git a/src/modules/folders/folders.repository.ts b/src/modules/folders/folders.repository.ts index 1cc4e7d..4348340 100644 --- a/src/modules/folders/folders.repository.ts +++ b/src/modules/folders/folders.repository.ts @@ -68,4 +68,12 @@ export class FolderRepository { return folder; } + + async getDefaultFolders(userId: string) { + const folders = await this.folderModel.find({ + userId: userId, + type: FolderType.DEFAULT, + }); + return folders; + } }