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

Refactor AiAssistantModel.setDefault

Shun Miyazawa 1 год назад
Родитель
Сommit
3938b64ace

+ 36 - 6
apps/app/src/features/openai/server/models/ai-assistant.ts

@@ -1,4 +1,5 @@
 import { type IGrantedGroup, GroupType } from '@growi/core';
+import type mongoose from 'mongoose';
 import { type Model, type Document, Schema } from 'mongoose';
 
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
@@ -8,7 +9,7 @@ import { type AiAssistant, AiAssistantShareScope, AiAssistantAccessScope } from
 export interface AiAssistantDocument extends AiAssistant, Document {}
 
 interface AiAssistantModel extends Model<AiAssistantDocument> {
-  setDefault(id: string, isDefault: boolean): Promise<AiAssistantDocument>;
+  setDefault(id: string, isDefault: boolean, ignoreShareScope?: boolean): Promise<AiAssistantDocument>;
 }
 
 /*
@@ -110,17 +111,46 @@ const schema = new Schema<AiAssistantDocument>(
 );
 
 
-schema.statics.setDefault = async function(id: string, isDefault: boolean): Promise<AiAssistantDocument> {
-  const aiAssistant = await this.findById(id);
+schema.statics.setDefault = async function(id: string, isDefault: boolean, ignoreShareScope = false): Promise<AiAssistantDocument> {
+  const query: mongoose.FilterQuery<AiAssistant> = {
+    _id: id,
+  };
+
+  if (!ignoreShareScope) {
+    query.shareScope = AiAssistantShareScope.PUBLIC_ONLY;
+  }
+
+  const aiAssistant = await this.findOne(query);
   if (aiAssistant == null) {
     throw new Error('AiAssistant not found');
   }
 
-  await this.updateMany({ isDefault: true }, { isDefault: false });
+  if (isDefault) {
+    await this.bulkWrite([
+      {
+        updateOne: {
+          filter: { _id: id },
+          update: { $set: { isDefault: true } },
+        },
+      },
+      {
+        updateMany: {
+          filter: {
+            _id: { $ne: id },
+            isDefault: true,
+          },
+          update: { $set: { isDefault: false } },
+        },
+      },
+    ]);
+  }
 
-  aiAssistant.isDefault = isDefault;
-  const updatedAiAssistant = await aiAssistant.save();
+  else {
+    aiAssistant.isDefault = false;
+    await aiAssistant.save();
+  }
 
+  const updatedAiAssistant = await this.findById(id);
   return updatedAiAssistant;
 };
 

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

@@ -9,7 +9,6 @@ import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
 import loggerFactory from '~/utils/logger';
 
-import { AiAssistantShareScope } from '../../interfaces/ai-assistant';
 import AiAssistantModel from '../models/ai-assistant';
 import { getOpenaiService } from '../services/openai';
 
@@ -50,11 +49,6 @@ export const setDefaultAiAssistantFactory: setDefaultAiAssistantFactory = (crowi
         const { id } = req.params;
         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);
         return res.apiv3({ updatedAiAssistant });
       }

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

@@ -838,7 +838,7 @@ class OpenaiService implements IOpenaiService {
     let updatedAiAssistant: AiAssistantDocument = await aiAssistant.save();
 
     if (data.shareScope !== AiAssistantShareScope.PUBLIC_ONLY && aiAssistant.isDefault) {
-      updatedAiAssistant = await AiAssistantModel.setDefault(aiAssistant._id, false);
+      updatedAiAssistant = await AiAssistantModel.setDefault(aiAssistant._id, false, true);
     }
 
     return updatedAiAssistant;