mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
It also brings the problem of consolidating bookers over multiple bookings. The amount of data is not justifying having it in an own entity
80 lines
2.1 KiB
TypeScript
80 lines
2.1 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.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[]): string {
|
|
const events = bookings.map((booking): {
|
|
productId: string
|
|
calName: string
|
|
start: [number, number, number]
|
|
startOutputType: 'local' | 'utc'
|
|
duration: { days: number }
|
|
title: string
|
|
description: string
|
|
status: EventStatus
|
|
} => ({
|
|
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.name}`,
|
|
description: `Name: ${booking.name}
|
|
Zeitraum: ${daysFormatFrontend(booking.days)}
|
|
|
|
Email: ${booking.email}
|
|
Telefon: ${booking.phone}
|
|
|
|
Link: ${getBaseURL()}/admin/booking/${booking.uuid}
|
|
`,
|
|
status:
|
|
booking.status === BOOKING_STATUS.CONFIRMED
|
|
? ('CONFIRMED' as EventStatus)
|
|
: ('TENTATIVE' as EventStatus),
|
|
}))
|
|
|
|
const { error, value } = createEvents(events)
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
return value
|
|
}
|