mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
fix days validation on updating booking
days validation needs to ignore own days
This commit is contained in:
@@ -26,7 +26,7 @@ export interface BookingDocument
|
||||
mongoose.SchemaTimestampsConfig {}
|
||||
|
||||
export interface BookingModel extends mongoose.Model<BookingDocument> {
|
||||
findBookedDays(): Promise<string[]>
|
||||
findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
|
||||
}
|
||||
|
||||
const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||
@@ -74,7 +74,9 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||
required: true,
|
||||
validate: {
|
||||
validator: async function (days: string[]) {
|
||||
const bookedDays = await getBookedDays()
|
||||
const booking = this as Booking
|
||||
const uuid = booking.uuid && [booking.uuid]
|
||||
const bookedDays = await getBookedDays(uuid)
|
||||
|
||||
const doubleBookedDays = days.filter((day: string) =>
|
||||
bookedDays.includes(day)
|
||||
@@ -112,13 +114,16 @@ BookingSchema.pre('validate', function (next: () => void) {
|
||||
next()
|
||||
})
|
||||
|
||||
BookingSchema.static('findBookedDays', async function (): Promise<string[]> {
|
||||
BookingSchema.static('findBookedDays', async function (
|
||||
uuidsToIngore: string[] = []
|
||||
): Promise<string[]> {
|
||||
const model = this as BookingModel
|
||||
const now = nowInTz()
|
||||
const bookedDays = await model
|
||||
.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) },
|
||||
},
|
||||
|
||||
@@ -20,9 +20,9 @@ function connect() {
|
||||
return connectedPromise
|
||||
}
|
||||
|
||||
export async function getBookedDays() {
|
||||
export async function getBookedDays(uuidsToIngore?: string[]) {
|
||||
await connect()
|
||||
return BookingModel.findBookedDays()
|
||||
return BookingModel.findBookedDays(uuidsToIngore)
|
||||
}
|
||||
|
||||
export async function getBookingByUUID(uuid: string) {
|
||||
|
||||
Reference in New Issue
Block a user