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 {}
|
mongoose.SchemaTimestampsConfig {}
|
||||||
|
|
||||||
export interface BookingModel extends mongoose.Model<BookingDocument> {
|
export interface BookingModel extends mongoose.Model<BookingDocument> {
|
||||||
findBookedDays(): Promise<string[]>
|
findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
const BookingSchema = new mongoose.Schema<BookingDocument>(
|
const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||||
@@ -74,7 +74,9 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
|
|||||||
required: true,
|
required: true,
|
||||||
validate: {
|
validate: {
|
||||||
validator: async function (days: string[]) {
|
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) =>
|
const doubleBookedDays = days.filter((day: string) =>
|
||||||
bookedDays.includes(day)
|
bookedDays.includes(day)
|
||||||
@@ -112,13 +114,16 @@ BookingSchema.pre('validate', function (next: () => void) {
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
BookingSchema.static('findBookedDays', async function (): Promise<string[]> {
|
BookingSchema.static('findBookedDays', async function (
|
||||||
|
uuidsToIngore: string[] = []
|
||||||
|
): Promise<string[]> {
|
||||||
const model = this as BookingModel
|
const model = this as BookingModel
|
||||||
const now = nowInTz()
|
const now = nowInTz()
|
||||||
const bookedDays = await model
|
const bookedDays = await model
|
||||||
.find(
|
.find(
|
||||||
{
|
{
|
||||||
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
||||||
|
uuid: { $nin: uuidsToIngore },
|
||||||
// dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway
|
// dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway
|
||||||
endDate: { $gt: dateFormatBackend(now) },
|
endDate: { $gt: dateFormatBackend(now) },
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ function connect() {
|
|||||||
return connectedPromise
|
return connectedPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBookedDays() {
|
export async function getBookedDays(uuidsToIngore?: string[]) {
|
||||||
await connect()
|
await connect()
|
||||||
return BookingModel.findBookedDays()
|
return BookingModel.findBookedDays(uuidsToIngore)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBookingByUUID(uuid: string) {
|
export async function getBookingByUUID(uuid: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user