use virtuals to get days

This commit is contained in:
Thomas Ruoff
2020-07-29 23:32:11 +02:00
parent ad6543a3d3
commit c73651d008
2 changed files with 7 additions and 10 deletions

View File

@@ -23,14 +23,7 @@ export async function getBookedDays() {
$or: [{ endDate: { $gt: new Date() } }, { startDate: { $gt: new Date() } }], $or: [{ endDate: { $gt: new Date() } }, { startDate: { $gt: new Date() } }],
}).exec() }).exec()
return bookings return bookings.map((booking) => booking.days).flat()
.reduce((acc, booking) => {
acc.push(
getDays({ startDate: booking.startDate, endDate: booking.endDate })
)
return acc
}, [])
.flat()
} }
export async function createBooking({ name, email, startDate, endDate }) { 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 }) const booking = new Booking({ startDate, endDate, booker: booker._id })
await booking.save() await booking.save()
await booking.populate('booker').execPopulate() await booking.populate('booker').execPopulate()
return booking.toJSON({ getters: true, virtuals: true }) return booking.toJSON()
} }

View File

@@ -26,7 +26,11 @@ export const BookingSchema = new Schema(
default: 'requested', default: 'requested',
}, },
}, },
{ timestamps: true } {
timestamps: true,
toJSON: { virtuals: true, getters: true },
toObject: { virtuals: true, getters: true },
}
) )
BookingSchema.virtual('days').get(function () { BookingSchema.virtual('days').get(function () {