Browse Source

Refactor setDefault (2)

Shun Miyazawa 1 year ago
parent
commit
783ca35195

+ 2 - 3
apps/app/src/features/openai/server/models/ai-assistant.ts

@@ -1,5 +1,4 @@
 import { type IGrantedGroup, GroupType } from '@growi/core';
 import { type IGrantedGroup, GroupType } from '@growi/core';
-import createError from 'http-errors';
 import { type Model, type Document, Schema } from 'mongoose';
 import { type Model, type Document, Schema } from 'mongoose';
 
 
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
@@ -112,9 +111,9 @@ const schema = new Schema<AiAssistantDocument>(
 
 
 
 
 schema.statics.setDefault = async function(id: string, isDefault: boolean): Promise<AiAssistantDocument> {
 schema.statics.setDefault = async function(id: string, isDefault: boolean): Promise<AiAssistantDocument> {
-  const aiAssistant = await this.findOne({ _id: id, shareScope: AiAssistantAccessScope.PUBLIC_ONLY });
+  const aiAssistant = await this.findById(id);
   if (aiAssistant == null) {
   if (aiAssistant == null) {
-    throw createError(404, 'AiAssistant document does not exist');
+    throw new Error('AiAssistant not found');
   }
   }
 
 
   await this.updateMany({ isDefault: true }, { isDefault: false });
   await this.updateMany({ isDefault: true }, { isDefault: false });

+ 6 - 0
apps/app/src/features/openai/server/routes/set-default-ai-assistant.ts

@@ -9,6 +9,7 @@ import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
 import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
+import { AiAssistantShareScope } from '../../interfaces/ai-assistant';
 import AiAssistantModel from '../models/ai-assistant';
 import AiAssistantModel from '../models/ai-assistant';
 import { getOpenaiService } from '../services/openai';
 import { getOpenaiService } from '../services/openai';
 
 
@@ -49,6 +50,11 @@ export const setDefaultAiAssistantFactory: setDefaultAiAssistantFactory = (crowi
         const { id } = req.params;
         const { id } = req.params;
         const { isDefault } = req.body;
         const { isDefault } = req.body;
 
 
+        const aiAssistant = await AiAssistantModel.findOne({ _id: id, shareScope: AiAssistantShareScope.PUBLIC_ONLY });
+        if (aiAssistant == null) {
+          return res.apiv3Err(new ErrorV3('AiAssistant not found or not public'), 404);
+        }
+
         const updatedAiAssistant = await AiAssistantModel.setDefault(id, isDefault);
         const updatedAiAssistant = await AiAssistantModel.setDefault(id, isDefault);
         return res.apiv3({ updatedAiAssistant });
         return res.apiv3({ updatedAiAssistant });
       }
       }

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

@@ -829,17 +829,17 @@ class OpenaiService implements IOpenaiService {
       this.createVectorStoreFileWithStream(newVectorStoreRelation, conditions);
       this.createVectorStoreFileWithStream(newVectorStoreRelation, conditions);
     }
     }
 
 
-    if (data.shareScope !== AiAssistantShareScope.PUBLIC_ONLY && aiAssistant.isDefault) {
-      await AiAssistantModel.setDefault(aiAssistant._id, false);
-    }
-
     const newData = {
     const newData = {
       ...data,
       ...data,
       vectorStore: newVectorStoreRelation ?? aiAssistant.vectorStore,
       vectorStore: newVectorStoreRelation ?? aiAssistant.vectorStore,
     };
     };
 
 
     aiAssistant.set({ ...newData });
     aiAssistant.set({ ...newData });
-    const updatedAiAssistant = await aiAssistant.save();
+    let updatedAiAssistant: AiAssistantDocument = await aiAssistant.save();
+
+    if (data.shareScope !== AiAssistantShareScope.PUBLIC_ONLY && aiAssistant.isDefault) {
+      updatedAiAssistant = await AiAssistantModel.setDefault(aiAssistant._id, false);
+    }
 
 
     return updatedAiAssistant;
     return updatedAiAssistant;
   }
   }