MCPcopy
hub / github.com/simstudioai/sim / getManageableWorkspaces

Function getManageableWorkspaces

apps/sim/lib/workspaces/permissions/utils.ts:448–504  ·  view source on GitHub ↗
(userId: string)

Source from the content-addressed store, hash-verified

446 * }>> - A list of workspaces that the user has access to
447 */
448export async function getManageableWorkspaces(userId: string): Promise<
449 Array<{
450 id: string
451 name: string
452 ownerId: string
453 accessType: 'direct' | 'owner'
454 }>
455> {
456 const ownedWorkspaces = await db
457 .select({
458 id: workspace.id,
459 name: workspace.name,
460 ownerId: workspace.ownerId,
461 })
462 .from(workspace)
463 .where(and(eq(workspace.ownerId, userId), isNull(workspace.archivedAt)))
464
465 const adminWorkspaces = await db
466 .select({
467 id: workspace.id,
468 name: workspace.name,
469 ownerId: workspace.ownerId,
470 })
471 .from(workspace)
472 .innerJoin(permissions, eq(permissions.entityId, workspace.id))
473 .where(
474 and(
475 isNull(workspace.archivedAt),
476 eq(permissions.userId, userId),
477 eq(permissions.entityType, 'workspace'),
478 eq(permissions.permissionType, 'admin')
479 )
480 )
481
482 const orgAdminWorkspaces = (await getOrgAdminWorkspaceRows(userId, 'active')).map((ws) => ({
483 id: ws.id,
484 name: ws.name,
485 ownerId: ws.ownerId,
486 }))
487
488 const ownedSet = new Set(ownedWorkspaces.map((w) => w.id))
489 const seen = new Set(ownedSet)
490 const combined: Array<{
491 id: string
492 name: string
493 ownerId: string
494 accessType: 'direct' | 'owner'
495 }> = ownedWorkspaces.map((ws) => ({ ...ws, accessType: 'owner' as const }))
496
497 for (const ws of [...adminWorkspaces, ...orgAdminWorkspaces]) {
498 if (seen.has(ws.id)) continue
499 seen.add(ws.id)
500 combined.push({ ...ws, accessType: 'direct' as const })
501 }
502
503 return combined
504}

Callers 1

utils.test.tsFile · 0.90

Calls 4

getOrgAdminWorkspaceRowsFunction · 0.90
eqFunction · 0.50
addMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected