further work on billing

This commit is contained in:
Thomas Ruoff
2020-10-07 00:32:22 +02:00
committed by Thomas Ruoff
parent f8434233d9
commit c396cdcbf9
6 changed files with 75 additions and 52 deletions

View File

@@ -1,6 +1,5 @@
import * as mongoose from 'mongoose'
import { BILL_STATUS, MILAGE_RATES, getMilageRateValue } from './enums'
import { BookingDocument } from './booking'
export interface AdditionalCosts {
name: string
@@ -10,7 +9,6 @@ export interface AdditionalCosts {
export interface BillDocument
extends mongoose.SchemaTimestampsConfig,
mongoose.Document {
booking: BookingDocument
milageStart: number
milageEnd: number
milage?: number
@@ -23,12 +21,6 @@ export interface BillModel extends mongoose.Model<BillDocument> {}
const BillSchema = new mongoose.Schema<BillDocument>(
{
booking: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booking',
unique: true,
required: true,
},
milageStart: {
type: Number,
required: true,

View File

@@ -1,6 +1,7 @@
import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays } from '../helpers/date'
import { BillDocument } from './bill'
import { BookerDocument } from './booker'
import { BOOKING_STATUS } from './enums'
@@ -9,6 +10,7 @@ export interface BookingDocument
mongoose.SchemaTimestampsConfig {
uuid: string
booker: BookerDocument
bill: BillDocument
startDate: Date
endDate: Date
status: BOOKING_STATUS
@@ -35,6 +37,11 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
ref: 'Booker',
required: true,
},
bill: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Bill',
required: false,
},
startDate: {
type: Date,
required: true,
@@ -56,11 +63,6 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
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,

View File

@@ -27,8 +27,8 @@ export async function getBookedDays() {
export async function getBookingByUUID(uuid: string) {
await connect()
const booking = await Booking.findOne({ uuid })
return booking?.populate('booker').execPopulate()
return Booking.findOne({ uuid })
//return booking.populate('bill').populate('booker').execPopulate()
}
export async function getBookings() {
@@ -87,14 +87,32 @@ export async function createBooking({
export async function createBill(bookingUUID: string, billData: BillDocument) {
await connect()
const booking = await getBookingByUUID(bookingUUID)
const bill =
(await Bill.findOne({ booking: booking._id })) ||
new Bill({ booking: booking._id })
const bill = new Bill()
bill.set(billData)
await bill.save()
await bill.populate('booking').execPopulate()
booking.bill = bill._id
await booking.save()
return bill.toJSON()
}
export async function patchBill(bookingUUID: string, billData: BillDocument) {
await connect()
const booking = await getBookingByUUID(bookingUUID)
const bill =
(booking.bill && (await Bill.findById(booking.bill))) ||
(await Bill.create())
bill.set(billData)
await bill.save()
if (booking.bill !== bill._id) {
booking.bill = bill._id
await booking.save()
}
return bill.toJSON()
}