mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
update mongoose to 5.8.3 (lots of types changes)
This commit is contained in:
@@ -8,10 +8,10 @@ import {
|
||||
} from '../helpers/date'
|
||||
import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar'
|
||||
|
||||
import { Bill } from './bill'
|
||||
import { IBill } from './bill'
|
||||
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
|
||||
|
||||
export type Booking = {
|
||||
export interface IBooking {
|
||||
uuid: string
|
||||
name: string
|
||||
email: string
|
||||
@@ -19,7 +19,7 @@ export type Booking = {
|
||||
street: string
|
||||
zip: string
|
||||
city: string
|
||||
bill?: Bill
|
||||
bill?: IBill
|
||||
// format YYYY-MM-DD
|
||||
startDate: string
|
||||
// format YYYY-MM-DD
|
||||
@@ -30,17 +30,17 @@ export type Booking = {
|
||||
destination?: string
|
||||
days?: string[]
|
||||
calendarEventId: string
|
||||
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
toJSON?: () => IBooking
|
||||
}
|
||||
|
||||
export type BookingDocument = Booking &
|
||||
mongoose.Document &
|
||||
mongoose.SchemaTimestampsConfig
|
||||
|
||||
export type BookingModel = mongoose.Model<BookingDocument> & {
|
||||
export type BookingModel = mongoose.Model<IBooking> & {
|
||||
findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
|
||||
}
|
||||
|
||||
const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||
const BookingSchema = new mongoose.Schema(
|
||||
{
|
||||
// need a seperate uuid to be able to target a booking anonimously
|
||||
uuid: {
|
||||
@@ -78,7 +78,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||
required: true,
|
||||
validate: {
|
||||
validator: async function (days: string[]): Promise<boolean> {
|
||||
const booking = this as Booking
|
||||
const booking = this
|
||||
const uuid = booking.uuid && [booking.uuid]
|
||||
const bookedDays = await BookingModel.findBookedDays(uuid)
|
||||
|
||||
@@ -111,7 +111,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
|
||||
)
|
||||
|
||||
BookingSchema.pre('validate', function (next: () => void): void {
|
||||
const booking = this as BookingDocument
|
||||
const booking = this
|
||||
booking.days = getDays({
|
||||
startDate: new Date(booking.startDate),
|
||||
endDate: new Date(booking.endDate),
|
||||
@@ -120,16 +120,16 @@ BookingSchema.pre('validate', function (next: () => void): void {
|
||||
})
|
||||
|
||||
BookingSchema.pre('save', async function (next: () => void): Promise<void> {
|
||||
const booking = this as BookingDocument
|
||||
const booking = this
|
||||
|
||||
if (!booking.calendarEventId) {
|
||||
// create calendar event before saving to database
|
||||
await createCalendarEvent(booking)
|
||||
await createCalendarEvent(booking.toJSON())
|
||||
} else if (
|
||||
[BOOKING_STATUS.CANCELED, BOOKING_STATUS.REJECTED].includes(booking.status)
|
||||
) {
|
||||
// event has been canceled or rejected, delete calendar event again to free up the slot
|
||||
await deleteCalendarEvent(booking)
|
||||
await deleteCalendarEvent(booking.toJSON())
|
||||
}
|
||||
|
||||
next()
|
||||
@@ -138,31 +138,26 @@ BookingSchema.pre('save', async function (next: () => void): Promise<void> {
|
||||
BookingSchema.static(
|
||||
'findBookedDays',
|
||||
async function (uuidsToIngore: string[] = []): Promise<string[]> {
|
||||
const model = this as BookingModel
|
||||
const booking = this
|
||||
const now = nowInTz()
|
||||
const bookedDays = await model
|
||||
.find(
|
||||
{
|
||||
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
||||
uuid: { $nin: uuidsToIngore },
|
||||
// dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway
|
||||
endDate: { $gt: dateFormatBackend(now) },
|
||||
},
|
||||
'days'
|
||||
)
|
||||
.exec()
|
||||
const bookedDays = await booking.find(
|
||||
{
|
||||
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
||||
uuid: { $nin: uuidsToIngore },
|
||||
// dateFormatBackend uses YYYY-MM-DD, which is startOfDay anyway
|
||||
endDate: { $gt: dateFormatBackend(now) },
|
||||
},
|
||||
'days'
|
||||
)
|
||||
|
||||
return bookedDays
|
||||
.map((b) => b.days)
|
||||
.map((b: { days: any }) => b.days)
|
||||
.flat()
|
||||
.sort()
|
||||
}
|
||||
)
|
||||
|
||||
const BookingModel = (mongoose.models.Booking ||
|
||||
mongoose.model<BookingDocument, BookingModel>(
|
||||
'Booking',
|
||||
BookingSchema
|
||||
)) as BookingModel
|
||||
mongoose.model<IBooking>('Booking', BookingSchema)) as BookingModel
|
||||
|
||||
export default BookingModel
|
||||
|
||||
Reference in New Issue
Block a user