From 6de1e8aceccccf86e2bf78a13d7c533ea2a0679b Mon Sep 17 00:00:00 2001 From: Jared Tracy Date: Tue, 13 Feb 2024 10:44:05 -0600 Subject: [PATCH] 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 a6566a5bb..d3453320e 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: {