make findBookedDays a static of schema

This commit is contained in:
Thomas Ruoff
2020-09-01 00:01:21 +02:00
committed by Thomas Ruoff
parent cf92a69fa7
commit 62a0e79664
2 changed files with 21 additions and 20 deletions

View File

@@ -64,6 +64,25 @@ BookingSchema.virtual('days').get(function () {
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> =
mongoose.models.Booking || mongoose.model('Booking', BookingSchema)