add/delete entry in google calendar

This commit is contained in:
Thomas Ruoff
2022-02-19 01:10:13 +01:00
committed by Thomas Ruoff
parent 673c2e2d00
commit 33c5a91e68
3 changed files with 98 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar'
import { Bill } from './bill'
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
@@ -21,7 +22,8 @@ export type Booking = {
purpose?: string
org?: string
destination?: string
days?: string[]
days?: string[],
calendarEventId: string,
}
export type BookingDocument = Booking &
@@ -103,6 +105,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
purpose: { type: String, required: false },
org: { type: String, required: false },
destination: { type: String, required: false },
calendarEventId: { type: String, required: false },
},
{
timestamps: true,
@@ -120,6 +123,20 @@ BookingSchema.pre('validate', function (next: () => void): void {
next()
})
BookingSchema.pre('save', async function (next: () => void): Promise<void> {
const booking = this as BookingDocument
if (!booking.calendarEventId) {
// create calendar event before saving to database
await createCalendarEvent(booking);
} else if ([BOOKING_STATUS.CANCELED, BOOKING_STATUS.REJECTED].includes(booking.status)) {
// event has been canceled or rejected, delete calendar event again to free up the slot
await deleteCalendarEvent(booking);
}
next();
});
BookingSchema.static('findBookedDays', async function (
uuidsToIngore: string[] = []
): Promise<string[]> {