ソースを参照

Refactor AiAssistantModel.setDefault

Shun Miyazawa 1 年間 前
コミット
04c2fc0043

+ 7 - 20
apps/app/src/features/openai/server/models/ai-assistant.ts

@@ -1,5 +1,4 @@
 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';
@@ -9,7 +8,7 @@ import { type AiAssistant, AiAssistantShareScope, AiAssistantAccessScope } from
 export interface AiAssistantDocument extends AiAssistant, Document {}
 
 interface AiAssistantModel extends Model<AiAssistantDocument> {
-  setDefault(id: string, isDefault: boolean, ignoreShareScope?: boolean): Promise<AiAssistantDocument>;
+  setDefault(id: string, isDefault: boolean): Promise<AiAssistantDocument>;
 }
 
 /*
@@ -111,25 +110,15 @@ const schema = new Schema<AiAssistantDocument>(
 );
 
 
-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');
-  }
-
+schema.statics.setDefault = async function(id: string, isDefault: boolean): Promise<AiAssistantDocument> {
   if (isDefault) {
     await this.bulkWrite([
       {
         updateOne: {
-          filter: { _id: id },
+          filter: {
+            _id: id,
+            shareScope:  AiAssistantShareScope.PUBLIC_ONLY,
+          },
           update: { $set: { isDefault: true } },
         },
       },
@@ -144,10 +133,8 @@ schema.statics.setDefault = async function(id: string, isDefault: boolean, ignor
       },
     ]);
   }
-
   else {
-    aiAssistant.isDefault = false;
-    await aiAssistant.save();
+    await this.findByIdAndUpdate(id, { isDefault: false });
   }
 
   const updatedAiAssistant = await this.findById(id);

+ 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, true);
+      updatedAiAssistant = await AiAssistantModel.setDefault(aiAssistant._id, false);
     }
 
     return updatedAiAssistant;