mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
improve server side validation error for double booking
This commit is contained in:
14
db/index.ts
14
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<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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user