make findBookedDays a static of schema

This commit is contained in:
Thomas Ruoff
2020-09-01 00:01:21 +02:00
committed by Thomas Ruoff
parent cf92a69fa7
commit 62a0e79664
2 changed files with 21 additions and 20 deletions

View File

@@ -20,22 +20,7 @@ function connect() {
export async function getBookedDays() {
await connect()
const bookings = await Booking.find(
{
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
$or: [
{ endDate: { $gt: new Date() } },
{ startDate: { $gt: new Date() } },
],
},
'startDate endDate'
).exec()
return bookings
.map((booking) => booking.days)
.flat()
.sort()
return Booking.findBookedDays()
}
export async function getBookingByUUID(uuid: string) {
@@ -62,10 +47,7 @@ export async function createBooking({
if (booking.days.some((day: string) => bookedDays.includes(day))) {
const error = new mongoose.Error.ValidationError(booking)
error.addError(
'endDate',
'At least on one day the van is not avaliable. Choose another startDate or endDate.'
)
error.addError('endDate', new mongoose.Error.ValidatorError())
throw error
}