feat: add non-auth apps and flowCount in getConnectedApps

This commit is contained in:
Ali BARIN
2022-08-05 16:13:50 +02:00
parent ae8f701e5c
commit 533d73d718
5 changed files with 35 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import { raw } from 'objection';
import App from '../../models/app'; import App from '../../models/app';
import Context from '../../types/express/context'; import Context from '../../types/express/context';
import { IApp, IConnection } from '@automatisch/types'; import { IApp, IConnection } from '@automatisch/types';
@@ -22,16 +23,41 @@ const getConnectedApps = async (
const connectionKeys = connections.map((connection) => connection.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[] }[];
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 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) => { .map((app: IApp) => {
const connection = connections.find( const connection = connections.find(
(connection) => (connection as IConnection).key === app.key (connection) => (connection as IConnection).key === app.key
); );
if (connection) { app.connectionCount = connection?.count || 0;
app.connectionCount = connection.count;
} app.flowCount = appFlowCounts[app.key];
return app; return app;
}); });

View File

@@ -85,6 +85,7 @@ type App {
name: String name: String
key: String key: String
connectionCount: Int connectionCount: Int
flowCount: Int
iconUrl: String iconUrl: String
docUrl: String docUrl: String
primaryColor: String primaryColor: String

View File

@@ -147,6 +147,7 @@ export interface IApp {
authenticationSteps: IAuthenticationStep[]; authenticationSteps: IAuthenticationStep[];
reconnectionSteps: IAuthenticationStep[]; reconnectionSteps: IAuthenticationStep[];
connectionCount: number; connectionCount: number;
flowCount: number;
triggers: any[]; triggers: any[];
actions: any[]; actions: any[];
connections: IConnection[]; connections: IConnection[];

View File

@@ -27,7 +27,7 @@ const countTranslation = (value: React.ReactNode) => (
function AppRow(props: AppRowProps): React.ReactElement { function AppRow(props: AppRowProps): React.ReactElement {
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const { name, primaryColor, iconUrl, connectionCount } = props.application; const { name, primaryColor, iconUrl, connectionCount, flowCount } = props.application;
return ( return (
<Link to={URLS.APP(name.toLowerCase())}> <Link to={URLS.APP(name.toLowerCase())}>
@@ -52,7 +52,7 @@ function AppRow(props: AppRowProps): React.ReactElement {
<Box sx={{ px: 2 }}> <Box sx={{ px: 2 }}>
<Typography variant="caption" color="textSecondary" sx={{ display: ['none', 'inline-block'] }}> <Typography variant="caption" color="textSecondary" sx={{ display: ['none', 'inline-block'] }}>
{formatMessage('app.flowCount', { count: countTranslation(0) })} {formatMessage('app.flowCount', { count: countTranslation(flowCount) })}
</Typography> </Typography>
</Box> </Box>

View File

@@ -9,6 +9,7 @@ export const GET_CONNECTED_APPS = gql`
docUrl docUrl
primaryColor primaryColor
connectionCount connectionCount
flowCount
supportsConnections supportsConnections
} }
} }