Skip to content

Commit b3cef50

Browse files
authored
Merge branch 'main' into feat/org-wide-workflows
2 parents acf15f3 + 86fc516 commit b3cef50

File tree

2 files changed

+88
-74
lines changed

2 files changed

+88
-74
lines changed

packages/features/bookings/lib/handleNewBooking.ts

+1-74
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { App, DestinationCalendar, EventTypeCustomInput } from "@prisma/client";
22
import { Prisma } from "@prisma/client";
3-
import type { IncomingMessage } from "http";
43
import { isValidPhoneNumber } from "libphonenumber-js";
54
// eslint-disable-next-line no-restricted-imports
65
import { cloneDeep } from "lodash";
@@ -41,7 +40,6 @@ import { getBookingFieldsWithSystemFields } from "@calcom/features/bookings/lib/
4140
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
4241
import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger";
4342
import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled";
44-
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
4543
import {
4644
allowDisablingAttendeeConfirmationEmails,
4745
allowDisablingHostConfirmationEmails,
@@ -79,7 +77,6 @@ import { getPiiFreeCalendarEvent, getPiiFreeEventType, getPiiFreeUser } from "@c
7977
import { safeStringify } from "@calcom/lib/safeStringify";
8078
import { checkBookingLimits, checkDurationLimits, getLuckyUser } from "@calcom/lib/server";
8179
import { getTranslation } from "@calcom/lib/server/i18n";
82-
import { UserRepository } from "@calcom/lib/server/repository/user";
8380
import { slugify } from "@calcom/lib/slugify";
8481
import { updateWebUser as syncServicesUpdateWebUser } from "@calcom/lib/sync/SyncServiceManager";
8582
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
@@ -112,6 +109,7 @@ import { checkForConflicts } from "./conflictChecker/checkForConflicts";
112109
import { getAllCredentials } from "./getAllCredentialsForUsersOnEvent/getAllCredentials";
113110
import { refreshCredentials } from "./getAllCredentialsForUsersOnEvent/refreshCredentials";
114111
import getBookingDataSchema from "./getBookingDataSchema";
112+
import { loadUsers } from "./handleNewBooking/loadUsers";
115113
import handleSeats from "./handleSeats/handleSeats";
116114
import type { BookingSeat } from "./handleSeats/types";
117115

@@ -297,40 +295,6 @@ type IsFixedAwareUser = User & {
297295
priority?: number;
298296
};
299297

300-
const loadUsers = async (eventType: NewBookingEventType, dynamicUserList: string[], req: IncomingMessage) => {
301-
try {
302-
if (!eventType.id) {
303-
if (!Array.isArray(dynamicUserList) || dynamicUserList.length === 0) {
304-
throw new Error("dynamicUserList is not properly defined or empty.");
305-
}
306-
const { isValidOrgDomain, currentOrgDomain } = orgDomainConfig(req);
307-
const users = await findUsersByUsername({
308-
usernameList: dynamicUserList,
309-
orgSlug: isValidOrgDomain ? currentOrgDomain : null,
310-
});
311-
return users;
312-
}
313-
const hosts = eventType.hosts || [];
314-
315-
if (!Array.isArray(hosts)) {
316-
throw new Error("eventType.hosts is not properly defined.");
317-
}
318-
319-
const users = hosts.map(({ user, isFixed, priority }) => ({
320-
...user,
321-
isFixed,
322-
priority,
323-
}));
324-
325-
return users.length ? users : eventType.users;
326-
} catch (error) {
327-
if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) {
328-
throw new HttpError({ statusCode: 400, message: error.message });
329-
}
330-
throw new HttpError({ statusCode: 500, message: "Unable to load users" });
331-
}
332-
};
333-
334298
export async function ensureAvailableUsers(
335299
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> & {
336300
users: IsFixedAwareUser[];
@@ -2647,40 +2611,3 @@ function handleCustomInputs(
26472611
}
26482612
});
26492613
}
2650-
2651-
/**
2652-
* This method is mostly same as the one in UserRepository but it includes a lot more relations which are specific requirement here
2653-
* TODO: Figure out how to keep it in UserRepository and use it here
2654-
*/
2655-
export const findUsersByUsername = async ({
2656-
usernameList,
2657-
orgSlug,
2658-
}: {
2659-
orgSlug: string | null;
2660-
usernameList: string[];
2661-
}) => {
2662-
log.debug("findUsersByUsername", { usernameList, orgSlug });
2663-
const { where, profiles } = await UserRepository._getWhereClauseForFindingUsersByUsername({
2664-
orgSlug,
2665-
usernameList,
2666-
});
2667-
return (
2668-
await prisma.user.findMany({
2669-
where,
2670-
select: {
2671-
...userSelect.select,
2672-
credentials: {
2673-
select: credentialForCalendarServiceSelect,
2674-
},
2675-
metadata: true,
2676-
},
2677-
})
2678-
).map((user) => {
2679-
const profile = profiles?.find((profile) => profile.user.id === user.id) ?? null;
2680-
return {
2681-
...user,
2682-
organizationId: profile?.organizationId ?? null,
2683-
profile,
2684-
};
2685-
});
2686-
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Prisma } from "@prisma/client";
2+
import type { IncomingMessage } from "http";
3+
4+
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
5+
import { HttpError } from "@calcom/lib/http-error";
6+
import logger from "@calcom/lib/logger";
7+
import { UserRepository } from "@calcom/lib/server/repository/user";
8+
import prisma, { userSelect } from "@calcom/prisma";
9+
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
10+
11+
import type { NewBookingEventType } from "../handleNewBooking";
12+
13+
const log = logger.getSubLogger({ prefix: ["[loadUsers]:handleNewBooking "] });
14+
15+
type EventType = Pick<NewBookingEventType, "hosts" | "users" | "id">;
16+
17+
export const loadUsers = async (eventType: EventType, dynamicUserList: string[], req: IncomingMessage) => {
18+
try {
19+
const { currentOrgDomain } = orgDomainConfig(req);
20+
21+
return eventType.id
22+
? await loadUsersByEventType(eventType)
23+
: await loadDynamicUsers(dynamicUserList, currentOrgDomain);
24+
} catch (error) {
25+
if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) {
26+
throw new HttpError({ statusCode: 400, message: error.message });
27+
}
28+
throw new HttpError({ statusCode: 500, message: "Unable to load users" });
29+
}
30+
};
31+
32+
const loadUsersByEventType = async (eventType: EventType): Promise<NewBookingEventType["users"]> => {
33+
const hosts = eventType.hosts || [];
34+
const users = hosts.map(({ user, isFixed, priority }) => ({
35+
...user,
36+
isFixed,
37+
priority,
38+
}));
39+
return users.length ? users : eventType.users;
40+
};
41+
42+
const loadDynamicUsers = async (dynamicUserList: string[], currentOrgDomain: string | null) => {
43+
if (!Array.isArray(dynamicUserList) || dynamicUserList.length === 0) {
44+
throw new Error("dynamicUserList is not properly defined or empty.");
45+
}
46+
return findUsersByUsername({
47+
usernameList: dynamicUserList,
48+
orgSlug: !!currentOrgDomain ? currentOrgDomain : null,
49+
});
50+
};
51+
52+
/**
53+
* This method is mostly same as the one in UserRepository but it includes a lot more relations which are specific requirement here
54+
* TODO: Figure out how to keep it in UserRepository and use it here
55+
*/
56+
export const findUsersByUsername = async ({
57+
usernameList,
58+
orgSlug,
59+
}: {
60+
orgSlug: string | null;
61+
usernameList: string[];
62+
}) => {
63+
log.debug("findUsersByUsername", { usernameList, orgSlug });
64+
const { where, profiles } = await UserRepository._getWhereClauseForFindingUsersByUsername({
65+
orgSlug,
66+
usernameList,
67+
});
68+
return (
69+
await prisma.user.findMany({
70+
where,
71+
select: {
72+
...userSelect.select,
73+
credentials: {
74+
select: credentialForCalendarServiceSelect,
75+
},
76+
metadata: true,
77+
},
78+
})
79+
).map((user) => {
80+
const profile = profiles?.find((profile) => profile.user.id === user.id) ?? null;
81+
return {
82+
...user,
83+
organizationId: profile?.organizationId ?? null,
84+
profile,
85+
};
86+
});
87+
};

0 commit comments

Comments
 (0)