From 40d220ed7b90246e33454f190278dcadbcfbc00f Mon Sep 17 00:00:00 2001 From: Sammy Date: Mon, 12 Jun 2023 19:43:33 +0200 Subject: [PATCH] refactor: use date-fns for date comparison --- server/events/guildScheduledEventCreate.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server/events/guildScheduledEventCreate.ts b/server/events/guildScheduledEventCreate.ts index 9b51552..f0b66b6 100644 --- a/server/events/guildScheduledEventCreate.ts +++ b/server/events/guildScheduledEventCreate.ts @@ -5,6 +5,8 @@ import { jellyfinHandler } from "../.."; import { closePoll } from "../commands/closepoll"; import { config } from "../configuration"; import { logger } from "../logger"; +import toDate from "date-fns/fp/toDate"; +import { addDays, isAfter, isBefore } from "date-fns"; export const name = 'guildScheduledEventCreate' @@ -73,10 +75,14 @@ async function checkForPollsToClose(event: GuildScheduledEvent): Promise { logger.error("Event does not have a scheduled start time. Cancelling", { guildId: event.guildId, requestId }) return } - if ((updatedEvent.scheduledStartTimestamp - Date.now()) <= (1000 * 60 * 60 * 24 * 2)) { + + const eventDate: Date = toDate(updatedEvent.scheduledStartTimestamp) + const closePollDate: Date = addDays(eventDate, -2) + + if (isAfter(Date.now(), closePollDate)) { logger.info("Less than two days until event. Closing poll", { guildId: event.guildId, requestId }) closePoll(event.guild, requestId) } else { - logger.info(`ScheduledStart: ${updatedEvent.scheduledStartTimestamp}. Now: ${Date.now()}`, { guildId: event.guildId, requestId }) + logger.info(`ScheduledStart: ${closePollDate}. Now: ${toDate(Date.now())}`, { guildId: event.guildId, requestId }) } } \ No newline at end of file