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:
17
db/bill.ts
17
db/bill.ts
@@ -2,27 +2,30 @@ import * as mongoose from 'mongoose'
|
||||
import { BILL_STATUS, MILAGE_TARIFS } from './enums'
|
||||
import { getBillTotal } from '../helpers/bill'
|
||||
|
||||
export type AdditionalCost = {
|
||||
export interface IAdditionalCost {
|
||||
name: string
|
||||
value: number
|
||||
}
|
||||
|
||||
export type Bill = {
|
||||
export interface IBill {
|
||||
milageStart: number
|
||||
milageEnd: number
|
||||
milage?: number
|
||||
tarif: MILAGE_TARIFS
|
||||
status: BILL_STATUS
|
||||
additionalCosts: AdditionalCost[]
|
||||
additionalCosts: IAdditionalCost[]
|
||||
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
export type BillDocument = Bill &
|
||||
export type BillDocument = IBill &
|
||||
mongoose.SchemaTimestampsConfig &
|
||||
mongoose.Document
|
||||
|
||||
export type BillModel = mongoose.Model<BillDocument>
|
||||
export type BillModel = mongoose.Model<IBill>
|
||||
|
||||
const BillSchema = new mongoose.Schema<BillDocument>(
|
||||
const BillSchema = new mongoose.Schema<IBill>(
|
||||
{
|
||||
milageStart: {
|
||||
type: Number,
|
||||
@@ -88,4 +91,4 @@ BillSchema.virtual('total').get(function (): number {
|
||||
})
|
||||
|
||||
export default (mongoose.models.Bill ||
|
||||
mongoose.model<BillDocument, BillModel>('Bill', BillSchema)) as BillModel
|
||||
mongoose.model<BillDocument, BillModel>('Bill', BillSchema)) as BillModel
|
||||
@@ -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
|
||||
|
||||
40
db/index.ts
40
db/index.ts
@@ -1,6 +1,6 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
import BookingModel, { Booking, BookingDocument } from './booking'
|
||||
import BillModel, { Bill } from './bill'
|
||||
import BookingModel, { IBooking } from './booking'
|
||||
import BillModel, { IBill } from './bill'
|
||||
import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
||||
import { BOOKING_STATUS } from './enums'
|
||||
import { uniqueFilter } from '../helpers/array'
|
||||
@@ -9,7 +9,7 @@ export const MONGO_URI = process.env.MONGO_URI
|
||||
|
||||
mongoose.set('strictQuery', false);
|
||||
|
||||
mongoose.connect(process.env.MONGO_URI, {
|
||||
mongoose.connect(MONGO_URI, {
|
||||
serverSelectionTimeoutMS: 3000,
|
||||
})
|
||||
|
||||
@@ -23,7 +23,7 @@ export async function getBookedDays(
|
||||
return [...bookedInDatabase, ...bookedInCalendar].filter(uniqueFilter).sort()
|
||||
}
|
||||
|
||||
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
|
||||
export async function getBookingByUUID(uuid: string): Promise<mongoose.HydratedDocument<IBooking>> {
|
||||
return BookingModel.findOne({ uuid })
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export async function getBookings({
|
||||
status = [BOOKING_STATUS.CONFIRMED, BOOKING_STATUS.REQUESTED],
|
||||
startDateGreaterThan = '2000-01-01T00:00:00Z',
|
||||
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
|
||||
BookingDocument[]
|
||||
IBooking[]
|
||||
> {
|
||||
return await BookingModel.find({
|
||||
status: { $in: status },
|
||||
@@ -53,7 +53,7 @@ export async function createBooking({
|
||||
street,
|
||||
zip,
|
||||
city,
|
||||
}: Booking): Promise<Booking> {
|
||||
}: IBooking): Promise<mongoose.FlattenMaps<IBooking & { _id: mongoose.Types.ObjectId }>> {
|
||||
const booking = new BookingModel({
|
||||
startDate,
|
||||
endDate,
|
||||
@@ -69,25 +69,25 @@ export async function createBooking({
|
||||
})
|
||||
|
||||
await booking.save()
|
||||
return booking.toJSON<Booking>()
|
||||
return booking.toJSON()
|
||||
}
|
||||
|
||||
export async function patchBooking(
|
||||
bookingUUID: string,
|
||||
bookingData: Booking
|
||||
): Promise<{ current: Booking; previous: Booking }> {
|
||||
bookingData: IBooking
|
||||
): Promise<{ current: IBooking; previous: IBooking }> {
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const oldBooking = booking.toJSON<Booking>()
|
||||
const oldBooking = booking.toJSON()
|
||||
booking.set(bookingData)
|
||||
await booking.save()
|
||||
|
||||
return { current: booking.toJSON<Booking>(), previous: oldBooking }
|
||||
return { current: booking.toJSON(), previous: oldBooking }
|
||||
}
|
||||
|
||||
export async function createBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
billData: IBill
|
||||
): Promise<IBill> {
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
|
||||
const bill = new BillModel()
|
||||
@@ -95,16 +95,16 @@ export async function createBill(
|
||||
|
||||
await bill.save()
|
||||
|
||||
booking.bill = bill._id
|
||||
booking.bill = bill
|
||||
await booking.save()
|
||||
|
||||
return bill.toJSON<Bill>()
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function patchBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
billData: IBill
|
||||
): Promise<IBill> {
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const bill =
|
||||
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
||||
@@ -113,12 +113,12 @@ export async function patchBill(
|
||||
bill.set(billData)
|
||||
await bill.save()
|
||||
|
||||
if (booking.bill !== bill._id) {
|
||||
booking.bill = bill._id
|
||||
if (booking.bill !== bill) {
|
||||
booking.bill = bill
|
||||
await booking.save()
|
||||
}
|
||||
|
||||
return bill.toJSON<Bill>()
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function getMilageMax(): Promise<number> {
|
||||
|
||||
Reference in New Issue
Block a user