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
+47 -26
View File
@@ -1,9 +1,9 @@
import { MoreThanOrEqual, LessThanOrEqual } from 'typeorm'
import { MoreThanOrEqual, LessThanOrEqual, Between } from 'typeorm'
import { ChatMessageRatingType, ChatType } from '../Interface'
import { ChatMessage } from '../database/entities/ChatMessage'
import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
import { aMonthAgo, setDateToStartOrEndOfDay } from '.'
import { aMonthAgo } from '.'
/**
* Method that get chat messages.
@@ -18,27 +18,35 @@ import { aMonthAgo, setDateToStartOrEndOfDay } from '.'
* @param {boolean} feedback
* @param {ChatMessageRatingType[]} feedbackTypes
*/
export const utilGetChatMessage = async (
chatflowid: string,
chatType: ChatType | undefined,
sortOrder: string = 'ASC',
chatId?: string,
memoryType?: string,
sessionId?: string,
startDate?: string,
endDate?: string,
messageId?: string,
feedback?: boolean,
interface GetChatMessageParams {
chatflowid: string
chatType?: ChatType
sortOrder?: string
chatId?: string
memoryType?: string
sessionId?: string
startDate?: string
endDate?: string
messageId?: string
feedback?: boolean
feedbackTypes?: ChatMessageRatingType[]
): Promise<ChatMessage[]> => {
}
export const utilGetChatMessage = async ({
chatflowid,
chatType,
sortOrder = 'ASC',
chatId,
memoryType,
sessionId,
startDate,
endDate,
messageId,
feedback,
feedbackTypes
}: GetChatMessageParams): Promise<ChatMessage[]> => {
const appServer = getRunningExpressApp()
let fromDate
if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start')
let toDate
if (endDate) toDate = setDateToStartOrEndOfDay(endDate, 'end')
if (feedback) {
const query = await appServer.AppDataSource.getRepository(ChatMessage).createQueryBuilder('chat_message')
@@ -62,10 +70,13 @@ export const utilGetChatMessage = async (
}
// set date range
query.andWhere('chat_message.createdDate BETWEEN :fromDate AND :toDate', {
fromDate: fromDate ?? aMonthAgo(),
toDate: toDate ?? new Date()
})
if (startDate) {
query.andWhere('chat_message.createdDate >= :startDateTime', { startDateTime: startDate ? new Date(startDate) : aMonthAgo() })
}
if (endDate) {
query.andWhere('chat_message.createdDate <= :endDateTime', { endDateTime: endDate ? new Date(endDate) : new Date() })
}
// sort
query.orderBy('chat_message.createdDate', sortOrder === 'DESC' ? 'DESC' : 'ASC')
@@ -89,6 +100,17 @@ export const utilGetChatMessage = async (
return messages
}
let createdDateQuery
if (startDate || endDate) {
if (startDate && endDate) {
createdDateQuery = Between(new Date(startDate), new Date(endDate))
} else if (startDate) {
createdDateQuery = MoreThanOrEqual(new Date(startDate))
} else if (endDate) {
createdDateQuery = LessThanOrEqual(new Date(endDate))
}
}
return await appServer.AppDataSource.getRepository(ChatMessage).find({
where: {
chatflowid,
@@ -96,8 +118,7 @@ export const utilGetChatMessage = async (
chatId,
memoryType: memoryType ?? undefined,
sessionId: sessionId ?? undefined,
...(fromDate && { createdDate: MoreThanOrEqual(fromDate) }),
...(toDate && { createdDate: LessThanOrEqual(toDate) }),
createdDate: createdDateQuery,
id: messageId ?? undefined
},
order: {
-9
View File
@@ -1745,15 +1745,6 @@ export const convertToValidFilename = (word: string) => {
.toLowerCase()
}
export const setDateToStartOrEndOfDay = (dateTimeStr: string, setHours: 'start' | 'end') => {
const date = new Date(dateTimeStr)
if (isNaN(date.getTime())) {
return undefined
}
setHours === 'start' ? date.setHours(0, 0, 0, 0) : date.setHours(23, 59, 59, 999)
return date
}
export const aMonthAgo = () => {
const date = new Date()
date.setMonth(new Date().getMonth() - 1)