1- import { MoreThanOrEqual , LessThanOrEqual } from 'typeorm'
1+ import { MoreThanOrEqual , LessThanOrEqual , Between } from 'typeorm'
22import { ChatMessageRatingType , ChatType } from '../Interface'
33import { ChatMessage } from '../database/entities/ChatMessage'
44import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
55import { getRunningExpressApp } from '../utils/getRunningExpressApp'
6- import { aMonthAgo , setDateToStartOrEndOfDay } from '.'
6+ import { aMonthAgo } from '.'
77
88/**
99 * Method that get chat messages.
@@ -18,26 +18,34 @@ import { aMonthAgo, setDateToStartOrEndOfDay } from '.'
1818 * @param {boolean } feedback
1919 * @param {ChatMessageRatingType[] } feedbackTypes
2020 */
21- export const utilGetChatMessage = async (
22- chatflowid : string ,
23- chatType : ChatType | undefined ,
24- sortOrder : string = 'ASC' ,
25- chatId ?: string ,
26- memoryType ?: string ,
27- sessionId ?: string ,
28- startDate ?: string ,
29- endDate ?: string ,
30- messageId ?: string ,
31- feedback ?: boolean ,
21+ interface GetChatMessageParams {
22+ chatflowid : string
23+ chatType ? : ChatType
24+ sortOrder ? : string
25+ chatId ?: string
26+ memoryType ?: string
27+ sessionId ?: string
28+ startDate ?: string
29+ endDate ?: string
30+ messageId ?: string
31+ feedback ?: boolean
3232 feedbackTypes ?: ChatMessageRatingType [ ]
33- ) : Promise < ChatMessage [ ] > => {
34- const appServer = getRunningExpressApp ( )
35-
36- let fromDate
37- if ( startDate ) fromDate = setDateToStartOrEndOfDay ( startDate , 'start' )
33+ }
3834
39- let toDate
40- if ( endDate ) toDate = setDateToStartOrEndOfDay ( endDate , 'end' )
35+ export const utilGetChatMessage = async ( {
36+ chatflowid,
37+ chatType,
38+ sortOrder = 'ASC' ,
39+ chatId,
40+ memoryType,
41+ sessionId,
42+ startDate,
43+ endDate,
44+ messageId,
45+ feedback,
46+ feedbackTypes
47+ } : GetChatMessageParams ) : Promise < ChatMessage [ ] > => {
48+ const appServer = getRunningExpressApp ( )
4149
4250 if ( feedback ) {
4351 const query = await appServer . AppDataSource . getRepository ( ChatMessage ) . createQueryBuilder ( 'chat_message' )
@@ -62,10 +70,13 @@ export const utilGetChatMessage = async (
6270 }
6371
6472 // set date range
65- query . andWhere ( 'chat_message.createdDate BETWEEN :fromDate AND :toDate' , {
66- fromDate : fromDate ?? aMonthAgo ( ) ,
67- toDate : toDate ?? new Date ( )
68- } )
73+ if ( startDate ) {
74+ query . andWhere ( 'chat_message.createdDate >= :startDateTime' , { startDateTime : startDate ? new Date ( startDate ) : aMonthAgo ( ) } )
75+ }
76+ if ( endDate ) {
77+ query . andWhere ( 'chat_message.createdDate <= :endDateTime' , { endDateTime : endDate ? new Date ( endDate ) : new Date ( ) } )
78+ }
79+
6980 // sort
7081 query . orderBy ( 'chat_message.createdDate' , sortOrder === 'DESC' ? 'DESC' : 'ASC' )
7182
@@ -89,15 +100,25 @@ export const utilGetChatMessage = async (
89100 return messages
90101 }
91102
103+ let createdDateQuery
104+ if ( startDate || endDate ) {
105+ if ( startDate && endDate ) {
106+ createdDateQuery = Between ( new Date ( startDate ) , new Date ( endDate ) )
107+ } else if ( startDate ) {
108+ createdDateQuery = MoreThanOrEqual ( new Date ( startDate ) )
109+ } else if ( endDate ) {
110+ createdDateQuery = LessThanOrEqual ( new Date ( endDate ) )
111+ }
112+ }
113+
92114 return await appServer . AppDataSource . getRepository ( ChatMessage ) . find ( {
93115 where : {
94116 chatflowid,
95117 chatType,
96118 chatId,
97119 memoryType : memoryType ?? undefined ,
98120 sessionId : sessionId ?? undefined ,
99- ...( fromDate && { createdDate : MoreThanOrEqual ( fromDate ) } ) ,
100- ...( toDate && { createdDate : LessThanOrEqual ( toDate ) } ) ,
121+ createdDate : createdDateQuery ,
101122 id : messageId ?? undefined
102123 } ,
103124 order : {
0 commit comments