2022-12-07 21:47:02 +00:00
|
|
|
import getApps from "@calcom/app-store/utils";
|
2023-08-02 03:54:28 +00:00
|
|
|
import type { CredentialDataWithTeamName } from "@calcom/app-store/utils";
|
2022-12-07 21:47:02 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
|
2023-08-01 11:10:52 +00:00
|
|
|
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
|
|
|
|
*/
|
2023-08-02 03:54:28 +00:00
|
|
|
const getEnabledApps = async (credentials: CredentialDataWithTeamName[], filterOnCredentials?: boolean) => {
|
2023-08-01 11:10:52 +00:00
|
|
|
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 } });
|
|
|
|
}
|
|
|
|
|
2023-01-23 23:27:20 +00:00
|
|
|
const enabledApps = await prisma.app.findMany({
|
2023-08-01 11:10:52 +00:00
|
|
|
where: {
|
2023-08-02 03:54:28 +00:00
|
|
|
enabled: true,
|
|
|
|
...(filterOnIds.credentials.some.OR.length && filterOnIds),
|
2023-08-01 11:10:52 +00:00
|
|
|
},
|
2023-01-23 23:27:20 +00:00
|
|
|
select: { slug: true, enabled: true },
|
|
|
|
});
|
2023-08-01 11:10:52 +00:00
|
|
|
const apps = getApps(credentials, filterOnCredentials);
|
2022-12-07 21:47:02 +00:00
|
|
|
|
|
|
|
const filteredApps = enabledApps.reduce((reducedArray, app) => {
|
|
|
|
const appMetadata = apps.find((metadata) => metadata.slug === app.slug);
|
|
|
|
if (appMetadata) {
|
|
|
|
reducedArray.push({ ...appMetadata, enabled: app.enabled });
|
|
|
|
}
|
|
|
|
return reducedArray;
|
|
|
|
}, [] as (ReturnType<typeof getApps>[number] & { enabled: boolean })[]);
|
|
|
|
|
|
|
|
return filteredApps;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default getEnabledApps;
|