diff --git a/db/index.ts b/db/index.ts index f77e873..c3dc958 100644 --- a/db/index.ts +++ b/db/index.ts @@ -1,6 +1,7 @@ import * as mongoose from 'mongoose' import Booker from './booker' import Booking from './booking' +import { dateFormatFrontend } from '../helpers/date' let connectedPromise: Promise @@ -44,11 +45,18 @@ export async function createBooking({ const booking = new Booking({ startDate, endDate, purpose, org, destination }) const bookedDays = await getBookedDays() - if (booking.days.some((day: string) => bookedDays.includes(day))) { + const doubleBookedDays = booking.days.filter((day: string) => + bookedDays.includes(day) + ) + if (doubleBookedDays.length) { const error = new mongoose.Error.ValidationError(booking) error.addError( - 'endDate', - new mongoose.Error.ValidatorError({ message: 'Schon gebucht' }) + 'days', + new mongoose.Error.ValidatorError({ + message: `${doubleBookedDays + .map(dateFormatFrontend) + .join(', ')} schon gebucht`, + }) ) throw error }