mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
make findBookedDays a static of schema
This commit is contained in:
committed by
Thomas Ruoff
parent
cf92a69fa7
commit
62a0e79664
@@ -64,6 +64,25 @@ BookingSchema.virtual('days').get(function () {
|
|||||||
return getDays({ startDate: this.startDate, endDate: this.endDate })
|
return getDays({ startDate: this.startDate, endDate: this.endDate })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
BookingSchema.static('findBookedDays', async function (): string[] {
|
||||||
|
console.log('in findBookedDays this is', this)
|
||||||
|
const bookings = await this.find(
|
||||||
|
{
|
||||||
|
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
||||||
|
$or: [
|
||||||
|
{ endDate: { $gt: new Date() } },
|
||||||
|
{ startDate: { $gt: new Date() } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'startDate endDate'
|
||||||
|
).exec()
|
||||||
|
|
||||||
|
return bookings
|
||||||
|
.map((booking: Booking) => booking.days)
|
||||||
|
.flat()
|
||||||
|
.sort()
|
||||||
|
})
|
||||||
|
|
||||||
const Model: mongoose.Model<Booking> =
|
const Model: mongoose.Model<Booking> =
|
||||||
mongoose.models.Booking || mongoose.model('Booking', BookingSchema)
|
mongoose.models.Booking || mongoose.model('Booking', BookingSchema)
|
||||||
|
|
||||||
|
|||||||
22
db/index.ts
22
db/index.ts
@@ -20,22 +20,7 @@ function connect() {
|
|||||||
|
|
||||||
export async function getBookedDays() {
|
export async function getBookedDays() {
|
||||||
await connect()
|
await connect()
|
||||||
|
return Booking.findBookedDays()
|
||||||
const bookings = await Booking.find(
|
|
||||||
{
|
|
||||||
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
|
||||||
$or: [
|
|
||||||
{ endDate: { $gt: new Date() } },
|
|
||||||
{ startDate: { $gt: new Date() } },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
'startDate endDate'
|
|
||||||
).exec()
|
|
||||||
|
|
||||||
return bookings
|
|
||||||
.map((booking) => booking.days)
|
|
||||||
.flat()
|
|
||||||
.sort()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBookingByUUID(uuid: string) {
|
export async function getBookingByUUID(uuid: string) {
|
||||||
@@ -62,10 +47,7 @@ export async function createBooking({
|
|||||||
|
|
||||||
if (booking.days.some((day: string) => bookedDays.includes(day))) {
|
if (booking.days.some((day: string) => bookedDays.includes(day))) {
|
||||||
const error = new mongoose.Error.ValidationError(booking)
|
const error = new mongoose.Error.ValidationError(booking)
|
||||||
error.addError(
|
error.addError('endDate', new mongoose.Error.ValidatorError())
|
||||||
'endDate',
|
|
||||||
'At least on one day the van is not avaliable. Choose another startDate or endDate.'
|
|
||||||
)
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user