mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-02 22:17:11 +01:00
94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
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 |