Files
automatisch/packages/backend/src/graphql/queries/get-app.ts
2023-11-30 14:45:08 +01:00

72 lines
1.9 KiB
TypeScript

import App from '../../models/app';
import Connection from '../../models/connection';
import Context from '../../types/express/context';
type Params = {
key: string;
};
const getApp = async (_parent: unknown, params: Params, context: Context) => {
const conditions = context.currentUser.can('read', 'Connection');
const app = await App.findOneByKey(params.key);
if (context.currentUser) {
const userConnections = context.currentUser.relatedConnectionsQuery();
const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections;
const connections = await Connection.query()
.with('connections', connectionBaseQuery)
.with(
'connections_with_flow_count',
Connection.query()
.clearSelect()
.select('connections.id')
.leftJoinRelated('steps')
.leftJoin('flows', function () {
this
.on(
'flows.id',
'=',
'steps.flow_id',
)
if (conditions.isCreator) {
this.andOnVal(
'flows.user_id',
'=',
context.currentUser.id
)
}
})
.where({
'connections.key': params.key,
'connections.draft': false,
})
.countDistinct('steps.flow_id as flowCount')
.groupBy('connections.id')
)
.select(
'connections.*',
'connections_with_flow_count.flowCount as flowCount'
)
.from('connections')
.withGraphFetched({
appConfig: true,
appAuthClient: true
})
.joinRaw('join connections_with_flow_count on connections.id = connections_with_flow_count.id')
.orderBy('connections.created_at', 'desc');
return {
...app,
connections,
};
}
return app;
};
export default getApp;