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";
|
|
|
|
|
2023-08-10 10:35:26 +00:00
|
|
|
type EnabledApp = ReturnType<typeof getApps>[number] & { enabled: boolean };
|
|
|
|
|
2023-08-01 11:10:52 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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);
|
2023-08-10 10:35:26 +00:00
|
|
|
const filteredApps = apps.reduce((reducedArray, app) => {
|
|
|
|
const appDbQuery = enabledApps.find((metadata) => metadata.slug === app.slug);
|
|
|
|
if (appDbQuery?.enabled || app.isGlobal) {
|
|
|
|
reducedArray.push({ ...app, enabled: true });
|
2022-12-07 21:47:02 +00:00
|
|
|
}
|
|
|
|
return reducedArray;
|
2023-08-10 10:35:26 +00:00
|
|
|
}, [] as EnabledApp[]);
|
2022-12-07 21:47:02 +00:00
|
|
|
|
|
|
|
return filteredApps;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default getEnabledApps;
|