mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +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 * as mongoose from 'mongoose'
|
||||||
import Booker from './booker'
|
import Booker from './booker'
|
||||||
import Booking from './booking'
|
import Booking from './booking'
|
||||||
|
import { dateFormatFrontend } from '../helpers/date'
|
||||||
|
|
||||||
let connectedPromise: Promise<typeof mongoose>
|
let connectedPromise: Promise<typeof mongoose>
|
||||||
|
|
||||||
@@ -44,11 +45,18 @@ export async function createBooking({
|
|||||||
const booking = new Booking({ startDate, endDate, purpose, org, destination })
|
const booking = new Booking({ startDate, endDate, purpose, org, destination })
|
||||||
const bookedDays = await getBookedDays()
|
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)
|
const error = new mongoose.Error.ValidationError(booking)
|
||||||
error.addError(
|
error.addError(
|
||||||
'endDate',
|
'days',
|
||||||
new mongoose.Error.ValidatorError({ message: 'Schon gebucht' })
|
new mongoose.Error.ValidatorError({
|
||||||
|
message: `${doubleBookedDays
|
||||||
|
.map(dateFormatFrontend)
|
||||||
|
.join(', ')} schon gebucht`,
|
||||||
|
})
|
||||||
)
|
)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user