import * as mongoose from 'mongoose' import { dateFormatBackend, getDays, nowInTz, dateParseBackend, } from '../helpers/date' import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar' import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums' export interface IBooking { uuid: string name: string email: string phone?: string street: string zip: string city: string // format YYYY-MM-DD startDate: string // format YYYY-MM-DD endDate: string status?: BOOKING_STATUS purpose?: string org?: string destination?: string days?: string[] calendarEventId?: string createdAt?: NativeDate updatedAt?: NativeDate toJSON?: () => IBooking } export type BookingModel = mongoose.Model & { findBookedDays(uuidsToIngore?: string[]): Promise } const BookingSchema = new mongoose.Schema( { // need a seperate uuid to be able to target a booking anonimously uuid: { type: String, default: () => crypto.randomUUID(), index: true, }, name: { type: String, required: true }, email: { type: String, required: true, minlength: 5 }, phone: { type: String, required: false }, street: { type: String, required: true }, zip: { type: String, required: true }, city: { type: String, required: true }, startDate: { type: String, required: true, validator: function (value: string): boolean { return !!dateParseBackend(value) }, }, endDate: { type: String, required: true, validator: function (value: string): boolean { return !!dateParseBackend(value) }, }, days: { type: [String], required: true, validate: { validator: async function (days: string[]): Promise { const booking = this const uuid = booking.uuid && [booking.uuid] const bookedDays = await BookingModel.findBookedDays(uuid) const doubleBookedDays = days.filter((day: string): boolean => bookedDays.includes(day) ) return doubleBookedDays.length === 0 }, message: (props: { value: string[] }): string => `At least one day is of ${props.value.join(',')} is already booked`, type: VALIDATION_ERRORS.AT_LEAST_ONE_DAY_BOOKED, }, }, status: { type: String, enum: Object.values(BOOKING_STATUS), required: true, default: BOOKING_STATUS.REQUESTED, }, purpose: { type: String, required: false }, org: { type: String, required: false }, destination: { type: String, required: false }, calendarEventId: { type: String, required: false }, }, { timestamps: true, toJSON: { virtuals: true, getters: true }, toObject: { virtuals: true, getters: true }, } ) BookingSchema.pre('validate', function (next: () => void): void { const booking = this booking.days = getDays({ startDate: new Date(booking.startDate), endDate: new Date(booking.endDate), }) next() }) BookingSchema.pre('save', async function (next: () => void): Promise { const booking = this if (!booking.calendarEventId) { // create calendar event before saving to database await createCalendarEvent(booking.toJSON()) } 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.toJSON()) } next() }) BookingSchema.static( 'findBookedDays', async function (uuidsToIngore: string[] = []): Promise { const booking = this const now = nowInTz() const bookedDays = await booking.find( { status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] }, uuid: { $nin: uuidsToIngore }, // dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway endDate: { $gt: dateFormatBackend(now) }, }, 'days' ) return bookedDays .map((b: { days: any }) => b.days) .flat() .sort() } ) const BookingModel = (mongoose.models.Booking || mongoose.model('Booking', BookingSchema)) as BookingModel export default BookingModel