diff --git a/db/bill.ts b/db/bill.ts new file mode 100644 index 0000000..c1a31a2 --- /dev/null +++ b/db/bill.ts @@ -0,0 +1,79 @@ +import * as mongoose from 'mongoose' +import { BILL_STATUS, MILAGE_RATES } from './enums' +import { BookingDocument } from './booking' + +export interface AdditionalCosts { + name: string + value: number +} + +export interface BillDocument + extends mongoose.SchemaTimestampsConfig, + mongoose.Document { + booking: BookingDocument + milageStart: number + milageEnd: number + milage?: number + rate: MILAGE_RATES + status: BILL_STATUS + additionalCosts: AdditionalCosts[] +} + +export interface BillModel extends mongoose.Model {} + +const BillSchema = new mongoose.Schema( + { + booking: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Booking', + required: true, + }, + milageStart: { type: Number, required: true }, + milageEnd: { type: Number, required: true }, + rate: { + type: Number, + enum: Object.values(MILAGE_RATES), + default: MILAGE_RATES.EXTERN_UP_TO_200, + required: true, + }, + status: { + type: String, + enum: Object.values(BILL_STATUS), + default: BILL_STATUS.UNINVOICED, + required: true, + }, + additionalCosts: [ + { + name: { type: String, required: true }, + value: { type: Number, required: true }, + }, + ], + }, + { timestamps: true, collation: { locale: 'de', strength: 1 } } +) + +BillSchema.virtual('milage').get(function () { + const bill = this as BillDocument + + if (!bill.milageStart || !bill.milageEnd) { + return null + } + return bill.milageEnd - bill.milageStart +}) + +BillSchema.virtual('total').get(function () { + const bill = this as BillDocument + + if (!bill.milageStart || !bill.milageEnd) { + return null + } + const milageCosts = bill.milage * bill.rate + const additionalCostSum = bill.additionalCosts + .map(({ value }) => value) + .reduce((acc, value) => acc + value, 0) + + return milageCosts + additionalCostSum +}) + +export default mongoose.models.Bill || + mongoose.model('Bill', BillSchema) diff --git a/db/billingRates.ts b/db/billingRates.ts deleted file mode 100644 index c145218..0000000 --- a/db/billingRates.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum BILLING_RATES { - INTERN = 0.24, - EXTERN_UP_TO_200 = 0.45, - EXTERN_OVER_200 = 0.55, -} diff --git a/db/booking.ts b/db/booking.ts index 943b6db..30b8136 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -2,7 +2,7 @@ import * as mongoose from 'mongoose' import { v4 as uuidv4 } from 'uuid' import { dateFormatBackend, getDays } from '../helpers/date' import { BookerDocument } from './booker' -import { BOOKING_STATUS } from './bookingStatus' +import { BOOKING_STATUS } from './enums' export interface BookingDocument extends mongoose.Document, @@ -56,6 +56,11 @@ const BookingSchema = new mongoose.Schema( purpose: { type: String, required: false }, org: { type: String, required: false }, destination: { type: String, required: false }, + bill: { + type: mongoose.Schema.Types.ObjectId, + ref: 'bill', + required: false, + }, }, { timestamps: true, diff --git a/db/bookingBill.ts b/db/bookingBill.ts deleted file mode 100644 index 01f5cb0..0000000 --- a/db/bookingBill.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as mongoose from 'mongoose' -import { BILLING_RATES } from './billingRates' -import { PAYMENT_STATE } from './paymentState' -import { BookingDocument } from './booking' - -export interface AdditionalCosts { - name: string - value: number -} - -export interface BookingBillDocument - extends mongoose.SchemaTimestampsConfig, - mongoose.Document { - booking: BookingDocument - milageStart: number - milageEnd: number - milage?: number - rate: BILLING_RATES - paymentState: PAYMENT_STATE - additionalCosts: AdditionalCosts[] -} - -export interface BookingBillModel extends mongoose.Model {} - -const BookingBillSchema = new mongoose.Schema( - { - booking: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Booking', - required: true, - }, - milageStart: { type: Number, required: true }, - milageEnd: { type: Number, required: true }, - rate: { - type: Number, - enum: Object.values(BILLING_RATES), - default: BILLING_RATES.EXTERN_UP_TO_200, - required: true, - }, - paymentState: { - type: String, - enum: Object.values(PAYMENT_STATE), - default: PAYMENT_STATE.OUTSTANDING, - required: true, - }, - additionalCosts: [ - { - name: { type: String, required: true }, - value: { type: Number, required: true }, - }, - ], - }, - { timestamps: true, collation: { locale: 'de', strength: 1 } } -) - -BookingBillSchema.virtual('milage').get(function () { - if (!this.milageStart || !this.milageEnd) { - return null - } - return this.milageEnd - this.milageStart -}) - -BookingBillSchema.virtual('invoiceAmount').get(function () { - if (!this.milageStart || !this.milageEnd) { - return null - } - const milageCosts = this.milage * this.rate - const additionalCostSum = this.additionalConsts.reduce( - (memo: number, { value }) => memo + value, - 0 - ) - return milageCosts + additionalCostSum -}) - -export default mongoose.models.BookingBill || - mongoose.model( - 'BookingBill', - BookingBillSchema - ) diff --git a/db/bookingStatus.ts b/db/bookingStatus.ts deleted file mode 100644 index a417c08..0000000 --- a/db/bookingStatus.ts +++ /dev/null @@ -1,6 +0,0 @@ -export enum BOOKING_STATUS { - REQUESTED = 'requested', - CONFIRMED = 'confirmed', - REJECTED = 'rejected', - CANCELED = 'canceled', -} diff --git a/db/enums.ts b/db/enums.ts new file mode 100644 index 0000000..b1f0d3d --- /dev/null +++ b/db/enums.ts @@ -0,0 +1,23 @@ +export enum BOOKING_STATUS { + REQUESTED = 'requested', + CONFIRMED = 'confirmed', + REJECTED = 'rejected', + CANCELED = 'canceled', +} + +export enum BILL_STATUS { + UNINVOICED = 'uninvoiced', + INVOICED = 'invoiced', + PAID = 'paid', +} + +export enum MILAGE_RATES { + INTERN_UP_TO_200 = 0.37, + INTERN_200_100 = 0.22, + INTERN_1001_2000 = 0.15, + INTERN_OVER_2000 = 0.13, + EXTERN_UP_TO_200 = 0.42, + EXTERN_200_100 = 0.25, + EXTERN_1001_2000 = 0.2, + EXTERN_OVER_2000 = 0.18, +} diff --git a/db/index.ts b/db/index.ts index 3704c05..2368320 100644 --- a/db/index.ts +++ b/db/index.ts @@ -2,7 +2,7 @@ import * as mongoose from 'mongoose' import Booker from './booker' import Booking from './booking' import { dateFormatFrontend } from '../helpers/date' -import { BOOKING_STATUS } from './bookingStatus' +import { BOOKING_STATUS } from './enums' let connectedPromise: Promise diff --git a/db/paymentState.ts b/db/paymentState.ts deleted file mode 100644 index f878fe1..0000000 --- a/db/paymentState.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum PAYMENT_STATE { - OUTSTANDING = 'outstanding', - INVOICED = 'invoiced', - PAID = 'paid', -} diff --git a/helpers/ical.ts b/helpers/ical.ts index 17b4381..82655f8 100644 --- a/helpers/ical.ts +++ b/helpers/ical.ts @@ -1,6 +1,6 @@ import { createEvents, EventStatus } from 'ics' import { BookingDocument } from '../db/booking' -import { BOOKING_STATUS } from '../db/bookingStatus' +import { BOOKING_STATUS } from '../db/enums' import { getBaseURL } from './url' function convertDay(value: string): [number, number, number] { diff --git a/pages/api/booking/[uuid].tsx b/pages/api/booking/[uuid].tsx index 2009748..8ff4959 100644 --- a/pages/api/booking/[uuid].tsx +++ b/pages/api/booking/[uuid].tsx @@ -1,6 +1,6 @@ import { NextApiRequest, NextApiResponse } from 'next' import { BookingDocument } from '../../../db/booking' -import { BOOKING_STATUS } from '../../../db/bookingStatus' +import { BOOKING_STATUS } from '../../../db/enums' import { getBookingByUUID } from '../../../db/index' export default async function userHandler( diff --git a/pages/booking/[uuid].tsx b/pages/booking/[uuid].tsx index 22fbbae..fb24066 100644 --- a/pages/booking/[uuid].tsx +++ b/pages/booking/[uuid].tsx @@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react' import Footer from '../../components/footer' import Header from '../../components/header' import { BookingDocument } from '../../db/booking' -import { BOOKING_STATUS } from '../../db/bookingStatus' +import { BOOKING_STATUS } from '../../db/enums' import { getBookingByUUID } from '../../db/index' import { dateFormatFrontend } from '../../helpers/date'