2023-04-25 22:39:47 +00:00
|
|
|
import type {
|
|
|
|
Attendee,
|
|
|
|
Booking,
|
|
|
|
BookingReference,
|
|
|
|
Credential,
|
|
|
|
DestinationCalendar,
|
|
|
|
EventType,
|
|
|
|
User,
|
|
|
|
} from "@prisma/client";
|
|
|
|
|
|
|
|
import { prisma } from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { SchedulingType } from "@calcom/prisma/enums";
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
import authedProcedure from "../../../procedures/authedProcedure";
|
2023-04-25 22:39:47 +00:00
|
|
|
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[];
|
|
|
|
};
|
|
|
|
};
|