2023-01-18 22:30:25 +00:00
import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData" ;
2023-03-09 09:07:23 +00:00
import { getAppFromSlug } from "@calcom/app-store/utils" ;
2022-12-20 22:15:06 +00:00
import prisma , { safeAppSelect , safeCredentialSelect } from "@calcom/prisma" ;
2023-02-17 09:11:03 +00:00
import { userMetadata } from "@calcom/prisma/zod-utils" ;
2023-02-16 22:39:57 +00:00
import type { AppFrontendPayload as App } from "@calcom/types/App" ;
import type { CredentialFrontendPayload as Credential } from "@calcom/types/Credential" ;
2022-03-23 22:00:30 +00:00
2022-05-02 20:39:35 +00:00
export async function getAppWithMetadata ( app : { dirName : string } ) {
2023-01-18 22:30:25 +00:00
const appMetadata : App | null = appStoreMetadata [ app . dirName as keyof typeof appStoreMetadata ] as App ;
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 ;
2023-01-18 22:30:25 +00:00
if ( metadata . logo && ! metadata . logo . includes ( "/api/app-store/" ) ) {
const appDirName = ` ${ metadata . isTemplate ? "templates" : "" } / ${ app . dirName } ` ;
metadata . logo = ` /api/app-store/ ${ appDirName } / ${ metadata . logo } ` ;
}
2022-05-02 20:39:35 +00:00
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() {
2022-12-07 21:47:02 +00:00
const dbApps = await prisma . app . findMany ( {
where : { enabled : true } ,
select : { dirName : true , slug : true , categories : true , enabled : true } ,
} ) ;
2022-12-20 22:15:06 +00:00
const apps = [ ] as App [ ] ;
2022-05-02 20:39:35 +00:00
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 ) {
2022-12-07 21:47:02 +00:00
const dbApps = await prisma . app . findMany ( {
where : { enabled : true } ,
2022-12-20 22:15:06 +00:00
select : {
. . . safeAppSelect ,
credentials : {
where : { userId } ,
select : safeCredentialSelect ,
} ,
} ,
2023-01-18 18:48:10 +00:00
orderBy : {
credentials : {
_count : "desc" ,
} ,
} ,
2022-12-07 21:47:02 +00:00
} ) ;
2023-02-17 09:11:03 +00:00
const user = await prisma . user . findUnique ( {
where : {
id : userId ,
} ,
select : {
metadata : true ,
} ,
} ) ;
const usersDefaultApp = userMetadata . parse ( user ? . metadata ) ? . defaultConferencingApp ? . appSlug ;
2022-12-20 22:15:06 +00:00
const apps = [ ] as ( App & {
2022-09-05 21:22:28 +00:00
credentials : Credential [ ] ;
2023-02-17 09:11:03 +00:00
isDefault? : boolean ;
2022-09-05 21:22:28 +00:00
} ) [ ] ;
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;
2023-03-09 09:07:23 +00:00
let dependencyData : {
name? : string ;
installed? : boolean ;
} [ ] = [ ] ;
if ( app . dependencies ) {
dependencyData = app . dependencies . map ( ( dependency ) = > {
const dependencyInstalled = dbApps . some (
( dbAppIterator ) = > dbAppIterator . credentials . length && dbAppIterator . slug === dependency
) ;
// If the app marked as dependency is simply deleted from the codebase, we can have the situation where App is marked installed in DB but we couldn't get the app.
const dependencyName = getAppFromSlug ( dependency ) ? . name ;
return { name : dependencyName , installed : dependencyInstalled } ;
} ) ;
}
2022-09-05 21:22:28 +00:00
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 ,
2022-12-20 22:15:06 +00:00
installed : true ,
2023-02-17 09:11:03 +00:00
isDefault : usersDefaultApp === dbapp . slug ,
2023-03-09 09:07:23 +00:00
. . . ( app . dependencies && { dependencyData } ) ,
2022-09-05 21:22:28 +00:00
} ) ;
}
2023-03-09 09:07:23 +00:00
2022-09-05 21:22:28 +00:00
return apps ;
}