mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
more hacking
This commit is contained in:
49
db/index.js
Normal file
49
db/index.js
Normal file
@@ -0,0 +1,49 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
18
db/schema.js
Normal file
18
db/schema.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Schema } from 'mongoose'
|
||||
|
||||
export const BookerSchema = new Schema({
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true },
|
||||
})
|
||||
|
||||
export const BookingSchema = new Schema({
|
||||
booker: { type: Schema.Types.ObjectId, ref: 'Booker', required: true },
|
||||
startDate: { type: Date, required: true },
|
||||
endDate: { type: Date, required: false },
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['requested', 'confirmed', 'rejected'],
|
||||
required: true,
|
||||
default: 'requested',
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user