diff --git a/components/book/dateSelect.tsx b/components/book/dateSelect.tsx index e31311b..bb48879 100644 --- a/components/book/dateSelect.tsx +++ b/components/book/dateSelect.tsx @@ -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 ( <> @@ -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} /> diff --git a/components/calendar.tsx b/components/calendar.tsx index 49c808b..a78fd90 100644 --- a/components/calendar.tsx +++ b/components/calendar.tsx @@ -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} diff --git a/context/book.tsx b/context/book.tsx index 34b779c..b06d5d8 100644 --- a/context/book.tsx +++ b/context/book.tsx @@ -5,7 +5,7 @@ import { clearBookingData, loadBookingData } from '../helpers/storage' import { createBooking } from '../helpers/booking' import { Booking } from '../db/booking' -export type BookFormData = Omit +export type BookFormData = Omit 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: '', diff --git a/db/booking.ts b/db/booking.ts index 18185ac..4555453 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -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( 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( 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( } ) +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({ diff --git a/db/index.ts b/db/index.ts index c3563a2..e5fab30 100644 --- a/db/index.ts +++ b/db/index.ts @@ -7,18 +7,12 @@ let connectedPromise: Promise export const MONGO_URI = process.env.MONGO_URI -export const MONGODB_OPTIONS = { - useCreateIndex: true, - useNewUrlParser: true, - useUnifiedTopology: true, -} - export function connect(): Promise { if (connectedPromise) { return connectedPromise } - connectedPromise = mongoose.connect(process.env.MONGO_URI, MONGODB_OPTIONS) + connectedPromise = mongoose.connect(process.env.MONGO_URI) return connectedPromise } diff --git a/lib/getServerSideProps.ts b/lib/getServerSideProps.ts index 12fec42..5fdfd7a 100644 --- a/lib/getServerSideProps.ts +++ b/lib/getServerSideProps.ts @@ -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 diff --git a/lib/googlecalendar.ts b/lib/googlecalendar.ts index 7b67182..2bf34c3 100644 --- a/lib/googlecalendar.ts +++ b/lib/googlecalendar.ts @@ -50,10 +50,10 @@ export async function createCalendarEvent(booking: Booking): Promise { 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 diff --git a/pages/admin/bookings/[uuid]/index.tsx b/pages/admin/bookings/[uuid]/index.tsx index 5ac5dce..62e8c1f 100644 --- a/pages/admin/bookings/[uuid]/index.tsx +++ b/pages/admin/bookings/[uuid]/index.tsx @@ -38,7 +38,7 @@ function ShowBookingAdmin({ booking: bookingProp }: { booking: Booking }) { return (

Buchung {booking.uuid}

- +
Buchungszeitraum: {daysFormatFrontend(booking.days)}
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 61089a9..5059177 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -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() } diff --git a/pages/api/bookings/[uuid]/bill.ts b/pages/api/bookings/[uuid]/bill.ts index 3ce5a0a..be954c2 100644 --- a/pages/api/bookings/[uuid]/bill.ts +++ b/pages/api/bookings/[uuid]/bill.ts @@ -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 { +export default async function billHandler( + req: NextApiRequest, + res: NextApiResponse + ): Promise { const { method, query: { uuid: uuids },