mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
make form just simple
This commit is contained in:
19
db/index.js
19
db/index.js
@@ -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 })
|
||||
}
|
||||
|
||||
42
db/schema.js
42
db/schema.js
@@ -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 })
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user