mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
move db into api
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { createBooking } from '../../db/index'
|
||||
import { createBooking } from './db/index'
|
||||
import { Error } from 'mongoose'
|
||||
|
||||
export default async function userHandler(req, res) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
|
||||
import { getBookedDays } from '../../db/index'
|
||||
import { getBookedDays } from './db/index'
|
||||
|
||||
export default async function useHandler(req, res) {
|
||||
const { method } = req
|
||||
|
||||
66
pages/api/db/index.js
Normal file
66
pages/api/db/index.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import mongoose from 'mongoose'
|
||||
import { BookingSchema, BookerSchema } from './schema'
|
||||
|
||||
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.models.Booker || mongoose.model('Booker', BookerSchema)
|
||||
export const Booking =
|
||||
mongoose.models.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() } },
|
||||
],
|
||||
},
|
||||
'startDate endDate'
|
||||
).exec()
|
||||
|
||||
return bookings.map((booking) => booking.days).flat()
|
||||
}
|
||||
|
||||
export async function createBooking({
|
||||
startDate,
|
||||
endDate,
|
||||
purpose,
|
||||
org,
|
||||
destination,
|
||||
name,
|
||||
email,
|
||||
street,
|
||||
zip,
|
||||
city,
|
||||
}) {
|
||||
const booking = new Booking({ startDate, endDate, purpose, org, destination })
|
||||
const bookedDays = await getBookedDays()
|
||||
|
||||
if (booking.days.some((day) => bookedDays.includes(day))) {
|
||||
throw new mongoose.Error.ValidationError(booking)
|
||||
}
|
||||
|
||||
const ignoreCaseEmailMatcher = new RegExp(email, 'i')
|
||||
let booker = await Booker.findOne({ email: ignoreCaseEmailMatcher }).exec()
|
||||
if (!booker) {
|
||||
booker = new Booker({ name, email, street, zip, city })
|
||||
await booker.save()
|
||||
}
|
||||
|
||||
booking.booker = booker._id
|
||||
await booking.save()
|
||||
await booking.populate('booker').execPopulate()
|
||||
return booking.toJSON()
|
||||
}
|
||||
45
pages/api/db/schema.js
Normal file
45
pages/api/db/schema.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Schema } from 'mongoose'
|
||||
|
||||
import { getDays, dateFormat } from '../../helpers/date'
|
||||
|
||||
export const BookerSchema = new Schema(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true, minlength: 5 },
|
||||
street: { type: String, required: true },
|
||||
zip: { type: String, required: true },
|
||||
city: { type: String, required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
|
||||
export const BookingSchema = new Schema(
|
||||
{
|
||||
booker: { type: Schema.Types.ObjectId, ref: 'Booker', required: true },
|
||||
startDate: {
|
||||
type: Date,
|
||||
required: true,
|
||||
get: dateFormat,
|
||||
min: new Date(),
|
||||
},
|
||||
endDate: { type: Date, required: false, get: dateFormat, min: new Date() },
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['requested', 'confirmed', 'rejected'],
|
||||
required: true,
|
||||
default: 'requested',
|
||||
},
|
||||
purpose: { type: String, required: false },
|
||||
org: { type: String, required: false },
|
||||
destination: { type: String, required: false },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true, getters: true },
|
||||
toObject: { virtuals: true, getters: true },
|
||||
}
|
||||
)
|
||||
|
||||
BookingSchema.virtual('days').get(function () {
|
||||
return getDays({ startDate: this.startDate, endDate: this.endDate })
|
||||
})
|
||||
Reference in New Issue
Block a user