Merge pull request #1716 from Jaredude/bugfix/chatmessage-query-by-chatId-memoryType-isNull-where

fixes bug where querying by chatId results in no records
This commit is contained in:
Henry Heng
2024-02-16 02:41:27 +08:00
committed by GitHub
+15 -5
View File
@@ -10,7 +10,7 @@ import logger from './utils/logger'
import { expressRequestLogger } from './utils/logger' import { expressRequestLogger } from './utils/logger'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import OpenAI from 'openai' import OpenAI from 'openai'
import { Between, IsNull, FindOptionsWhere } from 'typeorm' import { FindOptionsWhere, MoreThanOrEqual, LessThanOrEqual } from 'typeorm'
import { import {
IChatFlow, IChatFlow,
IncomingInput, IncomingInput,
@@ -1445,20 +1445,30 @@ export class App {
endDate?: string, endDate?: string,
messageId?: string messageId?: string
): Promise<ChatMessage[]> { ): Promise<ChatMessage[]> {
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
}
let fromDate let fromDate
if (startDate) fromDate = new Date(startDate) if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start')
let toDate let toDate
if (endDate) toDate = new Date(endDate) if (endDate) toDate = setDateToStartOrEndOfDay(endDate, 'end')
return await this.AppDataSource.getRepository(ChatMessage).find({ return await this.AppDataSource.getRepository(ChatMessage).find({
where: { where: {
chatflowid, chatflowid,
chatType, chatType,
chatId, chatId,
memoryType: memoryType ?? (chatId ? IsNull() : undefined), memoryType: memoryType ?? undefined,
sessionId: sessionId ?? undefined, sessionId: sessionId ?? undefined,
createdDate: toDate && fromDate ? Between(fromDate, toDate) : undefined, ...(fromDate && { createdDate: MoreThanOrEqual(fromDate) }),
...(toDate && { createdDate: LessThanOrEqual(toDate) }),
id: messageId ?? undefined id: messageId ?? undefined
}, },
order: { order: {