perf: Refactor getEnabledApps to filter on credentials passed (#10353)

Co-authored-by: Omar López <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-08-01 16:40:52 +05:30
1 parent 88f542cb0d
commit 6fa54aba46
20 files changed
+76 -27

No files matched your search

+33 -3
View File
@@ -2,12 +2,42 @@ import getApps from "@calcom/app-store/utils";
import type { CredentialData } from "@calcom/app-store/utils";
import { prisma } from "@calcom/prisma";
const getEnabledApps = async (userCredentials: CredentialData[]) => {
import type { Prisma } from ".prisma/client";
/**
*
* @param credentials - Can be user or team credentials
* @param filterOnCredentials - Only include apps where credentials are present
* @returns A list of enabled app metadata & credentials tied to them
*/
const getEnabledApps = async (credentials: CredentialData[], filterOnCredentials?: boolean) => {
const filterOnIds = {
credentials: {
some: {
OR: [] as Prisma.CredentialWhereInput[],
},
},
} satisfies Prisma.AppWhereInput;
if (filterOnCredentials) {
const userIds: number[] = [],
teamIds: number[] = [];
for (const credential of credentials) {
if (credential.userId) userIds.push(credential.userId);
if (credential.teamId) teamIds.push(credential.teamId);
}
if (userIds.length) filterOnIds.credentials.some.OR.push({ userId: { in: userIds } });
if (teamIds.length) filterOnIds.credentials.some.OR.push({ teamId: { in: teamIds } });
}
const enabledApps = await prisma.app.findMany({
where: { enabled: true },
where: {
OR: [{ enabled: true, ...(filterOnIds.credentials.some.OR.length && filterOnIds) }],
},
select: { slug: true, enabled: true },
});
const apps = getApps(userCredentials);
const apps = getApps(credentials, filterOnCredentials);
const filteredApps = enabledApps.reduce((reducedArray, app) => {
const appMetadata = apps.find((metadata) => metadata.slug === app.slug);
+2 -1
View File
@@ -236,6 +236,7 @@ export default async function getEventTypeById({
type: true,
key: true,
userId: true,
teamId: true,
appId: true,
invalid: true,
},
@@ -326,7 +327,7 @@ export default async function getEventTypeById({
const currentUser = eventType.users.find((u) => u.id === userId);
const t = await getTranslation(currentUser?.locale ?? "en", "common");
const integrations = await getEnabledApps(credentials);
const integrations = await getEnabledApps(credentials, true);
const locationOptions = getLocationGroupedOptions(integrations, t);
if (eventType.schedulingType === SchedulingType.MANAGED) {
locationOptions.splice(0, 0, {