From e3a9da6efa84ef5a206b88f9ea3f9369db62403b Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Tue, 15 Sep 2020 23:37:07 +0200 Subject: [PATCH] improve server side validation error for double booking --- db/index.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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 }