mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 19:00:59 +03:00
Bugfix/update get chat messages chattype (#3881)
update get chat messages chattype
This commit is contained in:
@@ -4,7 +4,7 @@ import chatflowsService from '../../services/chatflows'
|
||||
import chatMessagesService from '../../services/chat-messages'
|
||||
import { aMonthAgo, clearSessionMemory } from '../../utils'
|
||||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
|
||||
import { Between, DeleteResult, FindOptionsWhere } from 'typeorm'
|
||||
import { Between, DeleteResult, FindOptionsWhere, In } from 'typeorm'
|
||||
import { ChatMessage } from '../../database/entities/ChatMessage'
|
||||
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
@@ -49,19 +49,17 @@ const createChatMessage = async (req: Request, res: Response, next: NextFunction
|
||||
|
||||
const getAllChatMessages = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
let chatTypeFilter = req.query?.chatType as ChatType | undefined
|
||||
if (chatTypeFilter) {
|
||||
const _chatTypes = req.query?.chatType as string | undefined
|
||||
let chatTypes: ChatType[] | undefined
|
||||
if (_chatTypes) {
|
||||
try {
|
||||
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
|
||||
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) {
|
||||
chatTypeFilter = undefined
|
||||
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) {
|
||||
chatTypeFilter = ChatType.EXTERNAL
|
||||
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
|
||||
chatTypeFilter = ChatType.INTERNAL
|
||||
if (Array.isArray(_chatTypes)) {
|
||||
chatTypes = _chatTypes
|
||||
} else {
|
||||
chatTypes = JSON.parse(_chatTypes)
|
||||
}
|
||||
} catch (e) {
|
||||
return res.status(500).send(e)
|
||||
chatTypes = [_chatTypes as ChatType]
|
||||
}
|
||||
}
|
||||
const sortOrder = req.query?.order as string | undefined
|
||||
@@ -84,7 +82,7 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
|
||||
}
|
||||
const apiResponse = await chatMessagesService.getAllChatMessages(
|
||||
req.params.id,
|
||||
chatTypeFilter,
|
||||
chatTypes,
|
||||
sortOrder,
|
||||
chatId,
|
||||
memoryType,
|
||||
@@ -118,7 +116,7 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
|
||||
}
|
||||
const apiResponse = await chatMessagesService.getAllInternalChatMessages(
|
||||
req.params.id,
|
||||
ChatType.INTERNAL,
|
||||
[ChatType.INTERNAL],
|
||||
sortOrder,
|
||||
chatId,
|
||||
memoryType,
|
||||
@@ -155,7 +153,19 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
|
||||
const chatId = req.query?.chatId as string
|
||||
const memoryType = req.query?.memoryType as string | undefined
|
||||
const sessionId = req.query?.sessionId as string | undefined
|
||||
const _chatType = req.query?.chatType as string | undefined
|
||||
const _chatTypes = req.query?.chatType as string | undefined
|
||||
let chatTypes: ChatType[] | undefined
|
||||
if (_chatTypes) {
|
||||
try {
|
||||
if (Array.isArray(_chatTypes)) {
|
||||
chatTypes = _chatTypes
|
||||
} else {
|
||||
chatTypes = JSON.parse(_chatTypes)
|
||||
}
|
||||
} catch (e) {
|
||||
chatTypes = [_chatTypes as ChatType]
|
||||
}
|
||||
}
|
||||
const startDate = req.query?.startDate as string | undefined
|
||||
const endDate = req.query?.endDate as string | undefined
|
||||
const isClearFromViewMessageDialog = req.query?.isClearFromViewMessageDialog as string | undefined
|
||||
@@ -169,7 +179,7 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
|
||||
const hardDelete = req.query?.hardDelete as boolean | undefined
|
||||
const messages = await utilGetChatMessage({
|
||||
chatflowid,
|
||||
chatType: _chatType as ChatType | undefined,
|
||||
chatTypes,
|
||||
startDate,
|
||||
endDate,
|
||||
feedback: isFeedback,
|
||||
@@ -236,7 +246,9 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
|
||||
if (chatId) deleteOptions.chatId = chatId
|
||||
if (memoryType) deleteOptions.memoryType = memoryType
|
||||
if (sessionId) deleteOptions.sessionId = sessionId
|
||||
if (_chatType) deleteOptions.chatType = _chatType
|
||||
if (chatTypes && chatTypes.length > 0) {
|
||||
deleteOptions.chatType = In(chatTypes)
|
||||
}
|
||||
if (startDate && endDate) {
|
||||
const fromDate = new Date(startDate)
|
||||
const toDate = new Date(endDate)
|
||||
|
||||
@@ -3,7 +3,6 @@ import { Request, Response, NextFunction } from 'express'
|
||||
import statsService from '../../services/stats'
|
||||
import { ChatMessageRatingType, ChatType } from '../../Interface'
|
||||
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
|
||||
import { getErrorMessage } from '../../errors/utils'
|
||||
|
||||
const getChatflowStats = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
@@ -11,27 +10,22 @@ const getChatflowStats = async (req: Request, res: Response, next: NextFunction)
|
||||
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: statsController.getChatflowStats - id not provided!`)
|
||||
}
|
||||
const chatflowid = req.params.id
|
||||
let chatTypeFilter = req.query?.chatType as ChatType | undefined
|
||||
const _chatTypes = req.query?.chatType as string | undefined
|
||||
let chatTypes: ChatType[] | undefined
|
||||
if (_chatTypes) {
|
||||
try {
|
||||
if (Array.isArray(_chatTypes)) {
|
||||
chatTypes = _chatTypes
|
||||
} else {
|
||||
chatTypes = JSON.parse(_chatTypes)
|
||||
}
|
||||
} catch (e) {
|
||||
chatTypes = [_chatTypes as ChatType]
|
||||
}
|
||||
}
|
||||
const startDate = req.query?.startDate as string | undefined
|
||||
const endDate = req.query?.endDate as string | undefined
|
||||
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
|
||||
if (chatTypeFilter) {
|
||||
try {
|
||||
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
|
||||
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) {
|
||||
chatTypeFilter = undefined
|
||||
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) {
|
||||
chatTypeFilter = ChatType.EXTERNAL
|
||||
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
|
||||
chatTypeFilter = ChatType.INTERNAL
|
||||
}
|
||||
} catch (e) {
|
||||
throw new InternalFlowiseError(
|
||||
StatusCodes.INTERNAL_SERVER_ERROR,
|
||||
`Error: statsController.getChatflowStats - ${getErrorMessage(e)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
if (feedbackTypeFilters) {
|
||||
try {
|
||||
const feedbackTypeFilterArray = JSON.parse(JSON.stringify(feedbackTypeFilters))
|
||||
@@ -51,15 +45,7 @@ const getChatflowStats = async (req: Request, res: Response, next: NextFunction)
|
||||
return res.status(500).send(e)
|
||||
}
|
||||
}
|
||||
const apiResponse = await statsService.getChatflowStats(
|
||||
chatflowid,
|
||||
chatTypeFilter,
|
||||
startDate,
|
||||
endDate,
|
||||
'',
|
||||
true,
|
||||
feedbackTypeFilters
|
||||
)
|
||||
const apiResponse = await statsService.getChatflowStats(chatflowid, chatTypes, startDate, endDate, '', true, feedbackTypeFilters)
|
||||
return res.json(apiResponse)
|
||||
} catch (error) {
|
||||
next(error)
|
||||
|
||||
Reference in New Issue
Block a user