Get/Delete ChatMessage based on startDateTime / endDateTime (#3867)

* Get ChatMessage based on startDateTime / endDateTime - supplements existing startDate / endDate fields

* Proper query handling for between case

* Return 0 result rather than error

* lint fix

* update start date and end date query

---------

Co-authored-by: Henry <hzj94@hotmail.com>
This commit is contained in:
Ryan Halliday
2025-01-15 03:06:42 +13:00
committed by GitHub
parent aab493c3c7
commit 16aa3a0d29
8 changed files with 100 additions and 90 deletions
@@ -2,9 +2,9 @@ import { Request, Response, NextFunction } from 'express'
import { ChatMessageRatingType, ChatType, IReactFlowObject } from '../../Interface'
import chatflowsService from '../../services/chatflows'
import chatMessagesService from '../../services/chat-messages'
import { aMonthAgo, clearSessionMemory, setDateToStartOrEndOfDay } from '../../utils'
import { aMonthAgo, clearSessionMemory } from '../../utils'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Between, FindOptionsWhere } from 'typeorm'
import { Between, DeleteResult, FindOptionsWhere } from 'typeorm'
import { ChatMessage } from '../../database/entities/ChatMessage'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
@@ -167,21 +167,21 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
if (!chatId) {
const isFeedback = feedbackTypeFilters?.length ? true : false
const hardDelete = req.query?.hardDelete as boolean | undefined
const messages = await utilGetChatMessage(
const messages = await utilGetChatMessage({
chatflowid,
_chatType as ChatType | undefined,
undefined,
undefined,
undefined,
undefined,
chatType: _chatType as ChatType | undefined,
startDate,
endDate,
undefined,
isFeedback,
feedbackTypeFilters
)
feedback: isFeedback,
feedbackTypes: feedbackTypeFilters
})
const messageIds = messages.map((message) => message.id)
if (messages.length === 0) {
const result: DeleteResult = { raw: [], affected: 0 }
return res.json(result)
}
// Categorize by chatId_memoryType_sessionId
const chatIdMap = new Map<string, ChatMessage[]>()
messages.forEach((message) => {
@@ -238,8 +238,8 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
if (sessionId) deleteOptions.sessionId = sessionId
if (_chatType) deleteOptions.chatType = _chatType
if (startDate && endDate) {
const fromDate = setDateToStartOrEndOfDay(startDate, 'start')
const toDate = setDateToStartOrEndOfDay(endDate, 'end')
const fromDate = new Date(startDate)
const toDate = new Date(endDate)
deleteOptions.createdDate = Between(fromDate ?? aMonthAgo(), toDate ?? new Date())
}
const apiResponse = await chatMessagesService.removeAllChatMessages(chatId, chatflowid, deleteOptions)