kysely query 3

get-schedule-edge
supalarry 2023-10-26 10:10:09 +02:00
parent 6ebd64bd03
commit e5be585c03
1 changed files with 76 additions and 21 deletions

View File

@ -16,10 +16,9 @@ import isTimeOutOfBounds from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger";
import { performance } from "@calcom/lib/server/perfObserver";
import getSlots from "@calcom/lib/slots";
import prisma, { availabilityUserSelect } from "@calcom/prisma";
import prisma from "@calcom/prisma";
import { SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus } from "@calcom/prisma/enums";
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { EventBusyDate } from "@calcom/types/Calendar";
@ -351,25 +350,81 @@ export async function getDynamicEventType(
});
}
const dynamicEventType = getDefaultEvent(input.eventTypeSlug);
const users = await prisma.user.findMany({
where: {
username: {
in: Array.isArray(input.usernameList)
? input.usernameList
: input.usernameList
? [input.usernameList]
: [],
},
organization: isValidOrgDomain && currentOrgDomain ? getSlugOrRequestedSlug(currentOrgDomain) : null,
},
select: {
allowDynamicBooking: true,
...availabilityUserSelect,
credentials: {
select: credentialForCalendarServiceSelect,
},
},
});
const usernameList = Array.isArray(input.usernameList)
? input.usernameList
: input.usernameList
? [input.usernameList]
: [];
const slugifiedValue = slugify(currentOrgDomain);
const users = await db
.selectFrom("user")
.innerJoin("organization as org", "org.id", "user.organizationId")
.where((eb) =>
eb.and([
usernameList.length > 0 ? eb("username", "in", usernameList) : undefined,
isValidOrgDomain && currentOrgDomain
? eb.or([
eb("org.slug", "=", slugifiedValue),
eb.raw(`metadata->>'requestedSlug' = ?`, slugifiedValue),
])
: undefined,
])
)
.select((eb) => [
"allowDynamicBooking",
"id",
"timeZone",
"email",
"bufferTime",
"startTime",
"username",
"endTime",
"timeFormat",
"defaultScheduleId",
jsonArrayFrom(
eb
.selectFrom("Schedule")
.whereRef("Schedule.userId", "=", "users.id")
.select((eb) => [
"id",
"timeZone",
jsonArrayFrom(
eb
.selectFrom("Availability")
.select(["date", "startTime", "endTime", "days"])
.whereRef("Availability.scheduleId", "=", "Schedule.id")
).as("availability"),
])
).as("schedules"),
jsonArrayFrom(
eb.selectFrom("Availability").selectAll().whereRef("Availability.userId", "=", "users.id")
).as("availability"),
jsonArrayFrom(
eb.selectFrom("SelectedCalendar").selectAll().whereRef("SelectedCalendar.userId", "=", "users.id")
).as("selectedCalendars"),
jsonArrayFrom(
eb
.selectFrom("Credential")
.whereRef("Credential.userId", "=", "users.id")
.select([
"id",
"appId",
"type",
"userId",
"teamId",
"key",
"invalid",
jsonObjectFrom(
eb.selectFrom("users").whereRef("users.id", "=", "Credential.userId").select("email")
).as("user"),
])
).as("credentials"),
])
.execute();
const isDynamicAllowed = !users.some((user) => !user.allowDynamicBooking);
if (!isDynamicAllowed) {
throw new TRPCError({