mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add/delete entry in google calendar
This commit is contained in:
committed by
Thomas Ruoff
parent
673c2e2d00
commit
33c5a91e68
@@ -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[]> {
|
||||
|
||||
Reference in New Issue
Block a user