mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
It also brings the problem of consolidating bookers over multiple bookings. The amount of data is not justifying having it in an own entity
126 lines
2.5 KiB
TypeScript
126 lines
2.5 KiB
TypeScript
import * as mongoose from 'mongoose'
|
|
import BookingModel, { Booking, BookingDocument } from './booking'
|
|
import BillModel, { Bill } from './bill'
|
|
import { BOOKING_STATUS } from './enums'
|
|
|
|
let connectedPromise: Promise<typeof mongoose>
|
|
|
|
function connect(): Promise<typeof mongoose> {
|
|
if (connectedPromise) {
|
|
return
|
|
}
|
|
|
|
connectedPromise = mongoose.connect(process.env.MONGO_URI, {
|
|
useCreateIndex: true,
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
})
|
|
|
|
return connectedPromise
|
|
}
|
|
|
|
export async function getBookedDays(
|
|
uuidsToIngore?: string[]
|
|
): Promise<string[]> {
|
|
await connect()
|
|
return BookingModel.findBookedDays(uuidsToIngore)
|
|
}
|
|
|
|
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 },
|
|
}).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 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
|
|
}
|