mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
further code improvements
This commit is contained in:
committed by
Thomas Ruoff
parent
52a68e9989
commit
4b16e07985
@@ -11,7 +11,7 @@ export interface Booking
|
|||||||
booker: Booker
|
booker: Booker
|
||||||
startDate: Date
|
startDate: Date
|
||||||
endDate: Date
|
endDate: Date
|
||||||
status: string
|
status: BOOKING_STATUS
|
||||||
purpose: string
|
purpose: string
|
||||||
org: string
|
org: string
|
||||||
destination: string
|
destination: string
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
export const BOOKING_STATUS = {
|
export enum BOOKING_STATUS {
|
||||||
REQUESTED: 'requested',
|
REQUESTED = 'requested',
|
||||||
CONFIRMED: 'confirmed',
|
CONFIRMED = 'confirmed',
|
||||||
REJECTED: 'rejected',
|
REJECTED = 'rejected',
|
||||||
CANCELED: 'canceled',
|
CANCELED = 'canceled',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,12 +41,7 @@ export async function getBookedDays() {
|
|||||||
export async function getBookingByUUID(uuid: string) {
|
export async function getBookingByUUID(uuid: string) {
|
||||||
await connect()
|
await connect()
|
||||||
const booking = await Booking.findOne({ uuid })
|
const booking = await Booking.findOne({ uuid })
|
||||||
return booking.populate('booker').execPopulate()
|
return booking?.populate('booker').execPopulate()
|
||||||
}
|
|
||||||
|
|
||||||
export async function getBookingByUUIDAsJSON(uuid: string) {
|
|
||||||
const booking = await getBookingByUUID(uuid)
|
|
||||||
return booking.toJSON()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createBooking({
|
export async function createBooking({
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { Booking } from '../../../db/booking'
|
import { Booking } from '../../../db/booking'
|
||||||
import { BOOKING_STATUS } from '../../../db/bookingStatus'
|
import { BOOKING_STATUS } from '../../../db/bookingStatus'
|
||||||
import { getBookingByUUID, getBookingByUUIDAsJSON } from '../../../db/index'
|
import { getBookingByUUID } from '../../../db/index'
|
||||||
|
|
||||||
export default async function userHandler(
|
export default async function userHandler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
@@ -9,7 +9,7 @@ export default async function userHandler(
|
|||||||
) {
|
) {
|
||||||
const {
|
const {
|
||||||
method,
|
method,
|
||||||
query: { uuids },
|
query: { uuid: uuids },
|
||||||
} = req
|
} = req
|
||||||
|
|
||||||
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
|
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
|
||||||
@@ -17,10 +17,6 @@ export default async function userHandler(
|
|||||||
let booking: Booking
|
let booking: Booking
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'GET':
|
|
||||||
booking = await getBookingByUUIDAsJSON(uuid)
|
|
||||||
res.status(200).json(booking)
|
|
||||||
break
|
|
||||||
case 'PATCH':
|
case 'PATCH':
|
||||||
booking = await getBookingByUUID(uuid)
|
booking = await getBookingByUUID(uuid)
|
||||||
const readonlyProps = Object.keys(req.body).filter(
|
const readonlyProps = Object.keys(req.body).filter(
|
||||||
|
|||||||
@@ -4,15 +4,25 @@ import Footer from '../../components/footer'
|
|||||||
import Header from '../../components/header'
|
import Header from '../../components/header'
|
||||||
import { Booking } from '../../db/booking'
|
import { Booking } from '../../db/booking'
|
||||||
import { BOOKING_STATUS } from '../../db/bookingStatus'
|
import { BOOKING_STATUS } from '../../db/bookingStatus'
|
||||||
import { getBookingByUUIDAsJSON } from '../../db/index'
|
import { getBookingByUUID } from '../../db/index'
|
||||||
import { dateFormatFrontend } from '../../helpers/date'
|
import { dateFormatFrontend } from '../../helpers/date'
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async (context) => {
|
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 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
|
// 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 {
|
return {
|
||||||
props: { booking: bookingJSON },
|
props: { booking: bookingJSON },
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user