mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
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:
31
db/booker.ts
31
db/booker.ts
@@ -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)
|
||||
@@ -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',
|
||||
|
||||
29
db/index.ts
29
db/index.ts
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user