mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
fix all the formatting
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
import { google } from 'googleapis'
|
||||
import { getBaseURL } from '../helpers/url';
|
||||
import { getBaseURL } from '../helpers/url'
|
||||
import { Booking } from '../db/booking'
|
||||
import { getDays } from '../helpers/date';
|
||||
import { getDays } from '../helpers/date'
|
||||
|
||||
const calendarId = process.env.GOOGLE_CALENDAR_ID
|
||||
let credentials: object
|
||||
|
||||
try {
|
||||
credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON);
|
||||
credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON)
|
||||
} catch (error) {
|
||||
console.error('Unable to parse process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON - invalid JSON?');
|
||||
throw error;
|
||||
console.error(
|
||||
'Unable to parse process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON - invalid JSON?'
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
credentials,
|
||||
scopes: [
|
||||
'https://www.googleapis.com/auth/calendar',
|
||||
],
|
||||
scopes: ['https://www.googleapis.com/auth/calendar'],
|
||||
})
|
||||
|
||||
const calendar = google.calendar({
|
||||
@@ -32,14 +32,18 @@ export async function getBookedDays() {
|
||||
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
|
||||
}))
|
||||
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 {
|
||||
@@ -55,21 +59,24 @@ function getSummary(booking: Partial<Booking>): string {
|
||||
}
|
||||
|
||||
function getDescription(booking: Booking): string {
|
||||
const bookingUrl = `${getBaseURL()}/admin/booking/${booking.uuid}`;
|
||||
const bookingUrl = `${getBaseURL()}/admin/booking/${booking.uuid}`
|
||||
|
||||
return `Managelink ${bookingUrl}`;
|
||||
return `Managelink ${bookingUrl}`
|
||||
}
|
||||
|
||||
export async function createCalendarEvent(booking: Booking): Promise<Booking> {
|
||||
const response = await calendar.events.insert({
|
||||
calendarId,
|
||||
requestBody: {
|
||||
summary: getSummary(booking),
|
||||
description: getDescription(booking),
|
||||
start: { date: booking.startDate },
|
||||
end: { date: booking.endDate },
|
||||
const response = await calendar.events.insert(
|
||||
{
|
||||
calendarId,
|
||||
requestBody: {
|
||||
summary: getSummary(booking),
|
||||
description: getDescription(booking),
|
||||
start: { date: booking.startDate },
|
||||
end: { date: booking.endDate },
|
||||
},
|
||||
},
|
||||
}, {})
|
||||
{}
|
||||
)
|
||||
|
||||
booking.calendarEventId = response.data.id
|
||||
|
||||
@@ -82,7 +89,7 @@ export async function deleteCalendarEvent(booking: Booking) {
|
||||
eventId: booking.calendarEventId,
|
||||
// TODO: really useful?
|
||||
sendNotifications: true,
|
||||
});
|
||||
})
|
||||
|
||||
booking.calendarEventId = null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user