mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import * as mongoose from 'mongoose'
|
|
import BookingModel, { IBooking } from './booking'
|
|
import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
|
import { BOOKING_STATUS } from './enums'
|
|
import { uniqueFilter } from '../helpers/array'
|
|
|
|
export const MONGO_URI = process.env.MONGO_URI
|
|
|
|
mongoose.set('strictQuery', false)
|
|
|
|
mongoose.connect(MONGO_URI, {
|
|
serverSelectionTimeoutMS: 3000,
|
|
})
|
|
|
|
export async function getBookedDays(uuidsToIngore?: string[]) {
|
|
const [bookedInDatabase, bookedInCalendar] = await Promise.all([
|
|
BookingModel.findBookedDays(uuidsToIngore),
|
|
calendarGetBookedDays(),
|
|
])
|
|
return [...bookedInDatabase, ...bookedInCalendar].filter(uniqueFilter).sort()
|
|
}
|
|
|
|
export async function getBookingByUUID(
|
|
uuid: string
|
|
): Promise<mongoose.HydratedDocument<IBooking>> {
|
|
return BookingModel.findOne({ uuid })
|
|
}
|
|
|
|
export async function getBookings({
|
|
status = [BOOKING_STATUS.CONFIRMED, BOOKING_STATUS.REQUESTED],
|
|
startDateGreaterThan = '2000-01-01T00:00:00Z',
|
|
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
|
|
IBooking[]
|
|
> {
|
|
return await BookingModel.find({
|
|
status: { $in: status },
|
|
startDate: { $gte: startDateGreaterThan },
|
|
})
|
|
.sort({ startDate: -1 })
|
|
.exec()
|
|
}
|
|
|
|
export async function createBooking({
|
|
startDate,
|
|
endDate,
|
|
purpose,
|
|
org,
|
|
destination,
|
|
name,
|
|
email,
|
|
phone,
|
|
street,
|
|
zip,
|
|
city,
|
|
}: IBooking): Promise<
|
|
mongoose.FlattenMaps<IBooking & { _id: mongoose.Types.ObjectId }>
|
|
> {
|
|
const booking = new BookingModel({
|
|
startDate,
|
|
endDate,
|
|
purpose,
|
|
org,
|
|
destination,
|
|
name,
|
|
email,
|
|
phone,
|
|
street,
|
|
zip,
|
|
city,
|
|
})
|
|
|
|
await booking.save()
|
|
return booking.toJSON()
|
|
}
|
|
|
|
export async function patchBooking(
|
|
bookingUUID: string,
|
|
bookingData: IBooking
|
|
): Promise<{ current: IBooking; previous: IBooking }> {
|
|
const booking = await getBookingByUUID(bookingUUID)
|
|
const oldBooking = booking.toJSON()
|
|
booking.set(bookingData)
|
|
await booking.save()
|
|
|
|
return { current: booking.toJSON(), previous: oldBooking }
|
|
}
|