fix: include app data and credentials from DB (#11048)

* include app data and credentials from DB

* Improve performance
pull/11050/head
alannnc 2023-08-31 11:10:58 -07:00 committed by GitHub
parent c4e5d168a5
commit 53c8f8c04a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 38 deletions

View File

@ -1,10 +1,9 @@
import { Prisma } from "@prisma/client";
import { getAppFromSlug } from "@calcom/app-store/utils";
import { getCalendarCredentials, getConnectedCalendars } from "@calcom/core/CalendarManager";
import { getSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains";
import prisma, { baseEventTypeSelect } from "@calcom/prisma";
import { AppCategories, SchedulingType } from "@calcom/prisma/enums";
import { SchedulingType } from "@calcom/prisma/enums";
import { EventTypeMetaDataSchema, teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { WEBAPP_URL } from "../../../constants";
@ -32,7 +31,17 @@ export async function getTeamWithMembers(args: {
selectedCalendars: true,
credentials: {
include: {
app: true,
app: {
select: {
slug: true,
categories: true,
},
},
destinationCalendars: {
select: {
externalId: true,
},
},
},
},
});
@ -124,43 +133,39 @@ export async function getTeamWithMembers(args: {
});
if (!team) return null;
const members = await Promise.all(
team.members.map(async (obj) => {
const calendarCredentials = getCalendarCredentials(obj.user.credentials);
const { connectedCalendars } = await getConnectedCalendars(
calendarCredentials,
obj.user.selectedCalendars,
obj.user.destinationCalendar?.externalId
);
const connectedApps = obj.user.credentials
.map(({ app, id }) => {
const appMetaData = getAppFromSlug(app?.slug);
// This should improve performance saving already app data found.
const appDataMap = new Map();
const members = team.members.map((obj) => {
return {
...obj.user,
role: obj.role,
accepted: obj.accepted,
disableImpersonation: obj.disableImpersonation,
avatar: `${WEBAPP_URL}/${obj.user.username}/avatar.png`,
connectedApps: obj.user.credentials.map((cred) => {
const appSlug = cred.app?.slug;
let appData = appDataMap.get(appSlug);
if (!appData) {
appData = getAppFromSlug(appSlug);
appDataMap.set(appSlug, appData);
}
const isCalendar = cred?.app?.categories.includes("calendar");
const externalId = isCalendar ? cred.destinationCalendars[0]?.externalId : undefined;
return {
name: appData?.name,
logo: appData?.logo,
app: cred.app,
externalId,
};
}),
};
});
if (app?.categories.includes(AppCategories.calendar)) {
const externalId = connectedCalendars.find((cal) => cal.credentialId == id)?.primary?.email;
return { name: appMetaData?.name, logo: appMetaData?.logo, slug: appMetaData?.slug, externalId };
}
return { name: appMetaData?.name, logo: appMetaData?.logo, slug: appMetaData?.slug };
})
.sort((a, b) => (a.slug ?? "").localeCompare(b.slug ?? ""));
// Prevent credentials from leaking to frontend
const {
credentials: _credentials,
destinationCalendar: _destinationCalendar,
selectedCalendars: _selectedCalendars,
...rest
} = {
...obj.user,
role: obj.role,
accepted: obj.accepted,
disableImpersonation: obj.disableImpersonation,
avatar: `${WEBAPP_URL}/${obj.user.username}/avatar.png`,
connectedApps,
};
return rest;
})
);
const eventTypes = team.eventTypes.map((eventType) => ({
...eventType,
metadata: EventTypeMetaDataSchema.parse(eventType.metadata),