forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
63 lines (58 loc) · 2.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { StatusCodes } from 'http-status-codes'
import { IUser } from '../../Interface'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
import { Chat } from '../../database/entities/Chat'
import { Not, IsNull } from 'typeorm'
const getAllChats = async (user: IUser) => {
try {
const appServer = getRunningExpressApp()
const chats = await appServer.AppDataSource.getRepository(Chat).find({
where: {
owner: { id: user.id },
organization: { id: user.organizationId },
chatflowChatId: Not(IsNull())
},
order: {
createdDate: 'DESC'
}
})
return JSON.parse(JSON.stringify(chats))
} catch (error) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: chatsService.getAllChats - ${getErrorMessage(error)}`)
}
}
const getChatById = async (chatId: string, user: IUser) => {
try {
const appServer = getRunningExpressApp()
const chat = await appServer.AppDataSource.getRepository(Chat).findOne({
where: {
id: chatId,
owner: { id: user.id },
organization: { id: user.organizationId }
},
relations: {
chatflow: true
// users: true,
// messages: true
},
order: {
updatedDate: 'DESC'
// messages: {
// createdDate: 'ASC'
// }
}
})
if (!chat) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chat ${chatId} not found`)
}
return JSON.parse(JSON.stringify(chat))
} catch (error) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: chatsService.getChatById - ${getErrorMessage(error)}`)
}
}
export default {
getAllChats,
getChatById
}