Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { describe, expect, it, vi } from 'vitest'

import getFlowConnections from '@/graphql/queries/get-flow-connections'
import type Context from '@/types/express/context'

vi.mock('@/models/app', () => ({
default: {
findAll: vi.fn(() => [
{ key: 'slack', name: 'Slack', iconUrl: 'https://example.com/slack.png' },
{
key: 'telegram-bot',
name: 'Telegram',
iconUrl: 'https://example.com/telegram.png',
},
{
key: 'tiles',
name: 'Tables',
iconUrl: 'https://example.com/tiles.png',
},
]),
},
}))

const mockFlowConnectionsQuery = vi.fn()

vi.mock('@/models/flow-connections', () => ({
default: {
query: () => mockFlowConnectionsQuery(),
},
}))

const context = {
currentUser: {
withAccessibleFlows: vi.fn(() => Promise.resolve()),
},
} as unknown as Context

describe('getFlowConnections', () => {
it('should use connection screenName and app key for regular connections', async () => {
mockFlowConnectionsQuery.mockReturnValue({
where: vi.fn().mockReturnThis(),
withGraphFetched: vi.fn().mockResolvedValue([
{
flowId: 'flow-123',
connectionId: 'conn-456',
connectionType: 'connection',
connection: {
key: 'slack',
formattedData: {
screenName: 'My Slack Workspace',
},
},
user: { email: '[email protected]' },
},
]),
})

const result = await getFlowConnections({}, { flowId: 'flow-123' }, context)

expect(result).toEqual([
{
flowId: 'flow-123',
connectionId: 'conn-456',
connectionType: 'connection',
addedBy: '[email protected]',
appName: 'Slack',
appIconUrl: 'https://example.com/slack.png',
connectionName: 'My Slack Workspace',
},
])
})

it('should use table name and "tiles" as appKey for table connections', async () => {
mockFlowConnectionsQuery.mockReturnValue({
where: vi.fn().mockReturnThis(),
withGraphFetched: vi.fn().mockResolvedValue([
{
flowId: 'flow-123',
connectionId: 'table-789',
connectionType: 'table',
table: {
name: 'My Customer Table',
},
user: { email: '[email protected]' },
},
]),
})

const result = await getFlowConnections({}, { flowId: 'flow-123' }, context)

expect(result).toEqual([
{
flowId: 'flow-123',
connectionId: 'table-789',
connectionType: 'table',
addedBy: '[email protected]',
appName: 'Tables',
appIconUrl: 'https://example.com/tiles.png',
connectionName: 'My Customer Table',
},
])
})

it('should handle mixed connection types correctly', async () => {
mockFlowConnectionsQuery.mockReturnValue({
where: vi.fn().mockReturnThis(),
withGraphFetched: vi.fn().mockResolvedValue([
{
flowId: 'flow-123',
connectionId: 'conn-456',
connectionType: 'connection',
connection: {
key: 'telegram-bot',
formattedData: {
screenName: 'My Telegram Bot',
},
},
user: {
email: '[email protected]',
},
},
{
flowId: 'flow-123',
connectionId: 'table-789',
connectionType: 'table',
table: {
name: 'Sales Data',
},
user: {
email: '[email protected]',
},
},
]),
})

const result = await getFlowConnections({}, { flowId: 'flow-123' }, context)

expect(result).toHaveLength(2)
expect(result[0]).toMatchObject({
connectionType: 'connection',
appName: 'Telegram',
connectionName: 'My Telegram Bot',
})
expect(result[1]).toMatchObject({
connectionType: 'table',
appName: 'Tables',
connectionName: 'Sales Data',
})
})
})
60 changes: 60 additions & 0 deletions packages/backend/src/graphql/queries/get-flow-connections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { IApp } from '@/../../types'
import App from '@/models/app'
import FlowConnections from '@/models/flow-connections'

import { QueryResolvers } from '../__generated__/types.generated'

const getFlowConnections: QueryResolvers['getFlowConnections'] = async (
_parent,
params,
context,
) => {
const apps = await App.findAll()

await context.currentUser.withAccessibleFlows({
requiredRole: 'editor',
})

const rawFlowConnections = await FlowConnections.query()
.where({
flow_id: params.flowId,
})
.withGraphFetched({
connection: true,
user: true,
table: true,
})

const filteredFlowConnections = rawFlowConnections.filter(
(flowConnection) => flowConnection.connection || flowConnection.table,
)

const flowConnections = await Promise.all(
filteredFlowConnections.map(async (flowConnection) => {
let connectionName = flowConnection?.connection?.formattedData?.screenName
if (flowConnection.connectionType === 'table') {
connectionName = flowConnection.table?.name
}

let appKey = flowConnection.connection?.key
if (flowConnection.connectionType === 'table') {
appKey = 'tiles'
}
const app = apps.find((app: IApp) => app.key === appKey)

return {
flowId: flowConnection.flowId,
connectionId: flowConnection.connectionId,
connectionType: flowConnection.connectionType,
addedBy: flowConnection?.user?.email || '',
appName: app.name,
appIconUrl: app.iconUrl,
connectionName: connectionName as string,
}
}),
)

return flowConnections
}

export default getFlowConnections
2 changes: 2 additions & 0 deletions packages/backend/src/graphql/query-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import getExecution from './queries/get-execution'
import getExecutionSteps from './queries/get-execution-steps'
import getExecutions from './queries/get-executions'
import getFlow from './queries/get-flow'
import getFlowConnections from './queries/get-flow-connections'
import getFlowTransferDetails from './queries/get-flow-transfer-details'
import getFlows from './queries/get-flows'
import getPendingFlowTransfers from './queries/get-pending-flow-transfers'
Expand Down Expand Up @@ -43,6 +44,7 @@ export default {
getCurrentUser,
healthcheck,
getPendingFlowTransfers,
getFlowConnections,
getFlowTransferDetails,
getTemplates,
...tilesQueryResolvers,
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Query {
key: String!
parameters: JSONObject
): [JSONObject]
getFlowConnections(flowId: String!): [SharedFlowConnection!]!
# Tiles
getTable(tableId: String!): TableMetadata!
getTableConnections(tableIds: [String!]!): JSONObject
Expand Down Expand Up @@ -450,6 +451,16 @@ type FlowAttachmentConfig {
updatedAt: String!
}

type SharedFlowConnection {
flowId: String!
connectionId: String!
connectionType: String!
appName: String!
appIconUrl: String!
addedBy: String!
connectionName: String!
}

type Execution {
id: String
testRun: Boolean
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/models/flow-connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FlowConnections extends Base {
connectionType!: 'connection' | 'table'
connection?: Connection
table?: TableMetadata
user?: User
metadata: Record<string, any>
flow?: Flow

Expand Down