also get booked days from calendar

This commit is contained in:
Thomas Ruoff
2022-04-01 00:12:40 +02:00
parent cd58a535cb
commit c31448ff9c
5 changed files with 43 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
import { google } from 'googleapis'
import { getBaseURL } from '../helpers/url';
import { Booking } from '../db/booking'
import { getDays } from '../helpers/date';
const calendarId = process.env.GOOGLE_CALENDAR_ID
let credentials: object
@@ -23,13 +25,21 @@ const calendar = google.calendar({
auth,
})
export async function getNewEvents() {
export async function getBookedDays() {
const { data } = await calendar.events.list({
calendarId,
timeMin: new Date().toISOString(),
timeZone: 'utc',
})
return data.items
// ignore non all-day events
.filter(event => !!event.start.date)
.flatMap(event => getDays({
startDate: new Date(event.start.date),
endDate: new Date(event.end.date),
endDateExclusive: true
}))
}
function getSummary(booking: Partial<Booking>): string {
@@ -44,12 +54,18 @@ function getSummary(booking: Partial<Booking>): string {
return summary
}
function getDescription(booking: Booking): string {
const bookingUrl = `${getBaseURL()}/admin/booking/${booking.uuid}`;
return `Managelink ${bookingUrl}`;
}
export async function createCalendarEvent(booking: Booking): Promise<Booking> {
const response = await calendar.events.insert({
calendarId,
requestBody: {
summary: getSummary(booking),
// TODO: description,
description: getDescription(booking),
start: { date: booking.startDate },
end: { date: booking.endDate },
},
@@ -64,7 +80,9 @@ export async function deleteCalendarEvent(booking: Booking) {
await calendar.events.delete({
calendarId,
eventId: booking.calendarEventId,
})
// TODO: really useful?
sendNotifications: true,
});
booking.calendarEventId = null
}