mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
remove all bill related stuff
This commit is contained in:
94
db/bill.ts
94
db/bill.ts
@@ -1,94 +0,0 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
import { BILL_STATUS, MILAGE_TARIFS } from './enums'
|
||||
import { getBillTotal } from '../helpers/bill'
|
||||
|
||||
export interface IAdditionalCost {
|
||||
name: string
|
||||
value: number
|
||||
}
|
||||
|
||||
export interface IBill {
|
||||
milageStart: number
|
||||
milageEnd: number
|
||||
milage?: number
|
||||
tarif: MILAGE_TARIFS
|
||||
status: BILL_STATUS
|
||||
additionalCosts: IAdditionalCost[]
|
||||
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
export type BillDocument = IBill &
|
||||
mongoose.SchemaTimestampsConfig &
|
||||
mongoose.Document
|
||||
|
||||
export type BillModel = mongoose.Model<IBill>
|
||||
|
||||
const BillSchema = new mongoose.Schema<IBill>(
|
||||
{
|
||||
milageStart: {
|
||||
type: Number,
|
||||
required: true,
|
||||
validate: {
|
||||
validator: function (v: number): boolean {
|
||||
const bill = this as BillDocument
|
||||
|
||||
return v <= bill.milageEnd
|
||||
},
|
||||
message: (props: { value: Number }) =>
|
||||
`${props.value} is bigger than milageEnd!`,
|
||||
},
|
||||
},
|
||||
milageEnd: {
|
||||
type: Number,
|
||||
required: true,
|
||||
|
||||
validate: {
|
||||
validator: function (v: number): boolean {
|
||||
const bill = this as BillDocument
|
||||
|
||||
return v >= bill.milageStart
|
||||
},
|
||||
message: (props: { value: Number }) =>
|
||||
`${props.value} is smaller than milageStart!`,
|
||||
},
|
||||
},
|
||||
tarif: {
|
||||
type: String,
|
||||
enum: Object.values(MILAGE_TARIFS),
|
||||
default: MILAGE_TARIFS.EXTERN,
|
||||
required: true,
|
||||
},
|
||||
additionalCosts: [
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
value: { type: Number, required: true },
|
||||
},
|
||||
],
|
||||
status: {
|
||||
type: String,
|
||||
enum: Object.values(BILL_STATUS),
|
||||
default: BILL_STATUS.UNINVOICED,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true, getters: true },
|
||||
toObject: { virtuals: true, getters: true },
|
||||
}
|
||||
)
|
||||
|
||||
BillSchema.virtual('milage').get(function (): number {
|
||||
const bill = this as BillDocument
|
||||
return bill.milageEnd - bill.milageStart
|
||||
})
|
||||
|
||||
BillSchema.virtual('total').get(function (): number {
|
||||
const bill = this as BillDocument
|
||||
|
||||
return getBillTotal(bill)
|
||||
})
|
||||
|
||||
export default (mongoose.models.Bill ||
|
||||
mongoose.model<BillDocument, BillModel>('Bill', BillSchema)) as BillModel
|
||||
@@ -8,18 +8,16 @@ import {
|
||||
} from '../helpers/date'
|
||||
import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar'
|
||||
|
||||
import { IBill } from './bill'
|
||||
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
|
||||
|
||||
export interface IBooking {
|
||||
uuid: string
|
||||
name: string
|
||||
email: string
|
||||
phone: string
|
||||
phone?: string
|
||||
street: string
|
||||
zip: string
|
||||
city: string
|
||||
bill?: IBill
|
||||
// format YYYY-MM-DD
|
||||
startDate: string
|
||||
// format YYYY-MM-DD
|
||||
@@ -29,10 +27,7 @@ export interface IBooking {
|
||||
org?: string
|
||||
destination?: string
|
||||
days?: string[]
|
||||
calendarEventId: string
|
||||
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
calendarEventId?: string
|
||||
toJSON?: () => IBooking
|
||||
}
|
||||
|
||||
@@ -54,11 +49,6 @@ const BookingSchema = new mongoose.Schema(
|
||||
street: { type: String, required: true },
|
||||
zip: { type: String, required: true },
|
||||
city: { type: String, required: true },
|
||||
bill: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Bill',
|
||||
required: false,
|
||||
},
|
||||
startDate: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
12
db/enums.ts
12
db/enums.ts
@@ -5,18 +5,6 @@ export enum BOOKING_STATUS {
|
||||
CANCELED = 'canceled',
|
||||
}
|
||||
|
||||
export enum BILL_STATUS {
|
||||
UNINVOICED = 'uninvoiced',
|
||||
INVOICED = 'invoiced',
|
||||
PAID = 'paid',
|
||||
}
|
||||
|
||||
export enum MILAGE_TARIFS {
|
||||
INTERN = 'intern',
|
||||
EXTERN = 'extern',
|
||||
NOCHARGE = 'nocharge',
|
||||
}
|
||||
|
||||
export enum VALIDATION_ERRORS {
|
||||
AT_LEAST_ONE_DAY_BOOKED = 'atLeastOneDayBooked',
|
||||
}
|
||||
|
||||
57
db/index.ts
57
db/index.ts
@@ -1,13 +1,12 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
import BookingModel, { IBooking } from './booking'
|
||||
import BillModel, { IBill } from './bill'
|
||||
import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
||||
import { BOOKING_STATUS } from './enums'
|
||||
import { uniqueFilter } from '../helpers/array'
|
||||
|
||||
export const MONGO_URI = process.env.MONGO_URI
|
||||
|
||||
mongoose.set('strictQuery', false);
|
||||
mongoose.set('strictQuery', false)
|
||||
|
||||
mongoose.connect(MONGO_URI, {
|
||||
serverSelectionTimeoutMS: 3000,
|
||||
@@ -23,7 +22,9 @@ export async function getBookedDays(
|
||||
return [...bookedInDatabase, ...bookedInCalendar].filter(uniqueFilter).sort()
|
||||
}
|
||||
|
||||
export async function getBookingByUUID(uuid: string): Promise<mongoose.HydratedDocument<IBooking>> {
|
||||
export async function getBookingByUUID(
|
||||
uuid: string
|
||||
): Promise<mongoose.HydratedDocument<IBooking>> {
|
||||
return BookingModel.findOne({ uuid })
|
||||
}
|
||||
|
||||
@@ -53,7 +54,9 @@ export async function createBooking({
|
||||
street,
|
||||
zip,
|
||||
city,
|
||||
}: IBooking): Promise<mongoose.FlattenMaps<IBooking & { _id: mongoose.Types.ObjectId }>> {
|
||||
}: IBooking): Promise<
|
||||
mongoose.FlattenMaps<IBooking & { _id: mongoose.Types.ObjectId }>
|
||||
> {
|
||||
const booking = new BookingModel({
|
||||
startDate,
|
||||
endDate,
|
||||
@@ -83,49 +86,3 @@ export async function patchBooking(
|
||||
|
||||
return { current: booking.toJSON(), previous: oldBooking }
|
||||
}
|
||||
|
||||
export async function createBill(
|
||||
bookingUUID: string,
|
||||
billData: IBill
|
||||
): Promise<IBill> {
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
|
||||
const bill = new BillModel()
|
||||
bill.set(billData)
|
||||
|
||||
await bill.save()
|
||||
|
||||
booking.bill = bill
|
||||
await booking.save()
|
||||
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function patchBill(
|
||||
bookingUUID: string,
|
||||
billData: IBill
|
||||
): Promise<IBill> {
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const bill =
|
||||
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
||||
(await BillModel.create({}))
|
||||
|
||||
bill.set(billData)
|
||||
await bill.save()
|
||||
|
||||
if (booking.bill !== bill) {
|
||||
booking.bill = bill
|
||||
await booking.save()
|
||||
}
|
||||
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function getMilageMax(): Promise<number> {
|
||||
const billMaxMilageEnd = await BillModel.findOne({})
|
||||
.sort('-milageEnd')
|
||||
.select('milageEnd')
|
||||
.exec()
|
||||
|
||||
return billMaxMilageEnd?.milageEnd || 0
|
||||
}
|
||||
Reference in New Issue
Block a user