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 { BOOKING_STATUS } from './enums'
|
||||||
import { uniqueFilter } from '../helpers/array'
|
import { uniqueFilter } from '../helpers/array'
|
||||||
|
|
||||||
let connectedPromise: Promise<mongoose.Mongoose>
|
|
||||||
|
|
||||||
export const MONGO_URI = process.env.MONGO_URI
|
export const MONGO_URI = process.env.MONGO_URI
|
||||||
|
|
||||||
export function connect(): Promise<mongoose.Mongoose> {
|
mongoose.connect(process.env.MONGO_URI, {
|
||||||
if (connectedPromise) {
|
serverSelectionTimeoutMS: 3000,
|
||||||
return connectedPromise
|
})
|
||||||
}
|
|
||||||
|
|
||||||
connectedPromise = mongoose.connect(process.env.MONGO_URI)
|
|
||||||
|
|
||||||
return connectedPromise
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getBookedDays(
|
export async function getBookedDays(
|
||||||
uuidsToIngore?: string[]
|
uuidsToIngore?: string[]
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
await connect()
|
|
||||||
const bookedInDatabase = await BookingModel.findBookedDays(uuidsToIngore)
|
const bookedInDatabase = await BookingModel.findBookedDays(uuidsToIngore)
|
||||||
const bookedInCalendar = await calendarGetBookedDays()
|
const bookedInCalendar = await calendarGetBookedDays()
|
||||||
|
|
||||||
@@ -30,7 +21,6 @@ export async function getBookedDays(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
|
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
|
||||||
await connect()
|
|
||||||
return BookingModel.findOne({ uuid })
|
return BookingModel.findOne({ uuid })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +30,6 @@ export async function getBookings({
|
|||||||
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
|
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<
|
||||||
BookingDocument[]
|
BookingDocument[]
|
||||||
> {
|
> {
|
||||||
await connect()
|
|
||||||
return await BookingModel.find({
|
return await BookingModel.find({
|
||||||
status: { $in: status },
|
status: { $in: status },
|
||||||
startDate: { $gte: startDateGreaterThan },
|
startDate: { $gte: startDateGreaterThan },
|
||||||
@@ -62,7 +51,6 @@ export async function createBooking({
|
|||||||
zip,
|
zip,
|
||||||
city,
|
city,
|
||||||
}: Booking): Promise<Booking> {
|
}: Booking): Promise<Booking> {
|
||||||
await connect()
|
|
||||||
const booking = new BookingModel({
|
const booking = new BookingModel({
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -85,7 +73,6 @@ export async function patchBooking(
|
|||||||
bookingUUID: string,
|
bookingUUID: string,
|
||||||
bookingData: Booking
|
bookingData: Booking
|
||||||
): Promise<{ current: Booking; previous: Booking }> {
|
): Promise<{ current: Booking; previous: Booking }> {
|
||||||
await connect()
|
|
||||||
const booking = await getBookingByUUID(bookingUUID)
|
const booking = await getBookingByUUID(bookingUUID)
|
||||||
const oldBooking = booking.toJSON<Booking>()
|
const oldBooking = booking.toJSON<Booking>()
|
||||||
booking.set(bookingData)
|
booking.set(bookingData)
|
||||||
@@ -98,7 +85,6 @@ export async function createBill(
|
|||||||
bookingUUID: string,
|
bookingUUID: string,
|
||||||
billData: Bill
|
billData: Bill
|
||||||
): Promise<Bill> {
|
): Promise<Bill> {
|
||||||
await connect()
|
|
||||||
const booking = await getBookingByUUID(bookingUUID)
|
const booking = await getBookingByUUID(bookingUUID)
|
||||||
|
|
||||||
const bill = new BillModel()
|
const bill = new BillModel()
|
||||||
@@ -116,7 +102,6 @@ export async function patchBill(
|
|||||||
bookingUUID: string,
|
bookingUUID: string,
|
||||||
billData: Bill
|
billData: Bill
|
||||||
): Promise<Bill> {
|
): Promise<Bill> {
|
||||||
await connect()
|
|
||||||
const booking = await getBookingByUUID(bookingUUID)
|
const booking = await getBookingByUUID(bookingUUID)
|
||||||
const bill =
|
const bill =
|
||||||
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
(booking.bill && (await BillModel.findById(booking.bill))) ||
|
||||||
|
|||||||
Reference in New Issue
Block a user