Skip to content

Commit c89be26

Browse files
authored
Bugfix/update get chat messages chattype (#3881)
update get chat messages chattype
1 parent 62d5d1e commit c89be26

File tree

6 files changed

+53
-80
lines changed

6 files changed

+53
-80
lines changed

packages/components/credentials/OpenAPIAuth.credential.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/server/src/controllers/chat-messages/index.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import chatflowsService from '../../services/chatflows'
44
import chatMessagesService from '../../services/chat-messages'
55
import { aMonthAgo, clearSessionMemory } from '../../utils'
66
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
7-
import { Between, DeleteResult, FindOptionsWhere } from 'typeorm'
7+
import { Between, DeleteResult, FindOptionsWhere, In } from 'typeorm'
88
import { ChatMessage } from '../../database/entities/ChatMessage'
99
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
1010
import { StatusCodes } from 'http-status-codes'
@@ -49,19 +49,17 @@ const createChatMessage = async (req: Request, res: Response, next: NextFunction
4949

5050
const getAllChatMessages = async (req: Request, res: Response, next: NextFunction) => {
5151
try {
52-
let chatTypeFilter = req.query?.chatType as ChatType | undefined
53-
if (chatTypeFilter) {
52+
const _chatTypes = req.query?.chatType as string | undefined
53+
let chatTypes: ChatType[] | undefined
54+
if (_chatTypes) {
5455
try {
55-
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
56-
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) {
57-
chatTypeFilter = undefined
58-
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) {
59-
chatTypeFilter = ChatType.EXTERNAL
60-
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
61-
chatTypeFilter = ChatType.INTERNAL
56+
if (Array.isArray(_chatTypes)) {
57+
chatTypes = _chatTypes
58+
} else {
59+
chatTypes = JSON.parse(_chatTypes)
6260
}
6361
} catch (e) {
64-
return res.status(500).send(e)
62+
chatTypes = [_chatTypes as ChatType]
6563
}
6664
}
6765
const sortOrder = req.query?.order as string | undefined
@@ -84,7 +82,7 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
8482
}
8583
const apiResponse = await chatMessagesService.getAllChatMessages(
8684
req.params.id,
87-
chatTypeFilter,
85+
chatTypes,
8886
sortOrder,
8987
chatId,
9088
memoryType,
@@ -118,7 +116,7 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
118116
}
119117
const apiResponse = await chatMessagesService.getAllInternalChatMessages(
120118
req.params.id,
121-
ChatType.INTERNAL,
119+
[ChatType.INTERNAL],
122120
sortOrder,
123121
chatId,
124122
memoryType,
@@ -155,7 +153,19 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
155153
const chatId = req.query?.chatId as string
156154
const memoryType = req.query?.memoryType as string | undefined
157155
const sessionId = req.query?.sessionId as string | undefined
158-
const _chatType = req.query?.chatType as string | undefined
156+
const _chatTypes = req.query?.chatType as string | undefined
157+
let chatTypes: ChatType[] | undefined
158+
if (_chatTypes) {
159+
try {
160+
if (Array.isArray(_chatTypes)) {
161+
chatTypes = _chatTypes
162+
} else {
163+
chatTypes = JSON.parse(_chatTypes)
164+
}
165+
} catch (e) {
166+
chatTypes = [_chatTypes as ChatType]
167+
}
168+
}
159169
const startDate = req.query?.startDate as string | undefined
160170
const endDate = req.query?.endDate as string | undefined
161171
const isClearFromViewMessageDialog = req.query?.isClearFromViewMessageDialog as string | undefined
@@ -169,7 +179,7 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
169179
const hardDelete = req.query?.hardDelete as boolean | undefined
170180
const messages = await utilGetChatMessage({
171181
chatflowid,
172-
chatType: _chatType as ChatType | undefined,
182+
chatTypes,
173183
startDate,
174184
endDate,
175185
feedback: isFeedback,
@@ -236,7 +246,9 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
236246
if (chatId) deleteOptions.chatId = chatId
237247
if (memoryType) deleteOptions.memoryType = memoryType
238248
if (sessionId) deleteOptions.sessionId = sessionId
239-
if (_chatType) deleteOptions.chatType = _chatType
249+
if (chatTypes && chatTypes.length > 0) {
250+
deleteOptions.chatType = In(chatTypes)
251+
}
240252
if (startDate && endDate) {
241253
const fromDate = new Date(startDate)
242254
const toDate = new Date(endDate)

packages/server/src/controllers/stats/index.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,29 @@ import { Request, Response, NextFunction } from 'express'
33
import statsService from '../../services/stats'
44
import { ChatMessageRatingType, ChatType } from '../../Interface'
55
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6-
import { getErrorMessage } from '../../errors/utils'
76

87
const getChatflowStats = async (req: Request, res: Response, next: NextFunction) => {
98
try {
109
if (typeof req.params === 'undefined' || !req.params.id) {
1110
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: statsController.getChatflowStats - id not provided!`)
1211
}
1312
const chatflowid = req.params.id
14-
let chatTypeFilter = req.query?.chatType as ChatType | undefined
15-
const startDate = req.query?.startDate as string | undefined
16-
const endDate = req.query?.endDate as string | undefined
17-
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
18-
if (chatTypeFilter) {
13+
const _chatTypes = req.query?.chatType as string | undefined
14+
let chatTypes: ChatType[] | undefined
15+
if (_chatTypes) {
1916
try {
20-
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
21-
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) {
22-
chatTypeFilter = undefined
23-
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) {
24-
chatTypeFilter = ChatType.EXTERNAL
25-
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
26-
chatTypeFilter = ChatType.INTERNAL
17+
if (Array.isArray(_chatTypes)) {
18+
chatTypes = _chatTypes
19+
} else {
20+
chatTypes = JSON.parse(_chatTypes)
2721
}
2822
} catch (e) {
29-
throw new InternalFlowiseError(
30-
StatusCodes.INTERNAL_SERVER_ERROR,
31-
`Error: statsController.getChatflowStats - ${getErrorMessage(e)}`
32-
)
23+
chatTypes = [_chatTypes as ChatType]
3324
}
3425
}
26+
const startDate = req.query?.startDate as string | undefined
27+
const endDate = req.query?.endDate as string | undefined
28+
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
3529
if (feedbackTypeFilters) {
3630
try {
3731
const feedbackTypeFilterArray = JSON.parse(JSON.stringify(feedbackTypeFilters))
@@ -51,15 +45,7 @@ const getChatflowStats = async (req: Request, res: Response, next: NextFunction)
5145
return res.status(500).send(e)
5246
}
5347
}
54-
const apiResponse = await statsService.getChatflowStats(
55-
chatflowid,
56-
chatTypeFilter,
57-
startDate,
58-
endDate,
59-
'',
60-
true,
61-
feedbackTypeFilters
62-
)
48+
const apiResponse = await statsService.getChatflowStats(chatflowid, chatTypes, startDate, endDate, '', true, feedbackTypeFilters)
6349
return res.json(apiResponse)
6450
} catch (error) {
6551
next(error)

packages/server/src/services/chat-messages/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const createChatMessage = async (chatMessage: Partial<IChatMessage>) => {
2727
// Get all chatmessages from chatflowid
2828
const getAllChatMessages = async (
2929
chatflowId: string,
30-
chatTypeFilter: ChatType | undefined,
30+
chatTypes: ChatType[] | undefined,
3131
sortOrder: string = 'ASC',
3232
chatId?: string,
3333
memoryType?: string,
@@ -41,7 +41,7 @@ const getAllChatMessages = async (
4141
try {
4242
const dbResponse = await utilGetChatMessage({
4343
chatflowid: chatflowId,
44-
chatType: chatTypeFilter,
44+
chatTypes,
4545
sortOrder,
4646
chatId,
4747
memoryType,
@@ -64,7 +64,7 @@ const getAllChatMessages = async (
6464
// Get internal chatmessages from chatflowid
6565
const getAllInternalChatMessages = async (
6666
chatflowId: string,
67-
chatTypeFilter: ChatType | undefined,
67+
chatTypes: ChatType[] | undefined,
6868
sortOrder: string = 'ASC',
6969
chatId?: string,
7070
memoryType?: string,
@@ -78,7 +78,7 @@ const getAllInternalChatMessages = async (
7878
try {
7979
const dbResponse = await utilGetChatMessage({
8080
chatflowid: chatflowId,
81-
chatType: chatTypeFilter,
81+
chatTypes,
8282
sortOrder,
8383
chatId,
8484
memoryType,

packages/server/src/services/stats/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getErrorMessage } from '../../errors/utils'
99
// get stats for showing in chatflow
1010
const getChatflowStats = async (
1111
chatflowid: string,
12-
chatTypeFilter: ChatType | undefined,
12+
chatTypes: ChatType[] | undefined,
1313
startDate?: string,
1414
endDate?: string,
1515
messageId?: string,
@@ -19,7 +19,7 @@ const getChatflowStats = async (
1919
try {
2020
const chatmessages = (await utilGetChatMessage({
2121
chatflowid,
22-
chatType: chatTypeFilter,
22+
chatTypes,
2323
startDate,
2424
endDate,
2525
messageId,

packages/server/src/utils/getChatMessage.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MoreThanOrEqual, LessThanOrEqual, Between } from 'typeorm'
1+
import { MoreThanOrEqual, LessThanOrEqual, Between, In } from 'typeorm'
22
import { ChatMessageRatingType, ChatType } from '../Interface'
33
import { ChatMessage } from '../database/entities/ChatMessage'
44
import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
@@ -8,7 +8,7 @@ import { aMonthAgo } from '.'
88
/**
99
* Method that get chat messages.
1010
* @param {string} chatflowid
11-
* @param {ChatType} chatType
11+
* @param {ChatType[]} chatTypes
1212
* @param {string} sortOrder
1313
* @param {string} chatId
1414
* @param {string} memoryType
@@ -20,7 +20,7 @@ import { aMonthAgo } from '.'
2020
*/
2121
interface GetChatMessageParams {
2222
chatflowid: string
23-
chatType?: ChatType
23+
chatTypes?: ChatType[]
2424
sortOrder?: string
2525
chatId?: string
2626
memoryType?: string
@@ -34,7 +34,7 @@ interface GetChatMessageParams {
3434

3535
export const utilGetChatMessage = async ({
3636
chatflowid,
37-
chatType,
37+
chatTypes,
3838
sortOrder = 'ASC',
3939
chatId,
4040
memoryType,
@@ -56,8 +56,8 @@ export const utilGetChatMessage = async ({
5656
.where('chat_message.chatflowid = :chatflowid', { chatflowid })
5757

5858
// based on which parameters are available add `andWhere` clauses to the query
59-
if (chatType) {
60-
query.andWhere('chat_message.chatType = :chatType', { chatType })
59+
if (chatTypes && chatTypes.length > 0) {
60+
query.andWhere('chat_message.chatType IN (:...chatTypes)', { chatTypes })
6161
}
6262
if (chatId) {
6363
query.andWhere('chat_message.chatId = :chatId', { chatId })
@@ -114,7 +114,7 @@ export const utilGetChatMessage = async ({
114114
return await appServer.AppDataSource.getRepository(ChatMessage).find({
115115
where: {
116116
chatflowid,
117-
chatType,
117+
chatType: chatTypes?.length ? In(chatTypes) : undefined,
118118
chatId,
119119
memoryType: memoryType ?? undefined,
120120
sessionId: sessionId ?? undefined,

0 commit comments

Comments
 (0)