1
- import { MoreThanOrEqual , LessThanOrEqual } from 'typeorm'
1
+ import { MoreThanOrEqual , LessThanOrEqual , Between } from 'typeorm'
2
2
import { ChatMessageRatingType , ChatType } from '../Interface'
3
3
import { ChatMessage } from '../database/entities/ChatMessage'
4
4
import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
5
5
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
6
- import { aMonthAgo , setDateToStartOrEndOfDay } from '.'
6
+ import { aMonthAgo } from '.'
7
7
8
8
/**
9
9
* Method that get chat messages.
@@ -18,26 +18,34 @@ import { aMonthAgo, setDateToStartOrEndOfDay } from '.'
18
18
* @param {boolean } feedback
19
19
* @param {ChatMessageRatingType[] } feedbackTypes
20
20
*/
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
32
32
feedbackTypes ?: ChatMessageRatingType [ ]
33
- ) : Promise < ChatMessage [ ] > => {
34
- const appServer = getRunningExpressApp ( )
35
-
36
- let fromDate
37
- if ( startDate ) fromDate = setDateToStartOrEndOfDay ( startDate , 'start' )
33
+ }
38
34
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 ( )
41
49
42
50
if ( feedback ) {
43
51
const query = await appServer . AppDataSource . getRepository ( ChatMessage ) . createQueryBuilder ( 'chat_message' )
@@ -62,10 +70,13 @@ export const utilGetChatMessage = async (
62
70
}
63
71
64
72
// 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
+
69
80
// sort
70
81
query . orderBy ( 'chat_message.createdDate' , sortOrder === 'DESC' ? 'DESC' : 'ASC' )
71
82
@@ -89,15 +100,25 @@ export const utilGetChatMessage = async (
89
100
return messages
90
101
}
91
102
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
+
92
114
return await appServer . AppDataSource . getRepository ( ChatMessage ) . find ( {
93
115
where : {
94
116
chatflowid,
95
117
chatType,
96
118
chatId,
97
119
memoryType : memoryType ?? undefined ,
98
120
sessionId : sessionId ?? undefined ,
99
- ...( fromDate && { createdDate : MoreThanOrEqual ( fromDate ) } ) ,
100
- ...( toDate && { createdDate : LessThanOrEqual ( toDate ) } ) ,
121
+ createdDate : createdDateQuery ,
101
122
id : messageId ?? undefined
102
123
} ,
103
124
order : {
0 commit comments