mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
import mongoose from 'mongoose'
|
|
import { BookingSchema, BookerSchema } from './schema'
|
|
import { getDays } from '../lib/dateHelper'
|
|
|
|
mongoose.connect(process.env.MONGO_URI, {
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
connectTimeoutMS: 1000,
|
|
serverSelectionTimeoutMS: 5000,
|
|
})
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
mongoose.modelNames().includes('Booker') && mongoose.deleteModel('Booker')
|
|
mongoose.modelNames().includes('Booking') && mongoose.deleteModel('Booking')
|
|
}
|
|
|
|
export const Booker = mongoose.model('Booker', BookerSchema)
|
|
export const Booking = mongoose.model('Booking', BookingSchema)
|
|
|
|
export async function getBookedDays() {
|
|
const bookings = await Booking.find({
|
|
status: { $ne: 'rejected' },
|
|
$or: [{ endDate: { $gt: new Date() } }, { startDate: { $gt: new Date() } }],
|
|
}).exec()
|
|
|
|
return bookings
|
|
.reduce((acc, booking) => {
|
|
acc.push(
|
|
getDays({ startDate: booking.startDate, endDate: booking.endDate })
|
|
)
|
|
return acc
|
|
}, [])
|
|
.flat()
|
|
}
|
|
|
|
export async function createBooking({ name, email, startDate, endDate }) {
|
|
const booker = new Booker({ name, email })
|
|
await booker.save()
|
|
const booking = new Booking({ startDate, endDate, booker: booker._id })
|
|
await booking.save()
|
|
return {
|
|
booker: booking.booker._id,
|
|
startDate: booking.startDate,
|
|
endDate: booking.endDate,
|
|
bookedDate: booking.bookedDate,
|
|
confirmed: booking.confirmed,
|
|
confirmedDate: booking.confimredDate,
|
|
}
|
|
}
|