mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
use virtuals to get days
This commit is contained in:
11
db/index.js
11
db/index.js
@@ -23,14 +23,7 @@ export async function getBookedDays() {
|
||||
$or: [{ endDate: { $gt: new Date() } }, { startDate: { $gt: new Date() } }],
|
||||
}).exec()
|
||||
|
||||
return bookings
|
||||
.reduce((acc, booking) => {
|
||||
acc.push(
|
||||
getDays({ startDate: booking.startDate, endDate: booking.endDate })
|
||||
)
|
||||
return acc
|
||||
}, [])
|
||||
.flat()
|
||||
return bookings.map((booking) => booking.days).flat()
|
||||
}
|
||||
|
||||
export async function createBooking({ name, email, startDate, endDate }) {
|
||||
@@ -44,5 +37,5 @@ export async function createBooking({ name, email, startDate, endDate }) {
|
||||
const booking = new Booking({ startDate, endDate, booker: booker._id })
|
||||
await booking.save()
|
||||
await booking.populate('booker').execPopulate()
|
||||
return booking.toJSON({ getters: true, virtuals: true })
|
||||
return booking.toJSON()
|
||||
}
|
||||
|
||||
@@ -26,7 +26,11 @@ export const BookingSchema = new Schema(
|
||||
default: 'requested',
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
{
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true, getters: true },
|
||||
toObject: { virtuals: true, getters: true },
|
||||
}
|
||||
)
|
||||
|
||||
BookingSchema.virtual('days').get(function () {
|
||||
|
||||
Reference in New Issue
Block a user