2022-09-05 21:22:28 +00:00
|
|
|
import type { Credential } from "@prisma/client";
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-23 22:00:30 +00:00
|
|
|
import { App } from "@calcom/types/App";
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
export async function getAppWithMetadata(app: { dirName: string }) {
|
|
|
|
let appMetadata: App | null = null;
|
|
|
|
try {
|
|
|
|
appMetadata = (await import(`./${app.dirName}/_metadata`)).default as App;
|
|
|
|
} catch (error) {
|
2022-07-14 12:40:53 +00:00
|
|
|
try {
|
|
|
|
appMetadata = (await import(`./ee/${app.dirName}/_metadata`)).default as App;
|
|
|
|
} catch (e) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
console.error(`No metadata found for: "${app.dirName}". Message:`, error.message);
|
|
|
|
}
|
|
|
|
return null;
|
2022-05-02 20:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!appMetadata) return null;
|
|
|
|
// Let's not leak api keys to the front end
|
2022-07-14 12:40:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-05-02 20:39:35 +00:00
|
|
|
const { key, ...metadata } = appMetadata;
|
|
|
|
return metadata;
|
|
|
|
}
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
/** Mainly to use in listings for the frontend, use in getStaticProps or getServerSideProps */
|
2022-05-02 20:39:35 +00:00
|
|
|
export async function getAppRegistry() {
|
|
|
|
const dbApps = await prisma.app.findMany({ select: { dirName: true, slug: true, categories: true } });
|
|
|
|
const apps = [] as Omit<App, "key">[];
|
|
|
|
for await (const dbapp of dbApps) {
|
|
|
|
const app = await getAppWithMetadata(dbapp);
|
|
|
|
if (!app) continue;
|
2022-04-06 17:51:31 +00:00
|
|
|
// Skip if app isn't installed
|
2022-05-02 20:39:35 +00:00
|
|
|
/* This is now handled from the DB */
|
|
|
|
// if (!app.installed) return apps;
|
2022-08-26 00:48:50 +00:00
|
|
|
|
|
|
|
const { rating, reviews, trending, verified, ...remainingAppProps } = app;
|
2022-05-05 22:18:28 +00:00
|
|
|
apps.push({
|
2022-08-26 00:48:50 +00:00
|
|
|
rating: rating || 0,
|
|
|
|
reviews: reviews || 0,
|
|
|
|
trending: trending || true,
|
|
|
|
verified: verified || true,
|
|
|
|
...remainingAppProps,
|
|
|
|
category: app.category || "other",
|
2022-05-05 22:18:28 +00:00
|
|
|
installed:
|
|
|
|
true /* All apps from DB are considered installed by default. @TODO: Add and filter our by `enabled` property */,
|
|
|
|
});
|
2022-05-02 20:39:35 +00:00
|
|
|
}
|
|
|
|
return apps;
|
2022-03-23 22:00:30 +00:00
|
|
|
}
|
2022-09-05 21:22:28 +00:00
|
|
|
|
|
|
|
export async function getAppRegistryWithCredentials(userId: number) {
|
|
|
|
const dbApps = await prisma.app.findMany({ include: { credentials: { where: { userId } } } });
|
|
|
|
const apps = [] as (Omit<App, "key"> & {
|
|
|
|
credentials: Credential[];
|
|
|
|
})[];
|
|
|
|
for await (const dbapp of dbApps) {
|
|
|
|
const app = await getAppWithMetadata(dbapp);
|
|
|
|
if (!app) continue;
|
|
|
|
// Skip if app isn't installed
|
|
|
|
/* This is now handled from the DB */
|
|
|
|
// if (!app.installed) return apps;
|
|
|
|
|
|
|
|
const { rating, reviews, trending, verified, ...remainingAppProps } = app;
|
|
|
|
apps.push({
|
|
|
|
rating: rating || 0,
|
|
|
|
reviews: reviews || 0,
|
|
|
|
trending: trending || true,
|
|
|
|
verified: verified || true,
|
|
|
|
...remainingAppProps,
|
|
|
|
categories: dbapp.categories,
|
|
|
|
credentials: dbapp.credentials,
|
|
|
|
installed:
|
|
|
|
true /* All apps from DB are considered installed by default. @TODO: Add and filter our by `enabled` property */,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return apps;
|
|
|
|
}
|