another try to fix types for models

This commit is contained in:
Thomas Ruoff
2020-09-03 23:27:30 +02:00
committed by Thomas Ruoff
parent 82270e944c
commit 038e2b2420
5 changed files with 29 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
import * as mongoose from 'mongoose' import * as mongoose from 'mongoose'
export interface Booker export interface BookerDocument
extends mongoose.SchemaTimestampsConfig, extends mongoose.SchemaTimestampsConfig,
mongoose.Document { mongoose.Document {
name: string name: string
@@ -10,7 +10,9 @@ export interface Booker
city: string city: string
} }
const BookerSchema = new mongoose.Schema<Booker>( export interface BookerModel extends mongoose.Model<BookerDocument> {}
const BookerSchema = new mongoose.Schema<BookerDocument>(
{ {
name: { type: String, required: true }, name: { type: String, required: true },
email: { type: String, required: true, unique: true, minlength: 5 }, email: { type: String, required: true, unique: true, minlength: 5 },
@@ -21,6 +23,8 @@ const BookerSchema = new mongoose.Schema<Booker>(
{ timestamps: true, collation: { locale: 'de', strength: 1 } } { timestamps: true, collation: { locale: 'de', strength: 1 } }
) )
const Model: mongoose.Model<Booker> = const Model = mongoose.model<BookerDocument, BookerModel>(
mongoose.models.Booker || mongoose.model('Booker', BookerSchema) 'Booker',
BookerSchema
)
export default Model export default Model

View File

@@ -1,14 +1,14 @@
import * as mongoose from 'mongoose' import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays } from '../helpers/date' import { dateFormatBackend, getDays } from '../helpers/date'
import { Booker } from './booker' import { BookerDocument } from './booker'
import { BOOKING_STATUS } from './bookingStatus' import { BOOKING_STATUS } from './bookingStatus'
export interface Booking export interface BookingDocument
extends mongoose.Document, extends mongoose.Document,
mongoose.SchemaTimestampsConfig { mongoose.SchemaTimestampsConfig {
uuid: string uuid: string
booker: Booker booker: BookerDocument
startDate: Date startDate: Date
endDate: Date endDate: Date
status: BOOKING_STATUS status: BOOKING_STATUS
@@ -18,7 +18,11 @@ export interface Booking
days?: string[] days?: string[]
} }
const BookingSchema = new mongoose.Schema<Booking>( export interface BookingModel extends mongoose.Model<BookingDocument> {
findBookedDays(): Promise<string[]>
}
const BookingSchema = new mongoose.Schema<BookingDocument>(
{ {
// need a seperate uuid to be able to target a booking anonimously // need a seperate uuid to be able to target a booking anonimously
uuid: { uuid: {
@@ -78,12 +82,14 @@ BookingSchema.static('findBookedDays', async function (): Promise<string[]> {
).exec() ).exec()
return bookings return bookings
.map((booking: Booking) => booking.days) .map((booking: BookingDocument) => booking.days)
.flat() .flat()
.sort() .sort()
}) })
const Model: mongoose.Model<Booking> = const Model: BookingModel = mongoose.model<BookingDocument, BookingModel>(
mongoose.models.Booking || mongoose.model('Booking', BookingSchema) 'Booking',
BookingSchema
)
export default Model export default Model

View File

@@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { Booking } from '../../../db/booking' import { BookingDocument } from '../../../db/booking'
import { BOOKING_STATUS } from '../../../db/bookingStatus' import { BOOKING_STATUS } from '../../../db/bookingStatus'
import { getBookingByUUID } from '../../../db/index' import { getBookingByUUID } from '../../../db/index'
@@ -14,7 +14,7 @@ export default async function userHandler(
const uuid = Array.isArray(uuids) ? uuids[0] : uuids const uuid = Array.isArray(uuids) ? uuids[0] : uuids
let booking: Booking let booking: BookingDocument
switch (method) { switch (method) {
case 'PATCH': case 'PATCH':

View File

@@ -1,6 +1,6 @@
import { Error } from 'mongoose' import { Error } from 'mongoose'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { Booking } from '../../../db/booking' import { BookingDocument } from '../../../db/booking'
import { createBooking } from '../../../db/index' import { createBooking } from '../../../db/index'
import { sendReceivedBookingMail } from '../../../helpers/mail' import { sendReceivedBookingMail } from '../../../helpers/mail'
@@ -10,7 +10,7 @@ export default async function userHandler(
) { ) {
const { method } = req const { method } = req
let booking: Booking let booking: BookingDocument
switch (method) { switch (method) {
case 'POST': case 'POST':

View File

@@ -2,7 +2,7 @@ import { GetServerSideProps } from 'next'
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import Footer from '../../components/footer' import Footer from '../../components/footer'
import Header from '../../components/header' import Header from '../../components/header'
import { Booking } from '../../db/booking' import { BookingDocument } from '../../db/booking'
import { BOOKING_STATUS } from '../../db/bookingStatus' import { BOOKING_STATUS } from '../../db/bookingStatus'
import { getBookingByUUID } from '../../db/index' import { getBookingByUUID } from '../../db/index'
import { dateFormatFrontend } from '../../helpers/date' import { dateFormatFrontend } from '../../helpers/date'
@@ -28,7 +28,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
} }
} }
function getBookingStatus(booking: Booking) { function getBookingStatus(booking: BookingDocument) {
switch (booking.status) { switch (booking.status) {
case BOOKING_STATUS.REQUESTED: case BOOKING_STATUS.REQUESTED:
return 'In Bearbeitung' return 'In Bearbeitung'
@@ -43,7 +43,7 @@ function getBookingStatus(booking: Booking) {
} }
} }
async function cancelBooking(booking: Booking) { async function cancelBooking(booking: BookingDocument) {
const response = await fetch(`/api/booking/${booking.uuid}`, { const response = await fetch(`/api/booking/${booking.uuid}`, {
method: 'PATCH', method: 'PATCH',
mode: 'cors', mode: 'cors',
@@ -61,7 +61,7 @@ async function cancelBooking(booking: Booking) {
export default function ShowBooking({ export default function ShowBooking({
booking: bookingProp, booking: bookingProp,
}: { }: {
booking: Booking booking: BookingDocument
}) { }) {
const [booking, setBooking] = useState(bookingProp) const [booking, setBooking] = useState(bookingProp)