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

View File

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

View File

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

View File

@@ -16,8 +16,10 @@ export type Booking = {
zip: string zip: string
city: string city: string
bill?: Bill bill?: Bill
startDate: string start: string
endDate: string startDate: Date
endDate: Date
end: string
status?: BOOKING_STATUS status?: BOOKING_STATUS
purpose?: string purpose?: string
org?: string org?: string
@@ -56,26 +58,10 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
startDate: { startDate: {
type: Date, type: Date,
required: true, 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: { endDate: {
type: Date, type: Date,
required: false, 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: { days: {
type: [String], type: [String],
@@ -100,7 +86,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
type: String, type: String,
enum: Object.values(BOOKING_STATUS), enum: Object.values(BOOKING_STATUS),
required: true, required: true,
default: 'requested', default: BOOKING_STATUS.REQUESTED,
}, },
purpose: { type: String, required: false }, purpose: { type: String, required: false },
org: { 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 { BookingSchema.pre('validate', function (next: () => void): void {
const booking = this as BookingDocument const booking = this as BookingDocument
booking.days = getDays({ booking.days = getDays({

View File

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

View File

@@ -47,7 +47,7 @@ export const getServerSideBooking = async (
return { props: { booking: null } } return { props: { booking: null } }
} }
await booking.populate('bill').execPopulate() await booking.populate('bill')
// TODO: hack, not sure why _id is not serilizable // TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) as object 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: { requestBody: {
summary: getSummary(booking), summary: getSummary(booking),
// description, // description,
start: { date: booking.startDate }, start: { date: booking.start },
end: { date: booking.endDate }, end: { date: booking.end },
}, },
}) }, {})
booking.calendarEventId = response.data.id booking.calendarEventId = response.data.id

View File

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

View File

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

View File

@@ -1,7 +1,11 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Bill } from '../../../../db/bill' import { Bill } from '../../../../db/bill'
import { createBill, patchBill } from '../../../../db/index' 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 { const {
method, method,
query: { uuid: uuids }, query: { uuid: uuids },