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.
This commit is contained in:
Jared Tracy 2024-02-13 10:44:05 -06:00
parent 55c2a8612b
commit 6de1e8acec
1 changed files with 11 additions and 2 deletions

View File

@ -1442,11 +1442,20 @@ export class App {
startDate?: string, startDate?: string,
endDate?: string endDate?: 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: {