make form just simple

This commit is contained in:
Thomas Ruoff
2020-07-29 00:01:54 +02:00
parent a26e58b43e
commit 526c625a57
7 changed files with 116 additions and 119 deletions

View File

@@ -34,16 +34,15 @@ export async function getBookedDays() {
}
export async function createBooking({ name, email, startDate, endDate }) {
const booker = new Booker({ name, email })
await booker.save()
const ignoreCaseEmailMatcher = new RegExp(email, 'i')
let booker = await Booker.findOne({ email: ignoreCaseEmailMatcher }).exec()
if (!booker) {
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,
}
await booking.populate('booker').execPopulate()
return booking.toJSON({ getters: true, virtuals: true })
}

View File

@@ -1,18 +1,34 @@
import { Schema } from 'mongoose'
export const BookerSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true },
})
import { getDays, dateFormat } from '../lib/dateHelper'
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',
export const BookerSchema = new Schema(
{
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
},
{ timestamps: true }
)
export const BookingSchema = new Schema(
{
booker: { type: Schema.Types.ObjectId, ref: 'Booker', required: true },
startDate: {
type: Date,
required: true,
get: dateFormat,
},
endDate: { type: Date, required: false, get: dateFormat },
status: {
type: String,
enum: ['requested', 'confirmed', 'rejected'],
required: true,
default: 'requested',
},
},
{ timestamps: true }
)
BookingSchema.virtual('days').get(function () {
return getDays({ startDate: this.startDate, endDate: this.endDate })
})