mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 06:57:12 +01:00
switch to prisma
This commit is contained in:
@@ -1,79 +1,27 @@
|
||||
import { MILAGE_TARIFS } from '../db/enums'
|
||||
import { AdditionalCost, Bill } from '../db/bill'
|
||||
import { Bill, Prisma } from '@prisma/client'
|
||||
import fetch from './fetch'
|
||||
|
||||
function roundToCent(amount: number): number {
|
||||
return Math.round(amount * 100) / 100
|
||||
}
|
||||
|
||||
export function getMilageCosts({
|
||||
tarif,
|
||||
km,
|
||||
}: {
|
||||
tarif: MILAGE_TARIFS
|
||||
km: number
|
||||
}): number {
|
||||
if (tarif === MILAGE_TARIFS.NOCHARGE) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if (km <= 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
let rate: number
|
||||
|
||||
if (tarif === MILAGE_TARIFS.EXTERN) {
|
||||
if (km <= 200) {
|
||||
rate = 0.42
|
||||
} else if (km <= 1000) {
|
||||
rate = 0.25
|
||||
} else if (km <= 2000) {
|
||||
rate = 0.2
|
||||
} else {
|
||||
rate = 0.18
|
||||
}
|
||||
}
|
||||
|
||||
if (tarif === MILAGE_TARIFS.INTERN) {
|
||||
if (km <= 200) {
|
||||
rate = 0.37
|
||||
} else if (km <= 1000) {
|
||||
rate = 0.22
|
||||
} else if (km <= 2000) {
|
||||
rate = 0.15
|
||||
} else {
|
||||
rate = 0.13
|
||||
}
|
||||
}
|
||||
|
||||
if (rate === undefined) {
|
||||
throw Error('Unable to determine rate')
|
||||
}
|
||||
|
||||
return roundToCent(km * rate)
|
||||
}
|
||||
|
||||
export function getBillTotal({
|
||||
tarif,
|
||||
milage,
|
||||
additionalCosts,
|
||||
}: {
|
||||
tarif: MILAGE_TARIFS
|
||||
tarif: Prisma.Decimal
|
||||
milage?: number
|
||||
additionalCosts: AdditionalCost[]
|
||||
}): number {
|
||||
const milageCosts = getMilageCosts({ tarif, km: milage })
|
||||
additionalCosts: Prisma.AdditionalCostsCreateInput[]
|
||||
}): Prisma.Decimal {
|
||||
const milageCosts = tarif.mul(milage)
|
||||
const additionalCostsSum = additionalCosts
|
||||
.map(({ value }) => value)
|
||||
.reduce((acc: number, value: number) => acc + value, 0)
|
||||
.reduce((acc, {value} ) => (value as Prisma.Decimal).plus(acc), new Prisma.Decimal(0))
|
||||
|
||||
return roundToCent(milageCosts + additionalCostsSum)
|
||||
|
||||
return additionalCostsSum.add(milageCosts).toDecimalPlaces(2);
|
||||
}
|
||||
|
||||
export async function createBill(
|
||||
bookingUuid: string,
|
||||
bill: Bill
|
||||
bill: Prisma.BillCreateInput
|
||||
): Promise<Bill> {
|
||||
return fetch(`/api/bookings/${bookingUuid}/bill`, {
|
||||
method: 'POST',
|
||||
@@ -83,7 +31,7 @@ export async function createBill(
|
||||
|
||||
export async function patchBill(
|
||||
bookingUuid: string,
|
||||
bill: Bill
|
||||
bill: Prisma.BillUpdateInput
|
||||
): Promise<Bill> {
|
||||
return fetch(`/api/bookings/${bookingUuid}/bill`, {
|
||||
method: 'POST',
|
||||
|
||||
Reference in New Issue
Block a user