diff --git a/context/book.tsx b/context/book.tsx index 7e3305f..67cf92a 100644 --- a/context/book.tsx +++ b/context/book.tsx @@ -3,13 +3,11 @@ import { useRouter } from 'next/router' import { clearBookingData, loadBookingData } from '../helpers/storage' import { createBooking } from '../helpers/booking' -import { Booker } from '../db/booker' import { Booking } from '../db/booking' -export type BookFormData = Omit & - Booker & { - storeData?: boolean - } +export type BookFormData = Omit & { + storeData?: boolean +} type BookingProviderState = { postData?: boolean diff --git a/db/booker.ts b/db/booker.ts deleted file mode 100644 index d03006c..0000000 --- a/db/booker.ts +++ /dev/null @@ -1,31 +0,0 @@ -import * as mongoose from 'mongoose' - -export type Booker = { - name: string - email: string - phone: string - street: string - zip: string - city: string -} - -export type BookerDocument = Booker & - mongoose.SchemaTimestampsConfig & - mongoose.Document - -export type BookerModel = mongoose.Model - -const BookerSchema = new mongoose.Schema( - { - name: { type: String, required: true }, - email: { type: String, required: true, unique: true, minlength: 5 }, - phone: { type: String, required: false }, - street: { type: String, required: true }, - zip: { type: String, required: true }, - city: { type: String, required: true }, - }, - { timestamps: true, collation: { locale: 'de', strength: 1 } } -) - -export default mongoose.models.Booker || - mongoose.model('Booker', BookerSchema) diff --git a/db/booking.ts b/db/booking.ts index 18f3dc5..31fb893 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -3,13 +3,17 @@ import { v4 as uuidv4 } from 'uuid' import { dateFormatBackend, getDays, nowInTz } from '../helpers/date' import { Bill } from './bill' -import { Booker } from './booker' import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums' import { getBookedDays } from './index' export type Booking = { uuid: string - booker?: Booker + name: string + email: string + phone: string + street: string + zip: string + city: string bill?: Bill startDate: string endDate: string @@ -36,11 +40,12 @@ const BookingSchema = new mongoose.Schema( default: uuidv4, index: true, }, - booker: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Booker', - required: true, - }, + name: { type: String, required: true }, + email: { type: String, required: true, minlength: 5 }, + phone: { type: String, required: false }, + street: { type: String, required: true }, + zip: { type: String, required: true }, + city: { type: String, required: true }, bill: { type: mongoose.Schema.Types.ObjectId, ref: 'Bill', diff --git a/db/index.ts b/db/index.ts index 78d2e12..05ce2cd 100644 --- a/db/index.ts +++ b/db/index.ts @@ -1,5 +1,4 @@ import * as mongoose from 'mongoose' -import BookerModel, { Booker } from './booker' import BookingModel, { Booking, BookingDocument } from './booking' import BillModel, { Bill } from './bill' import { BOOKING_STATUS } from './enums' @@ -20,7 +19,9 @@ function connect(): Promise { return connectedPromise } -export async function getBookedDays(uuidsToIngore?: string[]): Promise { +export async function getBookedDays( + uuidsToIngore?: string[] +): Promise { await connect() return BookingModel.findBookedDays(uuidsToIngore) } @@ -33,14 +34,14 @@ export async function getBookingByUUID(uuid: string): Promise { export async function getBookings({ status = [BOOKING_STATUS.CONFIRMED, BOOKING_STATUS.REQUESTED], startDateGreaterThan = '2000-01-01T00:00:00Z', -}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise { +}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise< + BookingDocument[] +> { await connect() return await BookingModel.find({ status: { $in: status }, startDate: { $gte: startDateGreaterThan }, - }) - .populate('booker') - .exec() + }).exec() } export async function createBooking({ @@ -55,7 +56,7 @@ export async function createBooking({ street, zip, city, -}: Booking & Booker): Promise { +}: Booking): Promise { await connect() const booking = new BookingModel({ startDate, @@ -63,17 +64,15 @@ export async function createBooking({ purpose, org, destination, + name, + email, + phone, + street, + zip, + city, }) - let booker = await BookerModel.findOne({ email }).exec() - if (!booker) { - booker = new BookerModel({ name, email, phone, street, zip, city }) - await booker.save() - } - - booking.booker = booker._id await booking.save() - await booking.populate('booker').execPopulate() return booking.toJSON() } diff --git a/helpers/ical.ts b/helpers/ical.ts index 04a0c9c..0c40951 100644 --- a/helpers/ical.ts +++ b/helpers/ical.ts @@ -21,7 +21,7 @@ export function generateCalendarEntry(booking: Booking): string { duration: { days: booking.days.length }, location: 'Mömpelgardgasse 25, 72348 Rosenfeld, Deutschland', geo: { lat: 48.287044, lon: 8.726361 }, - description: `Gebucht auf ${booking.booker.name} + description: `Gebucht auf ${booking.name} Buchungs-Link: ${getBaseURL()}/booking/${booking.uuid} `, @@ -39,18 +39,27 @@ Buchungs-Link: ${getBaseURL()}/booking/${booking.uuid} } export function generateBookedCalendar(bookings: Booking[]): string { - const events = bookings.map((booking): { productId: string; calName: string; start: [number, number, number]; startOutputType: 'local' | 'utc'; duration: { days: number }; title: string; description: string; status: EventStatus } => ({ + const events = bookings.map((booking): { + productId: string + calName: string + start: [number, number, number] + startOutputType: 'local' | 'utc' + duration: { days: number } + title: string + description: string + status: EventStatus + } => ({ productId: 'app.vercel.pfadi-bussle/ics', calName: 'Pfadi-Bussle Buchungen', start: convertDay(booking.days[0]), startOutputType: 'local', duration: { days: booking.days.length }, - title: `Buchung ${booking.booker.name}`, - description: `Name: ${booking.booker.name} + title: `Buchung ${booking.name}`, + description: `Name: ${booking.name} Zeitraum: ${daysFormatFrontend(booking.days)} -Email: ${booking.booker.email} -Telefon: ${booking.booker.phone} +Email: ${booking.email} +Telefon: ${booking.phone} Link: ${getBaseURL()}/admin/booking/${booking.uuid} `, diff --git a/helpers/mail.ts b/helpers/mail.ts index e0b0601..0c7cbd7 100644 --- a/helpers/mail.ts +++ b/helpers/mail.ts @@ -32,7 +32,7 @@ Tel. 0151/212 253 62 ` function getReceivedBookingBookerText(booking: Booking): string { - return `Hallo liebe/r ${booking.booker.name}, + return `Hallo liebe/r ${booking.name}, Vielen Dank für Deine Buchungsanfrage zum ${daysFormatFrontend(booking.days)}! @@ -49,7 +49,7 @@ ${footer} } function getBookingConfirmedText(booking: Booking): string { - return `Hallo liebe/r ${booking.booker.name}, + return `Hallo liebe/r ${booking.name}, deine Buchunganfrage zum ${daysFormatFrontend( booking.days @@ -65,7 +65,7 @@ ${footer} ` } function getBookingRejectedText(booking: Booking): string { - return `Hallo liebe/r ${booking.booker.name}, + return `Hallo liebe/r ${booking.name}, es tut uns leid aber deine Buchungsanfrage zum ${daysFormatFrontend( booking.days @@ -86,7 +86,9 @@ es ging folgende Buchung ein: ${getBaseURL()}/admin/booking/${booking.uuid} MfG` } -export async function sendReceivedBookingAdminMail(booking: Booking): Promise { +export async function sendReceivedBookingAdminMail( + booking: Booking +): Promise { try { await sendMail({ to: [{ email: ADMIN_EMAIL }], @@ -102,10 +104,12 @@ export async function sendReceivedBookingAdminMail(booking: Booking): Promise { +export async function sendReceivedBookingBookerMail( + booking: Booking +): Promise { try { await sendMail({ - to: [{ email: booking.booker.email, name: booking.booker.name }], + to: [{ email: booking.email, name: booking.name }], from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' }, subject: `Deine Pfadi-Bussle Buchung ist eingegangen!`, textPlainContent: getReceivedBookingBookerText(booking), @@ -121,7 +125,7 @@ export async function sendReceivedBookingBookerMail(booking: Booking): Promise { try { await sendMail({ - to: [{ email: booking.booker.email, name: booking.booker.name }], + to: [{ email: booking.email, name: booking.name }], from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' }, subject: `Deine Pfadi-Bussle Buchung wurde bestätigt!`, textPlainContent: getBookingConfirmedText(booking), @@ -146,7 +150,7 @@ export async function sendBookingConfirmed(booking: Booking): Promise { export async function sendBookingRejected(booking: Booking): Promise { try { await sendMail({ - to: [{ email: booking.booker.email, name: booking.booker.name }], + to: [{ email: booking.email, name: booking.name }], from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' }, subject: `Deine Pfadi-Bussle Buchung wurde abgelehnt!`, textPlainContent: getBookingRejectedText(booking), diff --git a/helpers/storage.ts b/helpers/storage.ts index d8df461..52dc19a 100644 --- a/helpers/storage.ts +++ b/helpers/storage.ts @@ -7,8 +7,7 @@ function getStorage() { } export function storeBookingData(booking: Booking) { - const { name, email, street, zip, city } = booking.booker - const { org } = booking + const { name, email, street, zip, city, org } = booking getStorage().setItem( BOOKING_DATA_KEY, diff --git a/lib/getServerSideProps.ts b/lib/getServerSideProps.ts index 55fc2eb..d96a21a 100644 --- a/lib/getServerSideProps.ts +++ b/lib/getServerSideProps.ts @@ -48,7 +48,6 @@ export const getServerSideBooking = async ( return { props: { booking: null } } } - await booking.populate('booker').execPopulate() await booking.populate('bill').execPopulate() // TODO: hack, not sure why _id is not serilizable diff --git a/pages/admin/booking/[uuid]/bill.tsx b/pages/admin/booking/[uuid]/bill.tsx index 330f4f2..50c93a3 100644 --- a/pages/admin/booking/[uuid]/bill.tsx +++ b/pages/admin/booking/[uuid]/bill.tsx @@ -158,7 +158,7 @@ export default function BookingBillPage({ {daysFormatFrontend(booking.days)}
- Bucher: {booking.booker.name} + Bucher: {booking.name}
Buchungsstatus:{' '} diff --git a/pages/admin/booking/[uuid]/index.tsx b/pages/admin/booking/[uuid]/index.tsx index afe9772..834da28 100644 --- a/pages/admin/booking/[uuid]/index.tsx +++ b/pages/admin/booking/[uuid]/index.tsx @@ -74,7 +74,7 @@ export default function ShowBookingAdmin({ Buchungszeitraum: {daysFormatFrontend(booking.days)}
- Bucher: {booking.booker.name} + Bucher: {booking.name}
Buchungsstatus: {getBookingStatus(booking.status)} diff --git a/pages/admin/index.tsx b/pages/admin/index.tsx index 5477450..51ff8f2 100644 --- a/pages/admin/index.tsx +++ b/pages/admin/index.tsx @@ -59,13 +59,13 @@ export default function AdminRecentBookings({ bookings }) {
Bucher
- {booking.booker.name} + {booking.name}
Email
- {booking.booker.email} + {booking.email}
diff --git a/pages/api/booking/[uuid]/index.ts b/pages/api/booking/[uuid]/index.ts index 9e25de0..4f5d4a9 100644 --- a/pages/api/booking/[uuid]/index.ts +++ b/pages/api/booking/[uuid]/index.ts @@ -51,7 +51,6 @@ export default withSession(async function userHandler( booking.set(req.body) try { await booking.save() - await booking.populate('booker').execPopulate() res.status(200).json(booking.toJSON()) } catch (error) { res.status(400).end(`Failed to save booking: ${error.message}`) diff --git a/pages/api/booking/index.ts b/pages/api/booking/index.ts index 4d9ff83..af58adf 100644 --- a/pages/api/booking/index.ts +++ b/pages/api/booking/index.ts @@ -30,15 +30,11 @@ export default async function userHandler( return } - console.log( - `received booking ${booking.uuid} from {booking.booker.email}` - ) + console.log(`received booking ${booking.uuid} from {booking.email}`) await sendReceivedBookingAdminMail(booking) console.log(`send booking ${booking.uuid} received to admin`) await sendReceivedBookingBookerMail(booking) - console.log( - `send booking ${booking.uuid} received to {booking.booker.email}` - ) + console.log(`send booking ${booking.uuid} received to {booking.email}`) break default: res.setHeader('Allow', ['POST'])