mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
move admin api to /admin
This commit is contained in:
@@ -15,3 +15,12 @@ export default function withSession(handler: Handler) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isAdminSession = function (req: any, res: any) {
|
||||||
|
const user = req?.session.get('user')
|
||||||
|
if (user && user.role === 'admin') {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
res.status(401).end('Your are unauthorized. Best to move along...')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ async function saveBill(
|
|||||||
status: BILL_STATUS
|
status: BILL_STATUS
|
||||||
}
|
}
|
||||||
): Promise<BillDocument> {
|
): Promise<BillDocument> {
|
||||||
const response = await fetch(`/api/booking/${booking.uuid}/bill`, {
|
const response = await fetch(`/api/admin/booking/${booking.uuid}/bill`, {
|
||||||
method: booking.bill?._id ? 'PATCH' : 'POST',
|
method: booking.bill?._id ? 'PATCH' : 'POST',
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
cache: 'no-cache',
|
cache: 'no-cache',
|
||||||
|
|||||||
78
pages/admin/booking/[uuid]/index.tsx
Normal file
78
pages/admin/booking/[uuid]/index.tsx
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import Footer from '../../../../components/footer'
|
||||||
|
import Header from '../../../../components/header'
|
||||||
|
import Input from '../../../../components/input'
|
||||||
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||||
|
import { BookingDocument } from '../../../../db/booking'
|
||||||
|
import { getBookingStatus } from '../../../../helpers/booking'
|
||||||
|
|
||||||
|
export const getServerSideProps = getServerSideBooking
|
||||||
|
|
||||||
|
async function storeBooking(booking: BookingDocument) {
|
||||||
|
const response = await fetch(`/api/admin/booking/${booking.uuid}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
mode: 'cors',
|
||||||
|
cache: 'no-cache',
|
||||||
|
credentials: 'same-origin',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
referrerPolicy: 'no-referrer',
|
||||||
|
body: JSON.stringify({ ...booking }),
|
||||||
|
})
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ShowBookingAdmin({
|
||||||
|
booking: bookingProp,
|
||||||
|
}: {
|
||||||
|
booking: BookingDocument
|
||||||
|
}) {
|
||||||
|
const [booking, setBooking] = useState(bookingProp)
|
||||||
|
const [storingBooking, setStoringBooking] = useState(false)
|
||||||
|
const [storingBookingError, setStoringBookingError] = useState(null)
|
||||||
|
|
||||||
|
// in case the props change, update the internal state
|
||||||
|
useEffect(() => setBooking(bookingProp), [bookingProp])
|
||||||
|
|
||||||
|
const onStoreBooking = async () => {
|
||||||
|
try {
|
||||||
|
setStoringBookingError(null)
|
||||||
|
setStoringBooking(true)
|
||||||
|
const updatedBooking = await storeBooking(booking)
|
||||||
|
setBooking(updatedBooking)
|
||||||
|
} catch (error) {
|
||||||
|
setStoringBookingError('Buchung konnte nicht gespeichert werden.')
|
||||||
|
console.error('Failed to store booking', error)
|
||||||
|
}
|
||||||
|
setStoringBooking(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx-3 flex flex-col min-h-screen">
|
||||||
|
<Header />
|
||||||
|
<main className="flex-grow">
|
||||||
|
<h2 className="text-3xl">Buchung {booking.uuid}</h2>
|
||||||
|
<div>
|
||||||
|
<strong>Buchungsstatus:</strong> {getBookingStatus(booking)}
|
||||||
|
</div>
|
||||||
|
<Input label="Von" type="date" value={booking.startDate} readOnly />
|
||||||
|
<Input label="Bis" type="date" value={booking.endDate} readOnly />
|
||||||
|
{storingBookingError && (
|
||||||
|
<div className="error-message flex-grow">{storingBookingError}</div>
|
||||||
|
)}
|
||||||
|
<div className="my-6">
|
||||||
|
<button
|
||||||
|
onClick={onStoreBooking}
|
||||||
|
className="btn btn-blue"
|
||||||
|
disabled={storingBooking}
|
||||||
|
>
|
||||||
|
Buchung Speichern
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<Footer />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,20 +1,18 @@
|
|||||||
import { BillDocument } from '../../../../db/bill'
|
import { BillDocument } from '../../../../../db/bill'
|
||||||
import { createBill, patchBill } from '../../../../db/index'
|
import { createBill, patchBill } from '../../../../../db/index'
|
||||||
import withSession from '../../../../lib/session'
|
import withSession, { isAdminSession } from '../../../../../lib/session'
|
||||||
|
|
||||||
export default withSession(async function billHandler(req, res) {
|
export default withSession(async function billHandler(req, res) {
|
||||||
|
if (!isAdminSession(req, res)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
method,
|
method,
|
||||||
query: { uuid: uuids },
|
query: { uuid: uuids },
|
||||||
} = req
|
} = req
|
||||||
const bookingUUID = Array.isArray(uuids) ? uuids[0] : uuids
|
const bookingUUID = Array.isArray(uuids) ? uuids[0] : uuids
|
||||||
|
|
||||||
const user = req?.session.get('user')
|
|
||||||
if (!user || user.role !== 'admin') {
|
|
||||||
res.status(401).end('Your are unauthorized. Best to move along...')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let bill: BillDocument
|
let bill: BillDocument
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
32
pages/api/admin/booking/[uuid]/index.ts
Normal file
32
pages/api/admin/booking/[uuid]/index.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { BookingDocument } from '../../../../../db/booking'
|
||||||
|
import { getBookingByUUID } from '../../../../../db/index'
|
||||||
|
import withSession, { isAdminSession } from '../../../../../lib/session'
|
||||||
|
|
||||||
|
export default withSession(async function bookingHandler(req, res) {
|
||||||
|
if (!isAdminSession(req, res)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
method,
|
||||||
|
query: { uuid: uuids },
|
||||||
|
} = req
|
||||||
|
|
||||||
|
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
|
||||||
|
|
||||||
|
let booking: BookingDocument
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case 'PATCH':
|
||||||
|
booking = await getBookingByUUID(uuid)
|
||||||
|
|
||||||
|
// FIXME: validate all the things
|
||||||
|
booking.set(req.body)
|
||||||
|
await booking.save()
|
||||||
|
res.status(200).json(booking.toJSON())
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
res.setHeader('Allow', ['PATCH'])
|
||||||
|
res.status(405).end(`Method ${method} Not Allowed`)
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user