From 55c2a8612b9065a1ee5d2cbafb0dfb34fed53710 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Mon, 12 Feb 2024 20:12:54 -0600 Subject: [PATCH 1/3] fixes bug where querying by chatId results in no records previous logic for where clause of memoryType checked for existence of chatId and would query for memoryType is null This where clause logic results in an empty result for any request to the endpoint that filters by chatId --- packages/server/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 07797f32..a6566a5b 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1453,7 +1453,7 @@ export class App { chatflowid, chatType, chatId, - memoryType: memoryType ?? (chatId ? IsNull() : undefined), + memoryType: memoryType ?? undefined, sessionId: sessionId ?? undefined, createdDate: toDate && fromDate ? Between(fromDate, toDate) : undefined }, From 6de1e8aceccccf86e2bf78a13d7c533ea2a0679b Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Tue, 13 Feb 2024 10:44:05 -0600 Subject: [PATCH 2/3] handles invalid values for startDate, endDate also sets time window to beginning of the date or end of the date. This will be helpful with the timezone gap between the server (usually UTC) and the client (localized timezone) Ideal date solution to consider for the future would be to adjust the timestamp query based upon the server timezone setup. This becomes particularly helpful when the client is filtering by the end date but they are in a timezone behind UTC after the UTC has advanced to the next date. For example, being in Pacific time and querying for the current date after 4PM will result in not finding records that have been created after 4PM Pacific b/c the server timestamp (in UTC) will be the next calendar date. --- packages/server/src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index a6566a5b..d3453320 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1442,11 +1442,20 @@ export class App { startDate?: string, endDate?: string ): Promise { + 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 - if (startDate) fromDate = new Date(startDate) + if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start') let toDate - if (endDate) toDate = new Date(endDate) + if (endDate) toDate = setDateToStartOrEndOfDay(endDate, 'end') return await this.AppDataSource.getRepository(ChatMessage).find({ where: { From 4d7c7d6ef5df5db53576ddd9e80bbffbd6acd952 Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Tue, 13 Feb 2024 11:11:53 -0600 Subject: [PATCH 3/3] Changes getMessage date logic from Between to >= and <= to fix issue if invalid startDate is passed in also cleans up imports from typeorm lib --- packages/server/src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index d3453320..f401bbc1 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -10,7 +10,7 @@ import logger from './utils/logger' import { expressRequestLogger } from './utils/logger' import { v4 as uuidv4 } from 'uuid' import OpenAI from 'openai' -import { Between, IsNull, FindOptionsWhere } from 'typeorm' +import { FindOptionsWhere, MoreThanOrEqual, LessThanOrEqual } from 'typeorm' import { IChatFlow, IncomingInput, @@ -1464,7 +1464,8 @@ export class App { chatId, memoryType: memoryType ?? undefined, sessionId: sessionId ?? undefined, - createdDate: toDate && fromDate ? Between(fromDate, toDate) : undefined + ...(fromDate && { createdDate: MoreThanOrEqual(fromDate) }), + ...(toDate && { createdDate: LessThanOrEqual(toDate) }) }, order: { createdDate: sortOrder === 'DESC' ? 'DESC' : 'ASC'