address breaking changes

This commit is contained in:
Thomas Ruoff
2022-03-14 22:35:30 +01:00
committed by Thomas Ruoff
parent 8222e04880
commit ef7f80fd92
10 changed files with 41 additions and 46 deletions

View File

@@ -3,19 +3,20 @@ import { BookContext } from '../../context/book'
import InputWrapper from '../inputWrapper'
import Input from '../input'
import Calendar from '../calendar'
import { dateFormatBackend } from '../../helpers/date'
export default function DateSelect() {
const { onChangeEvent, onChange, state } = useContext(BookContext)
const { startDate, endDate } = state.formData
const today = new Date().toISOString().substring(0, 10)
const today = dateFormatBackend(new Date())
return (
<>
<InputWrapper label="Datum" required>
<Calendar
start={startDate}
end={endDate}
start={dateFormatBackend(startDate)}
end={dateFormatBackend(endDate)}
onChange={onChange}
className="my-6 max-w-lg"
/>
@@ -27,7 +28,7 @@ export default function DateSelect() {
type="date"
className=""
name="startDate"
value={startDate || ''}
value={dateFormatBackend(startDate) || ''}
onChange={onChangeEvent}
min={today}
required
@@ -40,10 +41,10 @@ export default function DateSelect() {
type="date"
className=""
name="endDate"
value={endDate || ''}
value={dateFormatBackend(endDate) || ''}
placeholder="Von"
onChange={onChangeEvent}
min={startDate || today}
min={dateFormatBackend(startDate) || today}
/>
</div>
</div>

View File

@@ -122,15 +122,15 @@ export default function MyCalendar({
// when startDate missing or both are already set
if (!startDate || (!!startDate && !!endDate)) {
onChange({ startDate: dateFormatBackend(date), endDate: null })
onChange({ startDate: date, endDate: null })
return
}
// when startDate set, but end missing
if (isAfter(date, startDate)) {
onChange({ endDate: dateFormatBackend(date) })
onChange({ endDate: date })
} else {
onChange({ startDate: dateFormatBackend(date), endDate: start })
onChange({ startDate: date, endDate: date })
}
}}
tileClassName={tileClassName}

View File

@@ -5,7 +5,7 @@ import { clearBookingData, loadBookingData } from '../helpers/storage'
import { createBooking } from '../helpers/booking'
import { Booking } from '../db/booking'
export type BookFormData = Omit<Booking, 'uuid' | 'calendarEventId'>
export type BookFormData = Omit<Booking, 'uuid' | 'calendarEventId' | 'start' | 'end'>
type BookingProviderState = {
postData?: boolean
@@ -103,8 +103,8 @@ const initialState: BookingProviderState = {
postDataError: null,
postDataSuccess: null,
formData: {
startDate: '',
endDate: '',
startDate: null,
endDate: null,
purpose: '',
org: '',
destination: '',

View File

@@ -16,8 +16,10 @@ export type Booking = {
zip: string
city: string
bill?: Bill
startDate: string
endDate: string
start: string
startDate: Date
endDate: Date
end: string
status?: BOOKING_STATUS
purpose?: string
org?: string
@@ -56,26 +58,10 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
startDate: {
type: Date,
required: true,
get: dateFormatBackend,
validate: {
validator: function (v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }): string =>
`${props.value} is in the past`,
},
},
endDate: {
type: Date,
required: false,
get: dateFormatBackend,
validate: {
validator: function (v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }): string =>
`${props.value} is in the past`,
},
},
days: {
type: [String],
@@ -100,7 +86,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
type: String,
enum: Object.values(BOOKING_STATUS),
required: true,
default: 'requested',
default: BOOKING_STATUS.REQUESTED,
},
purpose: { type: String, required: false },
org: { type: String, required: false },
@@ -114,6 +100,16 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
}
)
BookingSchema.virtual('start').get(function (): string {
const booking = this as BookingDocument
return dateFormatBackend(booking.startDate)
})
BookingSchema.virtual('end').get(function (): string {
const booking = this as BookingDocument
return dateFormatBackend(booking.endDate)
})
BookingSchema.pre('validate', function (next: () => void): void {
const booking = this as BookingDocument
booking.days = getDays({

View File

@@ -7,18 +7,12 @@ let connectedPromise: Promise<mongoose.Mongoose>
export const MONGO_URI = process.env.MONGO_URI
export const MONGODB_OPTIONS = {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
}
export function connect(): Promise<mongoose.Mongoose> {
if (connectedPromise) {
return connectedPromise
}
connectedPromise = mongoose.connect(process.env.MONGO_URI, MONGODB_OPTIONS)
connectedPromise = mongoose.connect(process.env.MONGO_URI)
return connectedPromise
}

View File

@@ -47,7 +47,7 @@ export const getServerSideBooking = async (
return { props: { booking: null } }
}
await booking.populate('bill').execPopulate()
await booking.populate('bill')
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) as object

View File

@@ -50,10 +50,10 @@ export async function createCalendarEvent(booking: Booking): Promise<Booking> {
requestBody: {
summary: getSummary(booking),
// description,
start: { date: booking.startDate },
end: { date: booking.endDate },
start: { date: booking.start },
end: { date: booking.end },
},
})
}, {})
booking.calendarEventId = response.data.id

View File

@@ -38,7 +38,7 @@ function ShowBookingAdmin({ booking: bookingProp }: { booking: Booking }) {
return (
<Layout>
<h2 className="text-3xl">Buchung {booking.uuid}</h2>
<Calendar start={booking.startDate} end={booking.endDate} />
<Calendar start={booking.start} end={booking.end} />
<div>
<strong>Buchungszeitraum:</strong> {daysFormatFrontend(booking.days)}
</div>

View File

@@ -3,14 +3,14 @@ import NextAuth from 'next-auth'
import EmailProvider from 'next-auth/providers/email'
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
import { MONGO_URI, MONGODB_OPTIONS } from '../../../db'
import { MONGO_URI } from '../../../db'
import { MongoClient } from 'mongodb'
let client: MongoClient
async function getMongoClient() {
if (!client) {
client = new MongoClient(MONGO_URI, MONGODB_OPTIONS)
client = new MongoClient(MONGO_URI)
await client.connect()
}

View File

@@ -1,7 +1,11 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Bill } from '../../../../db/bill'
import { createBill, patchBill } from '../../../../db/index'
export default async function billHandler(req, res): Promise<void> {
export default async function billHandler(
req: NextApiRequest,
res: NextApiResponse
): Promise<void> {
const {
method,
query: { uuid: uuids },