mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
remove *Document intefaces from FE code
This commit is contained in:
@@ -7,9 +7,7 @@ export interface AdditionalCost {
|
||||
value: number
|
||||
}
|
||||
|
||||
export interface BillDocument
|
||||
extends mongoose.SchemaTimestampsConfig,
|
||||
mongoose.Document {
|
||||
export interface Bill {
|
||||
milageStart: number
|
||||
milageEnd: number
|
||||
milage?: number
|
||||
@@ -18,6 +16,11 @@ export interface BillDocument
|
||||
additionalCosts: AdditionalCost[]
|
||||
}
|
||||
|
||||
export interface BillDocument
|
||||
extends Bill,
|
||||
mongoose.SchemaTimestampsConfig,
|
||||
mongoose.Document {}
|
||||
|
||||
export interface BillModel extends mongoose.Model<BillDocument> {}
|
||||
|
||||
const BillSchema = new mongoose.Schema<BillDocument>(
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
|
||||
export interface BookerDocument
|
||||
extends mongoose.SchemaTimestampsConfig,
|
||||
mongoose.Document {
|
||||
export interface Booker {
|
||||
name: string
|
||||
email: string
|
||||
street: string
|
||||
@@ -10,6 +8,11 @@ export interface BookerDocument
|
||||
city: string
|
||||
}
|
||||
|
||||
export interface BookerDocument
|
||||
extends Booker,
|
||||
mongoose.SchemaTimestampsConfig,
|
||||
mongoose.Document {}
|
||||
|
||||
export interface BookerModel extends mongoose.Model<BookerDocument> {}
|
||||
|
||||
const BookerSchema = new mongoose.Schema<BookerDocument>(
|
||||
|
||||
@@ -2,16 +2,14 @@ import * as mongoose from 'mongoose'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { startOfDay } from 'date-fns'
|
||||
import { dateFormatBackend, getDays } from '../helpers/date'
|
||||
import { BillDocument } from './bill'
|
||||
import { BookerDocument } from './booker'
|
||||
import { Bill } from './bill'
|
||||
import { Booker } from './booker'
|
||||
import { BOOKING_STATUS } from './enums'
|
||||
|
||||
export interface BookingDocument
|
||||
extends mongoose.Document,
|
||||
mongoose.SchemaTimestampsConfig {
|
||||
export interface Booking {
|
||||
uuid: string
|
||||
booker: BookerDocument
|
||||
bill: BillDocument
|
||||
booker: Booker
|
||||
bill: Bill
|
||||
startDate: Date
|
||||
endDate: Date
|
||||
status: BOOKING_STATUS
|
||||
@@ -21,6 +19,11 @@ export interface BookingDocument
|
||||
days?: string[]
|
||||
}
|
||||
|
||||
export interface BookingDocument
|
||||
extends Booking,
|
||||
mongoose.Document,
|
||||
mongoose.SchemaTimestampsConfig {}
|
||||
|
||||
export interface BookingModel extends mongoose.Model<BookingDocument> {
|
||||
findBookedDays(): Promise<string[]>
|
||||
}
|
||||
|
||||
44
db/index.ts
44
db/index.ts
@@ -1,7 +1,7 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
import Booker from './booker'
|
||||
import Booking from './booking'
|
||||
import Bill, { BillDocument } from './bill'
|
||||
import BookerModel, { Booker } from './booker'
|
||||
import BookingModel, { Booking } from './booking'
|
||||
import BillModel, { Bill } from './bill'
|
||||
import { dateFormatFrontend } from '../helpers/date'
|
||||
import { BOOKING_STATUS } from './enums'
|
||||
|
||||
@@ -23,17 +23,17 @@ function connect() {
|
||||
|
||||
export async function getBookedDays() {
|
||||
await connect()
|
||||
return Booking.findBookedDays()
|
||||
return BookingModel.findBookedDays()
|
||||
}
|
||||
|
||||
export async function getBookingByUUID(uuid: string) {
|
||||
await connect()
|
||||
return Booking.findOne({ uuid })
|
||||
return BookingModel.findOne({ uuid })
|
||||
}
|
||||
|
||||
export async function getBookings() {
|
||||
await connect()
|
||||
return await Booking.find({
|
||||
return await BookingModel.find({
|
||||
status: { $in: [BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED] },
|
||||
})
|
||||
.populate('booker')
|
||||
@@ -51,9 +51,15 @@ export async function createBooking({
|
||||
street,
|
||||
zip,
|
||||
city,
|
||||
}) {
|
||||
}: Booking & Booker): Promise<Booking> {
|
||||
await connect()
|
||||
const booking = new Booking({ startDate, endDate, purpose, org, destination })
|
||||
const booking = new BookingModel({
|
||||
startDate,
|
||||
endDate,
|
||||
purpose,
|
||||
org,
|
||||
destination,
|
||||
})
|
||||
const bookedDays = await getBookedDays()
|
||||
|
||||
const doubleBookedDays = booking.days.filter((day: string) =>
|
||||
@@ -72,9 +78,9 @@ export async function createBooking({
|
||||
throw error
|
||||
}
|
||||
|
||||
let booker = await Booker.findOne({ email }).exec()
|
||||
let booker = await BookerModel.findOne({ email }).exec()
|
||||
if (!booker) {
|
||||
booker = new Booker({ name, email, street, zip, city })
|
||||
booker = new BookerModel({ name, email, street, zip, city })
|
||||
await booker.save()
|
||||
}
|
||||
|
||||
@@ -84,11 +90,14 @@ export async function createBooking({
|
||||
return booking.toJSON()
|
||||
}
|
||||
|
||||
export async function createBill(bookingUUID: string, billData: BillDocument) {
|
||||
export async function createBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
await connect()
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
|
||||
const bill = new Bill()
|
||||
const bill = new BillModel()
|
||||
bill.set(billData)
|
||||
|
||||
await bill.save()
|
||||
@@ -99,12 +108,15 @@ export async function createBill(bookingUUID: string, billData: BillDocument) {
|
||||
return bill.toJSON()
|
||||
}
|
||||
|
||||
export async function patchBill(bookingUUID: string, billData: BillDocument) {
|
||||
export async function patchBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
await connect()
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const bill =
|
||||
(booking.bill && (await Bill.findById(booking.bill))) ||
|
||||
(await Bill.create())
|
||||
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
||||
(await BillModel.create())
|
||||
|
||||
bill.set(billData)
|
||||
await bill.save()
|
||||
@@ -118,7 +130,7 @@ export async function patchBill(bookingUUID: string, billData: BillDocument) {
|
||||
}
|
||||
|
||||
export async function getMilageMax(): Promise<number> {
|
||||
const billMaxMilageEnd = await Bill.findOne({})
|
||||
const billMaxMilageEnd = await BillModel.findOne({})
|
||||
.sort('-milageEnd')
|
||||
.select('milageEnd')
|
||||
.exec()
|
||||
|
||||
Reference in New Issue
Block a user