mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add missing fields to model
This commit is contained in:
38
db/index.js
38
db/index.js
@@ -17,23 +17,47 @@ 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()
|
||||
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({ name, email, startDate, endDate }) {
|
||||
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 })
|
||||
booker = new Booker({ name, email, street, zip, city })
|
||||
await booker.save()
|
||||
}
|
||||
|
||||
const booking = new Booking({ startDate, endDate, booker: booker._id })
|
||||
booking.booker = booker._id
|
||||
await booking.save()
|
||||
await booking.populate('booker').execPopulate()
|
||||
return booking.toJSON()
|
||||
|
||||
11
db/schema.js
11
db/schema.js
@@ -5,7 +5,10 @@ import { getDays, dateFormat } from '../lib/dateHelper'
|
||||
export const BookerSchema = new Schema(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: 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 }
|
||||
)
|
||||
@@ -17,14 +20,18 @@ export const BookingSchema = new Schema(
|
||||
type: Date,
|
||||
required: true,
|
||||
get: dateFormat,
|
||||
min: new Date(),
|
||||
},
|
||||
endDate: { type: Date, required: false, get: dateFormat },
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user