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

implement retrieval of teamAiAssistants

Shun Miyazawa 1 год назад
Родитель
Сommit
7929381de4
1 измененных файлов с 26 добавлено и 1 удалено
  1. 26 1
      apps/app/src/features/openai/server/services/openai.ts

+ 26 - 1
apps/app/src/features/openai/server/services/openai.ts

@@ -526,9 +526,34 @@ class OpenaiService implements IOpenaiService {
   async getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants> {
     const myAiAssistants = await AiAssistantModel.find({ owner: user });
 
+    const userGroups = [
+      ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+      ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+    ].map(group => group.toString());
+
+    const teamAiAssistantsConditions: mongoose.FilterQuery<AiAssistantDocument> = {
+      $or: [
+        {
+          $and: [
+            { owner: { $ne: user } },
+            { accessScope: AiAssistantAccessScope.PUBLIC_ONLY },
+          ],
+        },
+        {
+          $and: [
+            { owner: { $ne: user } },
+            { accessScope: AiAssistantAccessScope.GROUPS },
+            { 'grantedGroups.item': { $in: userGroups } },
+          ],
+        },
+      ],
+    };
+
+    const teamAiAssistants = await AiAssistantModel.find(teamAiAssistantsConditions);
+
     return {
       myAiAssistants: myAiAssistants as AiAssistant[] ?? [],
-      teamAiAssistants: [],
+      teamAiAssistants: teamAiAssistants as AiAssistant[] ?? [],
     };
   }