mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
143 lines
3.1 KiB
TypeScript
143 lines
3.1 KiB
TypeScript
import * as mongoose from 'mongoose'
|
|
import BookingModel, { Booking, BookingDocument } from './booking'
|
|
import BillModel, { Bill } from './bill'
|
|
import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
|
import { BOOKING_STATUS } from './enums'
|
|
import { uniqueFilter } from '../helpers/array'
|
|
|
|
let connectedPromise: Promise<mongoose.Mongoose>
|
|
|
|
export const MONGO_URI = process.env.MONGO_URI
|
|
|
|
export function connect(): Promise<mongoose.Mongoose> {
|
|
if (connectedPromise) {
|
|
return connectedPromise
|
|
}
|
|
|
|
connectedPromise = mongoose.connect(process.env.MONGO_URI)
|
|
|
|
return connectedPromise
|
|
}
|
|
|
|
export async function getBookedDays(
|
|
uuidsToIngore?: string[]
|
|
): Promise<string[]> {
|
|
await connect()
|
|
const bookedInDatabase = await BookingModel.findBookedDays(uuidsToIngore)
|
|
const bookedInCalendar = await calendarGetBookedDays()
|
|
|
|
return [...bookedInDatabase, ...bookedInCalendar].filter(uniqueFilter).sort()
|
|
}
|
|
|
|
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
|
|
await connect()
|
|
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<
|
|
BookingDocument[]
|
|
> {
|
|
await connect()
|
|
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,
|
|
}: Booking): Promise<Booking> {
|
|
await connect()
|
|
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: Booking
|
|
): Promise<Booking> {
|
|
await connect()
|
|
const booking = await getBookingByUUID(bookingUUID)
|
|
booking.set(bookingData)
|
|
await booking.save()
|
|
|
|
return booking.toJSON()
|
|
}
|
|
|
|
export async function createBill(
|
|
bookingUUID: string,
|
|
billData: Bill
|
|
): Promise<Bill> {
|
|
await connect()
|
|
const booking = await getBookingByUUID(bookingUUID)
|
|
|
|
const bill = new BillModel()
|
|
bill.set(billData)
|
|
|
|
await bill.save()
|
|
|
|
booking.bill = bill._id
|
|
await booking.save()
|
|
|
|
return bill.toJSON()
|
|
}
|
|
|
|
export async function patchBill(
|
|
bookingUUID: string,
|
|
billData: Bill
|
|
): Promise<Bill> {
|
|
await connect()
|
|
const booking = await getBookingByUUID(bookingUUID)
|
|
const bill =
|
|
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
|
(await BillModel.create({}))
|
|
|
|
bill.set(billData)
|
|
await bill.save()
|
|
|
|
if (booking.bill !== bill._id) {
|
|
booking.bill = bill._id
|
|
await booking.save()
|
|
}
|
|
|
|
return bill.toJSON()
|
|
}
|
|
|
|
export async function getMilageMax(): Promise<number> {
|
|
const billMaxMilageEnd = await BillModel.findOne({})
|
|
.sort('-milageEnd')
|
|
.select('milageEnd')
|
|
.exec()
|
|
|
|
return billMaxMilageEnd?.milageEnd || 0
|
|
}
|