From d89f9a8825b37d09cc9115f1f01655bc1a786399 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Wed, 6 Jul 2022 22:50:44 +0200 Subject: [PATCH] endDate is exclusive for allday events --- helpers/date.ts | 4 ++++ lib/googlecalendar.ts | 25 +++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/helpers/date.ts b/helpers/date.ts index 30c2e5b..43014ad 100644 --- a/helpers/date.ts +++ b/helpers/date.ts @@ -80,3 +80,7 @@ export function nowInTz(timezone = 'Europe/Berlin'): Date { const now = new Date() return utcToZonedTime(now, timezone) } + +export function getNextDay(date: Date) { + return addDays(date, 1) +} diff --git a/lib/googlecalendar.ts b/lib/googlecalendar.ts index 4de4b81..6d49db7 100644 --- a/lib/googlecalendar.ts +++ b/lib/googlecalendar.ts @@ -1,7 +1,7 @@ import { google } from 'googleapis' import { getBaseURL } from '../helpers/url' import { Booking } from '../db/booking' -import { getDays } from '../helpers/date' +import { getDays, getNextDay, dateFormatBackend } from '../helpers/date' import { log } from '../helpers/log' const calendarId = process.env.GOOGLE_CALENDAR_ID @@ -66,18 +66,19 @@ function getDescription(booking: Booking): string { } export async function createCalendarEvent(booking: Booking): Promise { - const response = await calendar.events.insert( - { - calendarId, - requestBody: { - summary: getSummary(booking), - description: getDescription(booking), - start: { date: booking.startDate }, - end: { date: booking.endDate }, - }, - }, - {} + const exclusiveEndDate = dateFormatBackend( + getNextDay(new Date(booking.endDate)) ) + const data = { + calendarId, + requestBody: { + summary: getSummary(booking), + description: getDescription(booking), + start: { date: booking.startDate }, + end: { date: exclusiveEndDate }, + }, + } + const response = await calendar.events.insert(data, {}) booking.calendarEventId = response.data.id