Files
cal.diy/packages/trpc/server/routers/viewer/bookings/util.ts
T
d6fb0df64f perf: tRPC procedures and middleware refactor (#8419)
* trpc procedures an middleware refactor

* allow use sessionMiddleware without a req object

* sync with the new tRPC structure

* tRPC refactor on routing form app

* import Prisma from @prisma/client

* Lazy load apps from appstore

* remove unrelated changes

* Add types for PaymentService

* type fixes

* Merge branch 'main' into roae85/cal-1514-set-the-user-session-only-on-the

* fix typo

* remove console.log

* remove explicit types from apstore object

* linter fixes

---------

Co-authored-by: Keith Williams <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
2023-05-09 19:27:05 +00:00

81 lines
2.0 KiB
TypeScript

import type {
Attendee,
Booking,
BookingReference,
Credential,
DestinationCalendar,
EventType,
User,
} from "@prisma/client";
import { prisma } from "@calcom/prisma";
import { SchedulingType } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import authedProcedure from "../../../procedures/authedProcedure";
import { commonBookingSchema } from "./types";
export const bookingsProcedure = authedProcedure
.input(commonBookingSchema)
.use(async ({ ctx, input, next }) => {
// Endpoints that just read the logged in user's data - like 'list' don't necessary have any input
const { bookingId } = input;
const booking = await prisma.booking.findFirst({
where: {
id: bookingId,
AND: [
{
OR: [
/* If user is organizer */
{ userId: ctx.user.id },
/* Or part of a collective booking */
{
eventType: {
schedulingType: SchedulingType.COLLECTIVE,
users: {
some: {
id: ctx.user.id,
},
},
},
},
],
},
],
},
include: {
attendees: true,
eventType: true,
destinationCalendar: true,
references: true,
user: {
include: {
destinationCalendar: true,
credentials: true,
},
},
},
});
if (!booking) throw new TRPCError({ code: "UNAUTHORIZED" });
return next({ ctx: { booking } });
});
export type BookingsProcedureContext = {
booking: Booking & {
eventType: EventType | null;
destinationCalendar: DestinationCalendar | null;
user:
| (User & {
destinationCalendar: DestinationCalendar | null;
credentials: Credential[];
})
| null;
references: BookingReference[];
attendees: Attendee[];
};
};