mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
default to max milage found for a new bill
This commit is contained in:
10
db/index.ts
10
db/index.ts
@@ -28,7 +28,6 @@ export async function getBookedDays() {
|
||||
export async function getBookingByUUID(uuid: string) {
|
||||
await connect()
|
||||
return Booking.findOne({ uuid })
|
||||
//return booking.populate('bill').populate('booker').execPopulate()
|
||||
}
|
||||
|
||||
export async function getBookings() {
|
||||
@@ -116,3 +115,12 @@ export async function patchBill(bookingUUID: string, billData: BillDocument) {
|
||||
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function getMilageMax(): Promise<number> {
|
||||
const billMaxMilageEnd = await Bill.findOne({})
|
||||
.sort('-milageEnd')
|
||||
.select('milageEnd')
|
||||
.exec()
|
||||
|
||||
return billMaxMilageEnd?.milageEnd
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
MILAGE_RATES,
|
||||
getMilageRateValue,
|
||||
} from '../../../db/enums'
|
||||
import { getBookingByUUID } from '../../../db/index'
|
||||
import { getBookingByUUID, getMilageMax } from '../../../db/index'
|
||||
import { dateFormatFrontend } from '../../../helpers/date'
|
||||
|
||||
const milageRateOptions = Object.values(MILAGE_RATES).map((rate) => {
|
||||
@@ -44,10 +44,13 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
|
||||
res.end()
|
||||
return { props: {} }
|
||||
}
|
||||
|
||||
const milageMax = await getMilageMax()
|
||||
|
||||
// TODO: hack, not sure why _id is not serilizable
|
||||
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
|
||||
return {
|
||||
props: { booking: bookingJSON },
|
||||
props: { booking: bookingJSON, milageMax },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,11 +119,15 @@ async function saveBill(
|
||||
|
||||
export default function BillPage({
|
||||
booking: bookingProp,
|
||||
milageMax,
|
||||
}: {
|
||||
booking: BookingDocument
|
||||
milageMax: number
|
||||
}) {
|
||||
const [booking, setBooking] = useState(bookingProp)
|
||||
const [milageStart, setMilageStart] = useState(booking.bill?.milageStart)
|
||||
const [milageStart, setMilageStart] = useState(
|
||||
booking.bill?.milageStart || milageMax
|
||||
)
|
||||
const [milageEnd, setMilageEnd] = useState(booking.bill?.milageEnd)
|
||||
const [rate, setRate] = useState(
|
||||
booking.bill?.rate || MILAGE_RATES.EXTERN_LTE_200
|
||||
|
||||
Reference in New Issue
Block a user