mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
extract patch booking to db
This commit is contained in:
committed by
Thomas Ruoff
parent
ef7f80fd92
commit
d9cd2af314
12
db/index.ts
12
db/index.ts
@@ -76,6 +76,18 @@ export async function createBooking({
|
|||||||
return booking.toJSON()
|
return booking.toJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function patchBooking(
|
||||||
|
bookingUUID: string,
|
||||||
|
bookingData: Booking,
|
||||||
|
): Promise<Booking> {
|
||||||
|
await connect()
|
||||||
|
const booking = await getBookingByUUID(bookingUUID)
|
||||||
|
booking.set(bookingData);
|
||||||
|
await booking.save()
|
||||||
|
|
||||||
|
return booking.toJSON()
|
||||||
|
}
|
||||||
|
|
||||||
export async function createBill(
|
export async function createBill(
|
||||||
bookingUUID: string,
|
bookingUUID: string,
|
||||||
billData: Bill
|
billData: Bill
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { BookingDocument } from '../../../../db/booking'
|
import { BookingDocument } from '../../../../db/booking'
|
||||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||||
import { getBookingByUUID } from '../../../../db/index'
|
import { patchBooking } from '../../../../db/index'
|
||||||
|
|
||||||
export default async function userHandler(
|
export default async function userHandler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
@@ -18,8 +18,6 @@ export default async function userHandler(
|
|||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'PATCH':
|
case 'PATCH':
|
||||||
booking = await getBookingByUUID(uuid)
|
|
||||||
|
|
||||||
if (!Object.values(BOOKING_STATUS).includes(req.body.status)) {
|
if (!Object.values(BOOKING_STATUS).includes(req.body.status)) {
|
||||||
res
|
res
|
||||||
.status(400)
|
.status(400)
|
||||||
@@ -33,8 +31,8 @@ export default async function userHandler(
|
|||||||
|
|
||||||
booking.set(req.body)
|
booking.set(req.body)
|
||||||
try {
|
try {
|
||||||
await booking.save()
|
const booking = await patchBooking(uuid, req.body);
|
||||||
res.status(200).json(booking.toJSON())
|
res.status(200).json(booking)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(400).end(`Failed to save booking: ${error.message}`)
|
res.status(400).end(`Failed to save booking: ${error.message}`)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user