From 4b16e079851d63a86cda21776357761bf696d672 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Sat, 29 Aug 2020 00:12:01 +0200 Subject: [PATCH] further code improvements --- db/booking.ts | 2 +- db/bookingStatus.ts | 10 +++++----- db/index.ts | 7 +------ pages/api/booking/[uuid].tsx | 8 ++------ pages/booking/[uuid].tsx | 18 ++++++++++++++---- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/db/booking.ts b/db/booking.ts index 64ad9f1..104afc6 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -11,7 +11,7 @@ export interface Booking booker: Booker startDate: Date endDate: Date - status: string + status: BOOKING_STATUS purpose: string org: string destination: string diff --git a/db/bookingStatus.ts b/db/bookingStatus.ts index 852df12..a417c08 100644 --- a/db/bookingStatus.ts +++ b/db/bookingStatus.ts @@ -1,6 +1,6 @@ -export const BOOKING_STATUS = { - REQUESTED: 'requested', - CONFIRMED: 'confirmed', - REJECTED: 'rejected', - CANCELED: 'canceled', +export enum BOOKING_STATUS { + REQUESTED = 'requested', + CONFIRMED = 'confirmed', + REJECTED = 'rejected', + CANCELED = 'canceled', } diff --git a/db/index.ts b/db/index.ts index 7ec14c2..16fb582 100644 --- a/db/index.ts +++ b/db/index.ts @@ -41,12 +41,7 @@ export async function getBookedDays() { export async function getBookingByUUID(uuid: string) { await connect() const booking = await Booking.findOne({ uuid }) - return booking.populate('booker').execPopulate() -} - -export async function getBookingByUUIDAsJSON(uuid: string) { - const booking = await getBookingByUUID(uuid) - return booking.toJSON() + return booking?.populate('booker').execPopulate() } export async function createBooking({ diff --git a/pages/api/booking/[uuid].tsx b/pages/api/booking/[uuid].tsx index 2249468..707a28f 100644 --- a/pages/api/booking/[uuid].tsx +++ b/pages/api/booking/[uuid].tsx @@ -1,7 +1,7 @@ import { NextApiRequest, NextApiResponse } from 'next' import { Booking } from '../../../db/booking' import { BOOKING_STATUS } from '../../../db/bookingStatus' -import { getBookingByUUID, getBookingByUUIDAsJSON } from '../../../db/index' +import { getBookingByUUID } from '../../../db/index' export default async function userHandler( req: NextApiRequest, @@ -9,7 +9,7 @@ export default async function userHandler( ) { const { method, - query: { uuids }, + query: { uuid: uuids }, } = req const uuid = Array.isArray(uuids) ? uuids[0] : uuids @@ -17,10 +17,6 @@ export default async function userHandler( let booking: Booking switch (method) { - case 'GET': - booking = await getBookingByUUIDAsJSON(uuid) - res.status(200).json(booking) - break case 'PATCH': booking = await getBookingByUUID(uuid) const readonlyProps = Object.keys(req.body).filter( diff --git a/pages/booking/[uuid].tsx b/pages/booking/[uuid].tsx index 487a5f9..698f830 100644 --- a/pages/booking/[uuid].tsx +++ b/pages/booking/[uuid].tsx @@ -4,15 +4,25 @@ import Footer from '../../components/footer' import Header from '../../components/header' import { Booking } from '../../db/booking' import { BOOKING_STATUS } from '../../db/bookingStatus' -import { getBookingByUUIDAsJSON } from '../../db/index' +import { getBookingByUUID } from '../../db/index' import { dateFormatFrontend } from '../../helpers/date' export const getServerSideProps: GetServerSideProps = async (context) => { - const { uuids } = context.params + const { + res, + params: { uuid: uuids }, + } = context const uuid = Array.isArray(uuids) ? uuids[0] : uuids - const booking = await getBookingByUUIDAsJSON(uuid) + const booking = await getBookingByUUID(uuid) + + if (!booking) { + res.statusCode = 404 + res.end() + return { props: {} } + } + // TODO: hack, not sure why _id is not serilizable - const bookingJSON = JSON.parse(JSON.stringify(booking)) + const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) return { props: { booking: bookingJSON }, }