mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { createEvents, createEvent, EventStatus } from 'ics'
|
|
import { Booking } from '../db/booking'
|
|
import { BOOKING_STATUS } from '../db/enums'
|
|
import { getBaseURL } from './url'
|
|
import { daysFormatFrontend } from './date'
|
|
|
|
function convertDay(value: string): [number, number, number] {
|
|
const parts = value.split('-')
|
|
if (parts.length !== 3) {
|
|
throw new Error('faile converting day')
|
|
}
|
|
return [Number(parts[0]), Number(parts[1]), Number(parts[2])]
|
|
}
|
|
|
|
export function generateCalendarEntry(booking: Booking): string {
|
|
const { error, value } = createEvent({
|
|
productId: 'app.vercel.pfadi-bussle/ics',
|
|
title: `Pfadi-Bussle Buchung`,
|
|
start: convertDay(booking.days[0]),
|
|
startOutputType: 'local',
|
|
duration: { days: booking.days.length },
|
|
location: 'Mömpelgardgasse 25, 72348 Rosenfeld, Deutschland',
|
|
geo: { lat: 48.287044, lon: 8.726361 },
|
|
description: `Gebucht auf ${booking.booker.name}
|
|
|
|
Buchungs-Link: ${getBaseURL()}/booking/${booking.uuid}
|
|
`,
|
|
status:
|
|
booking.status === BOOKING_STATUS.CONFIRMED
|
|
? ('CONFIRMED' as EventStatus)
|
|
: ('TENTATIVE' as EventStatus),
|
|
})
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
export function generateBookedCalendar(bookings: Booking[]) {
|
|
const events = bookings.map((booking) => ({
|
|
productId: 'app.vercel.pfadi-bussle/ics',
|
|
calName: 'Pfadi-Bussle Buchungen',
|
|
start: convertDay(booking.days[0]),
|
|
startOutputType: 'local',
|
|
duration: { days: booking.days.length },
|
|
title: `Buchung ${booking.booker.name}`,
|
|
description: `Name: ${booking.booker.name}
|
|
Zeitraum: ${daysFormatFrontend(booking.days)}
|
|
|
|
Email: ${booking.booker.email}
|
|
Telefon: ${booking.booker.phone}
|
|
|
|
Link: ${getBaseURL()}/admin/booking/${booking.uuid}
|
|
`,
|
|
status:
|
|
booking.status === BOOKING_STATUS.CONFIRMED
|
|
? ('CONFIRMED' as EventStatus)
|
|
: ('TENTATIVE' as EventStatus),
|
|
}))
|
|
|
|
// FIXME: startOutputType does not like 'local'
|
|
// @ts-ignore
|
|
const { error, value } = createEvents(events)
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
return value
|
|
}
|