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()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createEvents, EventStatus } from 'ics'
|
||||
import { BookingDocument } from '../db/booking'
|
||||
import { Booking } from '../db/booking'
|
||||
import { BOOKING_STATUS } from '../db/enums'
|
||||
import { getBaseURL } from './url'
|
||||
|
||||
@@ -11,7 +11,7 @@ function convertDay(value: string): [number, number, number] {
|
||||
return [Number(parts[0]), Number(parts[1]), Number(parts[2])]
|
||||
}
|
||||
|
||||
export function generateBookedCalendar(bookings: BookingDocument[]) {
|
||||
export function generateBookedCalendar(bookings: Booking[]) {
|
||||
const events = bookings.map((booking) => ({
|
||||
calName: 'Pfadi-Bussle Buchungen',
|
||||
start: convertDay(booking.days[0]),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BookingDocument } from '../db/booking'
|
||||
import { Booking } from '../db/booking'
|
||||
import { getBaseURL } from '../helpers/url'
|
||||
|
||||
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
|
||||
@@ -28,7 +28,7 @@ Pfadi Bussle Wart
|
||||
Tel. 0151/212 253 62
|
||||
`
|
||||
|
||||
function getReceivedBookingBookerText(booking: BookingDocument) {
|
||||
function getReceivedBookingBookerText(booking: Booking) {
|
||||
return `Hallo liebe/r ${booking.booker.name},
|
||||
|
||||
Vielen Dank für Deine Buchung. Nach Prüfung bestätigen wir die Buchung bald per E-Mail!
|
||||
@@ -43,7 +43,7 @@ ${footer}
|
||||
`
|
||||
}
|
||||
|
||||
function getBookingConfirmedText(booking: BookingDocument) {
|
||||
function getBookingConfirmedText(booking: Booking) {
|
||||
return `Hallo liebe/r ${booking.booker.name},
|
||||
|
||||
deine Buchung ist bestätigt!
|
||||
@@ -57,7 +57,7 @@ einsehen und stornieren.
|
||||
${footer}
|
||||
`
|
||||
}
|
||||
function getBookingRejectedText(booking: BookingDocument) {
|
||||
function getBookingRejectedText(booking: Booking) {
|
||||
return `Hallo liebe/r ${booking.booker.name},
|
||||
|
||||
es tut uns leid aber deine Buchung konnte leider nicht bestätigt werden.
|
||||
@@ -77,7 +77,7 @@ es ging folgende Buchung ein: ${getBaseURL()}/admin/booking/${booking.uuid}
|
||||
MfG`
|
||||
}
|
||||
|
||||
export async function sendReceivedBookingAdminMail(booking: BookingDocument) {
|
||||
export async function sendReceivedBookingAdminMail(booking: Booking) {
|
||||
try {
|
||||
await sendMail({
|
||||
to: [{ email: ADMIN_EMAIL }],
|
||||
@@ -90,7 +90,7 @@ export async function sendReceivedBookingAdminMail(booking: BookingDocument) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendReceivedBookingBookerMail(booking: BookingDocument) {
|
||||
export async function sendReceivedBookingBookerMail(booking: Booking) {
|
||||
try {
|
||||
await sendMail({
|
||||
to: [{ email: booking.booker.email, name: booking.booker.name }],
|
||||
@@ -103,7 +103,7 @@ export async function sendReceivedBookingBookerMail(booking: BookingDocument) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendBookingConfirmed(booking: BookingDocument) {
|
||||
export async function sendBookingConfirmed(booking: Booking) {
|
||||
try {
|
||||
await sendMail({
|
||||
to: [{ email: booking.booker.email, name: booking.booker.name }],
|
||||
@@ -116,7 +116,7 @@ export async function sendBookingConfirmed(booking: BookingDocument) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendBookingRejected(booking: BookingDocument) {
|
||||
export async function sendBookingRejected(booking: Booking) {
|
||||
try {
|
||||
await sendMail({
|
||||
to: [{ email: booking.booker.email, name: booking.booker.name }],
|
||||
|
||||
@@ -3,8 +3,8 @@ import Footer from '../../../../components/footer'
|
||||
import Header from '../../../../components/header'
|
||||
import Input from '../../../../components/input'
|
||||
import Select from '../../../../components/select'
|
||||
import { AdditionalCost, BillDocument } from '../../../../db/bill'
|
||||
import { BookingDocument } from '../../../../db/booking'
|
||||
import { AdditionalCost, Bill } from '../../../../db/bill'
|
||||
import { Booking } from '../../../../db/booking'
|
||||
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
|
||||
import { getMilageMax } from '../../../../db/index'
|
||||
import { dateFormatFrontend } from '../../../../helpers/date'
|
||||
@@ -80,7 +80,7 @@ function getBillStatusLabel(status: BILL_STATUS) {
|
||||
}
|
||||
|
||||
async function saveBill(
|
||||
booking: BookingDocument,
|
||||
booking: Booking,
|
||||
bill: {
|
||||
milageStart: number
|
||||
milageEnd: number
|
||||
@@ -89,9 +89,9 @@ async function saveBill(
|
||||
additionalCosts: AdditionalCost[]
|
||||
status: BILL_STATUS
|
||||
}
|
||||
): Promise<BillDocument> {
|
||||
): Promise<Bill> {
|
||||
const response = await fetch(`/api/admin/booking/${booking.uuid}/bill`, {
|
||||
method: booking.bill?._id ? 'PATCH' : 'POST',
|
||||
method: !!booking.bill ? 'PATCH' : 'POST',
|
||||
mode: 'cors',
|
||||
cache: 'no-cache',
|
||||
credentials: 'same-origin',
|
||||
@@ -108,7 +108,7 @@ export default function BookingBillPage({
|
||||
booking: bookingProp,
|
||||
milageMax,
|
||||
}: {
|
||||
booking: BookingDocument
|
||||
booking: Booking
|
||||
milageMax: number
|
||||
}) {
|
||||
const [booking, setBooking] = useState(bookingProp)
|
||||
@@ -300,7 +300,7 @@ export default function BookingBillPage({
|
||||
className="btn btn-blue mt-3"
|
||||
disabled={storingInProgress}
|
||||
>
|
||||
Rechnung {booking.bill?._id ? 'Updaten' : 'Erstellen'}
|
||||
Rechnung {!!booking.bill ? 'Updaten' : 'Erstellen'}
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
@@ -8,7 +8,7 @@ import withSession, {
|
||||
redirectToLogin,
|
||||
} from '../../../../lib/session'
|
||||
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||
import { BookingDocument } from '../../../../db/booking'
|
||||
import { Booking } from '../../../../db/booking'
|
||||
import { getBookingStatus } from '../../../../helpers/booking'
|
||||
import { dateFormatFrontend } from '../../../../helpers/date'
|
||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||
@@ -52,7 +52,7 @@ async function patchBooking(uuid: string, bookingData: any) {
|
||||
export default function ShowBookingAdmin({
|
||||
booking: bookingProp,
|
||||
}: {
|
||||
booking: BookingDocument
|
||||
booking: Booking
|
||||
}) {
|
||||
const [booking, setBooking] = useState(bookingProp)
|
||||
const [storingBooking, setStoringBooking] = useState(false)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { BillDocument } from '../../../../../db/bill'
|
||||
import { Bill } from '../../../../../db/bill'
|
||||
import { createBill, patchBill } from '../../../../../db/index'
|
||||
import withSession, { isAdminSession } from '../../../../../lib/session'
|
||||
|
||||
export default withSession(async function billHandler(req, res) {
|
||||
if (!isAdminSession(req, res)) {
|
||||
if (!isAdminSession(req)) {
|
||||
res.status(403).send({ message: 'Not Authorized' })
|
||||
return
|
||||
}
|
||||
@@ -14,7 +14,7 @@ export default withSession(async function billHandler(req, res) {
|
||||
} = req
|
||||
const bookingUUID = Array.isArray(uuids) ? uuids[0] : uuids
|
||||
|
||||
let bill: BillDocument
|
||||
let bill: Bill
|
||||
|
||||
switch (method) {
|
||||
case 'POST':
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import { BOOKING_STATUS } from '../../../../../db/enums'
|
||||
|
||||
export default withSession(async function bookingHandler(req, res) {
|
||||
if (!isAdminSession(req, res)) {
|
||||
if (!isAdminSession(req)) {
|
||||
res.status(403).send({ message: 'Not Authorized' })
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Error } from 'mongoose'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { BookingDocument } from '../../../db/booking'
|
||||
import { Booking } from '../../../db/booking'
|
||||
import { createBooking } from '../../../db/index'
|
||||
import { sendReceivedBookingAdminMail, sendReceivedBookingBookerMail } from '../../../helpers/mail'
|
||||
import {
|
||||
sendReceivedBookingAdminMail,
|
||||
sendReceivedBookingBookerMail,
|
||||
} from '../../../helpers/mail'
|
||||
|
||||
export default async function userHandler(
|
||||
req: NextApiRequest,
|
||||
@@ -10,7 +13,7 @@ export default async function userHandler(
|
||||
) {
|
||||
const { method } = req
|
||||
|
||||
let booking: BookingDocument
|
||||
let booking: Booking
|
||||
|
||||
switch (method) {
|
||||
case 'POST':
|
||||
|
||||
@@ -2,14 +2,14 @@ import React, { useEffect, useState } from 'react'
|
||||
import Footer from '../../../components/footer'
|
||||
import Header from '../../../components/header'
|
||||
import { getServerSideBooking } from '../../../lib/getServerSideProps'
|
||||
import { BookingDocument } from '../../../db/booking'
|
||||
import { Booking } from '../../../db/booking'
|
||||
import { BOOKING_STATUS } from '../../../db/enums'
|
||||
import { dateFormatFrontend } from '../../../helpers/date'
|
||||
import { getBookingStatus } from '../../../helpers/booking'
|
||||
|
||||
export const getServerSideProps = getServerSideBooking
|
||||
|
||||
async function cancelBooking(booking: BookingDocument) {
|
||||
async function cancelBooking(booking: Booking) {
|
||||
const response = await fetch(`/api/booking/${booking.uuid}`, {
|
||||
method: 'PATCH',
|
||||
mode: 'cors',
|
||||
@@ -27,7 +27,7 @@ async function cancelBooking(booking: BookingDocument) {
|
||||
export default function ShowBooking({
|
||||
booking: bookingProp,
|
||||
}: {
|
||||
booking: BookingDocument
|
||||
booking: Booking
|
||||
}) {
|
||||
const [booking, setBooking] = useState(bookingProp)
|
||||
const [storingBooking, setStoringBooking] = useState(false)
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import Footer from '../../../components/footer'
|
||||
import Header from '../../../components/header'
|
||||
import { BookingDocument } from '../../../db/booking'
|
||||
import { Booking } from '../../../db/booking'
|
||||
import { loadBookingData, storeBookingData } from '../../../helpers/storage'
|
||||
import { getServerSideBooking } from '../../../lib/getServerSideProps'
|
||||
|
||||
@@ -11,7 +11,7 @@ export const getServerSideProps = getServerSideBooking
|
||||
export default function ShowBookingStored({
|
||||
booking: booking,
|
||||
}: {
|
||||
booking: BookingDocument
|
||||
booking: Booking
|
||||
}) {
|
||||
const [storedBookingData, setStoredBookingData] = useState(null)
|
||||
const [bookingDataStored, setBookingDataStored] = useState(false)
|
||||
|
||||
Reference in New Issue
Block a user