From 533d73d718f7091bb1b886bb48912664cb06fc3b Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 5 Aug 2022 16:13:50 +0200 Subject: [PATCH 1/2] feat: add non-auth apps and flowCount in getConnectedApps --- .../src/graphql/queries/get-connected-apps.ts | 34 ++++++++++++++++--- packages/backend/src/graphql/schema.graphql | 1 + packages/types/index.d.ts | 1 + packages/web/src/components/AppRow/index.tsx | 4 +-- .../src/graphql/queries/get-connected-apps.ts | 1 + 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/graphql/queries/get-connected-apps.ts b/packages/backend/src/graphql/queries/get-connected-apps.ts index 2373944f..a28503e2 100644 --- a/packages/backend/src/graphql/queries/get-connected-apps.ts +++ b/packages/backend/src/graphql/queries/get-connected-apps.ts @@ -1,3 +1,4 @@ +import { raw } from 'objection'; import App from '../../models/app'; import Context from '../../types/express/context'; import { IApp, IConnection } from '@automatisch/types'; @@ -22,16 +23,41 @@ const getConnectedApps = async ( const connectionKeys = connections.map((connection) => connection.key); + const flows = await context.currentUser + .$relatedQuery('steps') + .select('flow_id', raw('ARRAY_AGG(app_key)').as('apps')) + .groupBy('flow_id') as unknown as { flow_id: string; apps: string[] }[]; + + const appFlowCounts = flows.reduce((counts, flow) => { + const apps = flow.apps; + const unifiedApps = Array.from(new Set(apps)) + + for (const app of unifiedApps) { + if (!counts[app]) { + counts[app] = 0; + } + + counts[app] += 1; + } + + return counts; + }, {} as { [key: string]: number }); + apps = apps - .filter((app: IApp) => connectionKeys.includes(app.key)) + .filter((app: IApp) => { + const hasConnections = connectionKeys.includes(app.key); + const hasFlows = flows.find((flow) => flow.apps.includes(app.key)); + + return hasFlows || hasConnections; + }) .map((app: IApp) => { const connection = connections.find( (connection) => (connection as IConnection).key === app.key ); - if (connection) { - app.connectionCount = connection.count; - } + app.connectionCount = connection?.count || 0; + + app.flowCount = appFlowCounts[app.key]; return app; }); diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 3d5eeffa..8e135879 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -85,6 +85,7 @@ type App { name: String key: String connectionCount: Int + flowCount: Int iconUrl: String docUrl: String primaryColor: String diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 67b12115..51807ecd 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -147,6 +147,7 @@ export interface IApp { authenticationSteps: IAuthenticationStep[]; reconnectionSteps: IAuthenticationStep[]; connectionCount: number; + flowCount: number; triggers: any[]; actions: any[]; connections: IConnection[]; diff --git a/packages/web/src/components/AppRow/index.tsx b/packages/web/src/components/AppRow/index.tsx index f04f9c35..e575f187 100644 --- a/packages/web/src/components/AppRow/index.tsx +++ b/packages/web/src/components/AppRow/index.tsx @@ -27,7 +27,7 @@ const countTranslation = (value: React.ReactNode) => ( function AppRow(props: AppRowProps): React.ReactElement { const formatMessage = useFormatMessage(); - const { name, primaryColor, iconUrl, connectionCount } = props.application; + const { name, primaryColor, iconUrl, connectionCount, flowCount } = props.application; return ( @@ -52,7 +52,7 @@ function AppRow(props: AppRowProps): React.ReactElement { - {formatMessage('app.flowCount', { count: countTranslation(0) })} + {formatMessage('app.flowCount', { count: countTranslation(flowCount) })} diff --git a/packages/web/src/graphql/queries/get-connected-apps.ts b/packages/web/src/graphql/queries/get-connected-apps.ts index 1c91f6e9..b4529e8e 100644 --- a/packages/web/src/graphql/queries/get-connected-apps.ts +++ b/packages/web/src/graphql/queries/get-connected-apps.ts @@ -9,6 +9,7 @@ export const GET_CONNECTED_APPS = gql` docUrl primaryColor connectionCount + flowCount supportsConnections } } From 8c5d95796fcad3261130527518fe4b48a9aac9d8 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Mon, 8 Aug 2022 19:39:11 +0300 Subject: [PATCH 2/2] refactor: Adjust getConnectedApps graphql query --- .../src/graphql/queries/get-connected-apps.ts | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/packages/backend/src/graphql/queries/get-connected-apps.ts b/packages/backend/src/graphql/queries/get-connected-apps.ts index a28503e2..31780691 100644 --- a/packages/backend/src/graphql/queries/get-connected-apps.ts +++ b/packages/backend/src/graphql/queries/get-connected-apps.ts @@ -1,4 +1,3 @@ -import { raw } from 'objection'; import App from '../../models/app'; import Context from '../../types/express/context'; import { IApp, IConnection } from '@automatisch/types'; @@ -21,34 +20,21 @@ const getConnectedApps = async ( .count('connections.id as count') .groupBy('connections.key'); - const connectionKeys = connections.map((connection) => connection.key); - const flows = await context.currentUser - .$relatedQuery('steps') - .select('flow_id', raw('ARRAY_AGG(app_key)').as('apps')) - .groupBy('flow_id') as unknown as { flow_id: string; apps: string[] }[]; + .$relatedQuery('flows') + .withGraphJoined('steps') + .orderBy('created_at', 'desc'); - const appFlowCounts = flows.reduce((counts, flow) => { - const apps = flow.apps; - const unifiedApps = Array.from(new Set(apps)) + const duplicatedUsedApps = flows + .map((flow) => flow.steps.map((step) => step.appKey)) + .flat() + .filter(Boolean); - for (const app of unifiedApps) { - if (!counts[app]) { - counts[app] = 0; - } - - counts[app] += 1; - } - - return counts; - }, {} as { [key: string]: number }); + const usedApps = [...new Set(duplicatedUsedApps)]; apps = apps .filter((app: IApp) => { - const hasConnections = connectionKeys.includes(app.key); - const hasFlows = flows.find((flow) => flow.apps.includes(app.key)); - - return hasFlows || hasConnections; + return usedApps.includes(app.key); }) .map((app: IApp) => { const connection = connections.find( @@ -56,8 +42,15 @@ const getConnectedApps = async ( ); app.connectionCount = connection?.count || 0; + app.flowCount = 0; - app.flowCount = appFlowCounts[app.key]; + flows.forEach((flow) => { + const usedFlow = flow.steps.find((step) => step.appKey === app.key); + + if (usedFlow) { + app.flowCount += 1; + } + }); return app; });