improve server side validation error for double booking

This commit is contained in:
Thomas Ruoff
2020-09-15 23:37:07 +02:00
parent e2e94d86a2
commit e3a9da6efa

View File

@@ -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<typeof mongoose>
@@ -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
}