update mongoose to 5.8.3 (lots of types changes)

This commit is contained in:
Thomas Ruoff
2024-09-10 22:44:25 +02:00
parent 3ecbd16a2c
commit 32818b7492
22 changed files with 136 additions and 251 deletions

View File

@@ -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