diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index 1cbfca6307..1de34856d3 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -67,6 +67,7 @@ import { whereClause } from './UserManagement/UserManagementHelper'; import { WorkflowsService } from './workflows/workflows.services'; import { START_NODES } from './constants'; import { webhookNotFoundErrorMessage } from './utils'; +import { In } from 'typeorm'; const WEBHOOK_PROD_UNREGISTERED_HINT = "The workflow must be active for a production URL to run successfully. You can activate the workflow using the toggle in the top-right of the editor. Note that unlike test URL calls, production URL calls aren't shown on the canvas (only in the executions list)"; @@ -168,11 +169,7 @@ export class ActiveWorkflowRunner { activeWorkflowIds.push.apply(activeWorkflowIds, this.activeWorkflows.allActiveWorkflows()); const activeWorkflows = await this.getActiveWorkflows(); - activeWorkflowIds = [ - ...activeWorkflowIds, - ...activeWorkflows.map((workflow) => workflow.id.toString()), - ]; - + activeWorkflowIds = [...activeWorkflowIds, ...activeWorkflows]; // Make sure IDs are unique activeWorkflowIds = Array.from(new Set(activeWorkflowIds)); @@ -348,30 +345,31 @@ export class ActiveWorkflowRunner { /** * Returns the ids of the currently active workflows */ - async getActiveWorkflows(user?: User): Promise { + async getActiveWorkflows(user?: User): Promise { let activeWorkflows: WorkflowEntity[] = []; - if (!user || user.globalRole.name === 'owner') { activeWorkflows = await Db.collections.Workflow.find({ select: ['id'], where: { active: true }, }); + return activeWorkflows.map((workflow) => workflow.id.toString()); } else { - const shared = await Db.collections.SharedWorkflow.find({ - relations: ['workflow'], - where: whereClause({ - user, - entityType: 'workflow', - }), + const active = await Db.collections.Workflow.find({ + select: ['id'], + where: { active: true }, }); - - activeWorkflows = shared.reduce((acc, cur) => { - if (cur.workflow.active) acc.push(cur.workflow); - return acc; - }, []); + const activeIds = active.map((workflow) => workflow.id); + const where = whereClause({ + user, + entityType: 'workflow', + }); + Object.assign(where, { workflowId: In(activeIds) }); + const shared = await Db.collections.SharedWorkflow.find({ + select: ['workflowId'], + where, + }); + return shared.map((id) => id.workflowId.toString()); } - - return activeWorkflows.filter((workflow) => this.activationErrors[workflow.id] === undefined); } /** diff --git a/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts b/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts index adb37dce57..8ed5031628 100644 --- a/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts +++ b/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts @@ -24,8 +24,6 @@ export async function getSharedWorkflowIds(user: User): Promise { select: ['workflowId'], }); return sharedWorkflows.map(({ workflowId }) => workflowId); - - return sharedWorkflows.map(({ workflowId }) => workflowId); } export async function getSharedWorkflow( diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 8739fe9ed1..6ad8ac7da2 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -764,8 +764,7 @@ export class Server extends AbstractServer { this.app.get( `/${this.restEndpoint}/active`, ResponseHelper.send(async (req: WorkflowRequest.GetAllActive) => { - const activeWorkflows = await this.activeWorkflowRunner.getActiveWorkflows(req.user); - return activeWorkflows.map(({ id }) => id); + return this.activeWorkflowRunner.getActiveWorkflows(req.user); }), ); diff --git a/packages/cli/src/UserManagement/PermissionChecker.ts b/packages/cli/src/UserManagement/PermissionChecker.ts index d2327159d4..b8b851419a 100644 --- a/packages/cli/src/UserManagement/PermissionChecker.ts +++ b/packages/cli/src/UserManagement/PermissionChecker.ts @@ -44,6 +44,7 @@ export class PermissionChecker { const workflowSharings = await Db.collections.SharedWorkflow.find({ relations: ['workflow'], where: { workflowId: workflow.id }, + select: ['userId'], }); workflowUserIds = workflowSharings.map((s) => s.userId); }