move CRUD operations to helpers

This commit is contained in:
Thomas Ruoff
2021-06-07 23:32:54 +02:00
parent 865bbb20fa
commit 92477e5325
9 changed files with 102 additions and 93 deletions

View File

@@ -3,12 +3,11 @@ import Footer from '../../../../components/footer'
import Header from '../../../../components/header'
import Input from '../../../../components/input'
import Select from '../../../../components/select'
import { AdditionalCost, Bill } from '../../../../db/bill'
import { Booking } from '../../../../db/booking'
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
import { getMilageMax } from '../../../../db/index'
import { daysFormatFrontend } from '../../../../helpers/date'
import { getBillTotal } from '../../../../helpers/bill'
import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill'
import { getBookingStatus } from '../../../../helpers/booking'
import withSession, {
isAdminSession,
@@ -79,31 +78,6 @@ function getBillStatusLabel(status: BILL_STATUS) {
}
}
async function saveBill(
booking: Booking,
bill: {
milageStart: number
milageEnd: number
milage?: number
tarif: MILAGE_TARIFS
additionalCosts: AdditionalCost[]
status: BILL_STATUS
}
): Promise<Bill> {
const response = await fetch(`/api/admin/booking/${booking.uuid}/bill`, {
method: !!booking.bill ? 'PATCH' : 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
referrerPolicy: 'no-referrer',
body: JSON.stringify(bill),
})
return response.json()
}
export default function BookingBillPage({
booking: bookingProp,
milageMax,
@@ -136,7 +110,8 @@ export default function BookingBillPage({
setStoringError(null)
try {
const bill = await saveBill(booking, {
const saveBill = !!booking.bill ? createBill : patchBill
const bill = await saveBill(booking.uuid, {
milageStart,
milageEnd,
milage,