mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
create mongo conection of import
This commit is contained in:
21
db/index.ts
21
db/index.ts
@@ -5,24 +5,15 @@ import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
||||
import { BOOKING_STATUS } from './enums'
|
||||
import { uniqueFilter } from '../helpers/array'
|
||||
|
||||
let connectedPromise: Promise<mongoose.Mongoose>
|
||||
|
||||
export const MONGO_URI = process.env.MONGO_URI
|
||||
|
||||
export function connect(): Promise<mongoose.Mongoose> {
|
||||
if (connectedPromise) {
|
||||
return connectedPromise
|
||||
}
|
||||
|
||||
connectedPromise = mongoose.connect(process.env.MONGO_URI)
|
||||
|
||||
return connectedPromise
|
||||
}
|
||||
mongoose.connect(process.env.MONGO_URI, {
|
||||
serverSelectionTimeoutMS: 3000,
|
||||
})
|
||||
|
||||
export async function getBookedDays(
|
||||
uuidsToIngore?: string[]
|
||||
): Promise<string[]> {
|
||||
await connect()
|
||||
const bookedInDatabase = await BookingModel.findBookedDays(uuidsToIngore)
|
||||
const bookedInCalendar = await calendarGetBookedDays()
|
||||
|
||||
@@ -30,7 +21,6 @@ export async function getBookedDays(
|
||||
}
|
||||
|
||||
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
|
||||
await connect()
|
||||
return BookingModel.findOne({ uuid })
|
||||
}
|
||||
|
||||
@@ -40,7 +30,6 @@ export async function getBookings({
|
||||
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
|
||||
BookingDocument[]
|
||||
> {
|
||||
await connect()
|
||||
return await BookingModel.find({
|
||||
status: { $in: status },
|
||||
startDate: { $gte: startDateGreaterThan },
|
||||
@@ -62,7 +51,6 @@ export async function createBooking({
|
||||
zip,
|
||||
city,
|
||||
}: Booking): Promise<Booking> {
|
||||
await connect()
|
||||
const booking = new BookingModel({
|
||||
startDate,
|
||||
endDate,
|
||||
@@ -85,7 +73,6 @@ export async function patchBooking(
|
||||
bookingUUID: string,
|
||||
bookingData: Booking
|
||||
): Promise<{ current: Booking; previous: Booking }> {
|
||||
await connect()
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const oldBooking = booking.toJSON<Booking>()
|
||||
booking.set(bookingData)
|
||||
@@ -98,7 +85,6 @@ export async function createBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
await connect()
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
|
||||
const bill = new BillModel()
|
||||
@@ -116,7 +102,6 @@ export async function patchBill(
|
||||
bookingUUID: string,
|
||||
billData: Bill
|
||||
): Promise<Bill> {
|
||||
await connect()
|
||||
const booking = await getBookingByUUID(bookingUUID)
|
||||
const bill =
|
||||
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
||||
|
||||
Reference in New Issue
Block a user