( userId: string, workflowId?: string, workflowName?: string, workspaceId?: string )
| 130 | } |
| 131 | |
| 132 | export async function resolveWorkflowIdForUser( |
| 133 | userId: string, |
| 134 | workflowId?: string, |
| 135 | workflowName?: string, |
| 136 | workspaceId?: string |
| 137 | ): Promise<WorkflowResolutionResult> { |
| 138 | if (workflowId) { |
| 139 | const authorization = await authorizeWorkflowByWorkspacePermission({ |
| 140 | workflowId, |
| 141 | userId, |
| 142 | action: 'read', |
| 143 | }) |
| 144 | if (!authorization.allowed) { |
| 145 | return { |
| 146 | status: 'not_found', |
| 147 | message: 'No workflows found. Create a workflow first or provide a valid workflowId.', |
| 148 | } |
| 149 | } |
| 150 | const wf = await getWorkflowById(workflowId) |
| 151 | if (!wf?.workspaceId) { |
| 152 | return { |
| 153 | status: 'not_found', |
| 154 | message: 'No workflows found. Create a workflow first or provide a valid workflowId.', |
| 155 | } |
| 156 | } |
| 157 | return { |
| 158 | status: 'resolved', |
| 159 | workflowId, |
| 160 | workspaceId: wf.workspaceId, |
| 161 | workflowName: wf.name || undefined, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | const accessibleRows = await listAccessibleWorkspaceRowsForUser(userId, 'all') |
| 166 | const workspaceIdList = accessibleRows.map((row) => row.workspace.id) |
| 167 | const allowedWorkspaceIds = workspaceId |
| 168 | ? workspaceIdList.filter((candidateWorkspaceId) => candidateWorkspaceId === workspaceId) |
| 169 | : workspaceIdList |
| 170 | if (allowedWorkspaceIds.length === 0) { |
| 171 | return { |
| 172 | status: 'not_found', |
| 173 | message: 'No workflows found. Create a workflow first or provide a valid workflowId.', |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | const workflowRows = await db |
| 178 | .select() |
| 179 | .from(workflowTable) |
| 180 | .where( |
| 181 | and(inArray(workflowTable.workspaceId, allowedWorkspaceIds), isNull(workflowTable.archivedAt)) |
| 182 | ) |
| 183 | .orderBy(asc(workflowTable.sortOrder), asc(workflowTable.createdAt), asc(workflowTable.id)) |
| 184 | |
| 185 | const workflows = workflowRows.filter( |
| 186 | (workflow): workflow is (typeof workflowRows)[number] & { workspaceId: string } => |
| 187 | workflow.workspaceId !== null |
| 188 | ) |
| 189 |
no test coverage detected