(userId: string)
| 446 | * }>> - A list of workspaces that the user has access to |
| 447 | */ |
| 448 | export 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 | } |
no test coverage detected