From c73651d008e03ccaf784c12200f85a1a9fcd6372 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Wed, 29 Jul 2020 23:32:11 +0200 Subject: [PATCH] use virtuals to get days --- db/index.js | 11 ++--------- db/schema.js | 6 +++++- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/db/index.js b/db/index.js index cddc493..78d277a 100644 --- a/db/index.js +++ b/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() } diff --git a/db/schema.js b/db/schema.js index c248582..ba265a7 100644 --- a/db/schema.js +++ b/db/schema.js @@ -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 () {