mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
134 lines
2.8 KiB
TypeScript
134 lines
2.8 KiB
TypeScript
import { BookingStatus, Booking, PrismaClient, Prisma } from '@prisma/client'
|
|
|
|
import { getBookedDays as calendarGetBookedDays } from '../lib/googlecalendar'
|
|
import { uniqueFilter } from '../helpers/array'
|
|
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
|
|
|
|
declare global {
|
|
// allow global `var` declarations
|
|
// eslint-disable-next-line no-var
|
|
var prisma: PrismaClient | undefined
|
|
}
|
|
|
|
export const prisma =
|
|
global.prisma ||
|
|
new PrismaClient({
|
|
log: ['query'],
|
|
})
|
|
|
|
if (process.env.NODE_ENV !== 'production') global.prisma = prisma
|
|
|
|
export async function getBookedDays(
|
|
uuidsToIngore?: string[]
|
|
) {
|
|
const [bookingsInDbRaw, bookingsInCalendar] = await Promise.all([
|
|
prisma.booking.findMany({
|
|
where: {
|
|
uuid: { notIn: uuidsToIngore },
|
|
startDate: { gte: dateFormatBackend(nowInTz()) },
|
|
status: { notIn: [BookingStatus.REJECTED, BookingStatus.CANCELED] }
|
|
},
|
|
select: {
|
|
startDate: true,
|
|
endDate: true,
|
|
},
|
|
}),
|
|
calendarGetBookedDays(),
|
|
])
|
|
|
|
const bookingsInDb = bookingsInDbRaw.map(booking => getDays(booking)).flat();
|
|
|
|
return [...bookingsInDb, ...bookingsInCalendar].filter(uniqueFilter).sort()
|
|
}
|
|
|
|
export function getBookingByUUID(uuid: string) {
|
|
// TODO: can we ignore canceled and rejected ones ?
|
|
return prisma.booking.findUniqueOrThrow({
|
|
where: {
|
|
uuid,
|
|
},
|
|
include: {
|
|
bill: true
|
|
}
|
|
})
|
|
}
|
|
|
|
export function getBookings({
|
|
status = [
|
|
BookingStatus.REQUESTED,
|
|
BookingStatus.CONFIRMED,
|
|
],
|
|
startDateGreaterThan = '2000-01-01T00:00:00Z',
|
|
}
|
|
: { status?: BookingStatus[]; startDateGreaterThan?: string } = {}) {
|
|
return prisma.booking.findMany({
|
|
where: {
|
|
startDate: { gte: startDateGreaterThan },
|
|
status: { notIn: status }
|
|
},
|
|
});
|
|
}
|
|
|
|
export function createBooking(data: Booking) {
|
|
return prisma.booking.create({
|
|
data
|
|
});
|
|
}
|
|
|
|
export async function patchBooking(
|
|
uuid: string,
|
|
data: Prisma.BookingCreateInput
|
|
) {
|
|
const current = await prisma.booking.update({
|
|
where: { uuid },
|
|
data
|
|
});
|
|
const previous = { ...current, ...data }
|
|
return { current, previous }
|
|
|
|
}
|
|
|
|
export function createBill(
|
|
bookingUUID: string,
|
|
data: Prisma.BillCreateInput,
|
|
) {
|
|
return prisma.bill.create({
|
|
data: {
|
|
...data,
|
|
booking: {
|
|
connect: { uuid: bookingUUID }
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
export function patchBill(
|
|
bookingUUID: string,
|
|
data: Prisma.BillUncheckedUpdateInput,
|
|
) {
|
|
const { id, ...rest } = data;
|
|
return prisma.bill.update({
|
|
where: {
|
|
},
|
|
data: {
|
|
...rest,
|
|
booking: {
|
|
connect: { uuid: bookingUUID }
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function getMilageMax() {
|
|
const { milageEnd } = await prisma.bill.findFirst({
|
|
select: { milageEnd: true },
|
|
orderBy: [
|
|
{
|
|
milageEnd: 'desc',
|
|
},
|
|
],
|
|
});
|
|
|
|
return milageEnd || 0;
|
|
}
|