remover booker, that's overdosed

It also brings the problem of consolidating bookers over multiple
bookings. The amount of data is not justifying having it in an own
entity
This commit is contained in:
Thomas Ruoff
2021-06-21 23:21:23 +02:00
parent 3700b5f450
commit 9f3b6bb2e1
13 changed files with 63 additions and 86 deletions

View File

@@ -1,31 +0,0 @@
import * as mongoose from 'mongoose'
export type Booker = {
name: string
email: string
phone: string
street: string
zip: string
city: string
}
export type BookerDocument = Booker &
mongoose.SchemaTimestampsConfig &
mongoose.Document
export type BookerModel = mongoose.Model<BookerDocument>
const BookerSchema = new mongoose.Schema<BookerDocument>(
{
name: { type: String, required: true },
email: { type: String, required: true, unique: true, minlength: 5 },
phone: { type: String, required: false },
street: { type: String, required: true },
zip: { type: String, required: true },
city: { type: String, required: true },
},
{ timestamps: true, collation: { locale: 'de', strength: 1 } }
)
export default <BookerModel>mongoose.models.Booker ||
mongoose.model<BookerDocument, BookerModel>('Booker', BookerSchema)

View File

@@ -3,13 +3,17 @@ import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
import { Bill } from './bill'
import { Booker } from './booker'
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
import { getBookedDays } from './index'
export type Booking = {
uuid: string
booker?: Booker
name: string
email: string
phone: string
street: string
zip: string
city: string
bill?: Bill
startDate: string
endDate: string
@@ -36,11 +40,12 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
default: uuidv4,
index: true,
},
booker: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booker',
required: true,
},
name: { type: String, required: true },
email: { type: String, required: true, minlength: 5 },
phone: { type: String, required: false },
street: { type: String, required: true },
zip: { type: String, required: true },
city: { type: String, required: true },
bill: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Bill',

View File

@@ -1,5 +1,4 @@
import * as mongoose from 'mongoose'
import BookerModel, { Booker } from './booker'
import BookingModel, { Booking, BookingDocument } from './booking'
import BillModel, { Bill } from './bill'
import { BOOKING_STATUS } from './enums'
@@ -20,7 +19,9 @@ function connect(): Promise<typeof mongoose> {
return connectedPromise
}
export async function getBookedDays(uuidsToIngore?: string[]): Promise<string[]> {
export async function getBookedDays(
uuidsToIngore?: string[]
): Promise<string[]> {
await connect()
return BookingModel.findBookedDays(uuidsToIngore)
}
@@ -33,14 +34,14 @@ export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
export async function getBookings({
status = [BOOKING_STATUS.CONFIRMED, BOOKING_STATUS.REQUESTED],
startDateGreaterThan = '2000-01-01T00:00:00Z',
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<BookingDocument[]> {
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
BookingDocument[]
> {
await connect()
return await BookingModel.find({
status: { $in: status },
startDate: { $gte: startDateGreaterThan },
})
.populate('booker')
.exec()
}).exec()
}
export async function createBooking({
@@ -55,7 +56,7 @@ export async function createBooking({
street,
zip,
city,
}: Booking & Booker): Promise<Booking> {
}: Booking): Promise<Booking> {
await connect()
const booking = new BookingModel({
startDate,
@@ -63,17 +64,15 @@ export async function createBooking({
purpose,
org,
destination,
name,
email,
phone,
street,
zip,
city,
})
let booker = await BookerModel.findOne({ email }).exec()
if (!booker) {
booker = new BookerModel({ name, email, phone, street, zip, city })
await booker.save()
}
booking.booker = booker._id
await booking.save()
await booking.populate('booker').execPopulate()
return booking.toJSON()
}