perf: completion chatId

This commit is contained in:
archer
2023-07-23 20:07:35 +08:00
parent b7d18e38d1
commit 67e10d6f2c
35 changed files with 447 additions and 385 deletions

View File

@@ -0,0 +1,65 @@
import { ChatItemType } from '@/types/chat';
import { Chat, App } from '@/service/mongo';
import { ChatSourceEnum } from '@/constants/chat';
type Props = {
chatId: string;
appId: string;
userId: string;
variables?: Record<string, any>;
isOwner: boolean;
source: `${ChatSourceEnum}`;
shareId?: string;
content: [ChatItemType, ChatItemType];
};
export async function saveChat({
chatId,
appId,
userId,
variables,
isOwner,
source,
shareId,
content
}: Props) {
const chatHistory = await Chat.findOne(
{
chatId,
userId
},
'_id'
);
if (chatHistory) {
await Chat.findOneAndUpdate(
{ chatId },
{
$push: {
content: {
$each: content
}
},
title: content[0].value.slice(0, 20),
updateTime: new Date()
}
);
} else {
await Chat.create({
chatId,
userId,
appId,
variables,
title: content[0].value.slice(0, 20),
source,
shareId,
content: content
});
}
if (isOwner && source === ChatSourceEnum.online) {
App.findByIdAndUpdate(appId, {
updateTime: new Date()
});
}
}