6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
137 lines
5.1 KiB
TypeScript
137 lines
5.1 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { pinnedItem, settings, type workspace as workspaceTable } from '@sim/db/schema'
|
|
import type { PermissionType } from '@sim/platform-authz/workspace'
|
|
import { and, eq } from 'drizzle-orm'
|
|
import type { PlanCategory } from '@/lib/billing/plan-helpers'
|
|
import {
|
|
evaluateWorkspaceInvitePolicy,
|
|
getInvitePlanCategoryForOrganization,
|
|
getInvitePlanCategoryForUser,
|
|
getWorkspaceCreationPolicy,
|
|
resolveInviteFlags,
|
|
WORKSPACE_MODE,
|
|
type WorkspaceCreationPolicy,
|
|
type WorkspaceInviteFlags,
|
|
} from '@/lib/workspaces/policy'
|
|
import { listAccessibleWorkspaceRowsForUser, type WorkspaceScope } from '@/lib/workspaces/utils'
|
|
|
|
type WorkspaceRow = typeof workspaceTable.$inferSelect
|
|
|
|
/** Accessible workspace row decorated with the viewer's role and invite policy flags. */
|
|
export type WorkspaceWithInviteFlags = WorkspaceRow &
|
|
WorkspaceInviteFlags & {
|
|
role: 'owner' | 'admin' | 'member'
|
|
permissions: PermissionType
|
|
}
|
|
|
|
/** The GET /api/workspaces payload assembled by {@link listWorkspacesForViewer}. */
|
|
export interface WorkspaceListPayload {
|
|
workspaces: WorkspaceWithInviteFlags[]
|
|
lastActiveWorkspaceId: string | null
|
|
/** Workspace ids the viewer pinned to the top of the switcher. */
|
|
pinnedWorkspaceIds: string[]
|
|
creationPolicy: WorkspaceCreationPolicy
|
|
}
|
|
|
|
/**
|
|
* Decorates accessible workspace rows with the viewer's role and per-workspace
|
|
* invite policy flags (resolving each workspace's billed plan category once per
|
|
* billed user / organization).
|
|
*/
|
|
async function buildWorkspacesWithInviteFlags(
|
|
userWorkspaces: Array<{ workspace: WorkspaceRow; permissionType: PermissionType }>,
|
|
userId: string
|
|
): Promise<WorkspaceWithInviteFlags[]> {
|
|
const nonOrgBilledUserIds = [
|
|
...new Set(
|
|
userWorkspaces
|
|
.filter(({ workspace: ws }) => ws.workspaceMode !== WORKSPACE_MODE.ORGANIZATION)
|
|
.map(({ workspace: ws }) => ws.billedAccountUserId)
|
|
),
|
|
]
|
|
const orgIds = [
|
|
...new Set(
|
|
userWorkspaces
|
|
.filter(
|
|
({ workspace: ws }) =>
|
|
ws.workspaceMode === WORKSPACE_MODE.ORGANIZATION && ws.organizationId
|
|
)
|
|
.map(({ workspace: ws }) => ws.organizationId as string)
|
|
),
|
|
]
|
|
const planCategoryByBilledUser = new Map<string, PlanCategory>()
|
|
const planCategoryByOrg = new Map<string, PlanCategory>()
|
|
await Promise.all([
|
|
...nonOrgBilledUserIds.map(async (billedUserId) => {
|
|
planCategoryByBilledUser.set(billedUserId, await getInvitePlanCategoryForUser(billedUserId))
|
|
}),
|
|
...orgIds.map(async (orgId) => {
|
|
planCategoryByOrg.set(orgId, await getInvitePlanCategoryForOrganization(orgId))
|
|
}),
|
|
])
|
|
|
|
return userWorkspaces.map(({ workspace: workspaceDetails, permissionType }) => {
|
|
const billedPlanCategory: PlanCategory =
|
|
workspaceDetails.workspaceMode === WORKSPACE_MODE.ORGANIZATION
|
|
? workspaceDetails.organizationId
|
|
? (planCategoryByOrg.get(workspaceDetails.organizationId) ?? 'free')
|
|
: 'free'
|
|
: (planCategoryByBilledUser.get(workspaceDetails.billedAccountUserId) ?? 'free')
|
|
const invitePolicy = evaluateWorkspaceInvitePolicy(workspaceDetails, { billedPlanCategory })
|
|
|
|
return {
|
|
...workspaceDetails,
|
|
role:
|
|
workspaceDetails.ownerId === userId
|
|
? ('owner' as const)
|
|
: permissionType === 'admin'
|
|
? ('admin' as const)
|
|
: ('member' as const),
|
|
permissions: permissionType,
|
|
...resolveInviteFlags(invitePolicy, workspaceDetails.billedAccountUserId === userId),
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Read-only assembly of the GET /api/workspaces payload for a viewer: accessible
|
|
* workspaces with role/invite flags, the viewer's last active workspace id, and
|
|
* the workspace creation policy.
|
|
*
|
|
* Unlike the route, this performs no writes — no default-workspace creation and
|
|
* no orphaned-workflow repair. It exists for the workspace layout's sidebar
|
|
* prefetch, which only runs after host-context authorization has proven the
|
|
* viewer already has at least one accessible workspace.
|
|
*/
|
|
export async function listWorkspacesForViewer(params: {
|
|
userId: string
|
|
activeOrganizationId: string | null
|
|
scope?: WorkspaceScope
|
|
}): Promise<WorkspaceListPayload> {
|
|
const { userId, activeOrganizationId, scope = 'active' } = params
|
|
|
|
/** Workspace pins ride along here; see `pinnedResourceTypeSchema` for why. */
|
|
const [creationPolicy, workspaces, userSettings, workspacePins] = await Promise.all([
|
|
getWorkspaceCreationPolicy({ userId, activeOrganizationId }),
|
|
listAccessibleWorkspaceRowsForUser(userId, scope).then((rows) =>
|
|
buildWorkspacesWithInviteFlags(rows, userId)
|
|
),
|
|
db
|
|
.select({ lastActiveWorkspaceId: settings.lastActiveWorkspaceId })
|
|
.from(settings)
|
|
.where(eq(settings.userId, userId))
|
|
.limit(1),
|
|
db
|
|
.select({ resourceId: pinnedItem.resourceId })
|
|
.from(pinnedItem)
|
|
.where(and(eq(pinnedItem.userId, userId), eq(pinnedItem.resourceType, 'workspace'))),
|
|
])
|
|
|
|
return {
|
|
workspaces,
|
|
lastActiveWorkspaceId: userSettings[0]?.lastActiveWorkspaceId ?? null,
|
|
pinnedWorkspaceIds: workspacePins.map((row) => row.resourceId),
|
|
creationPolicy,
|
|
}
|
|
}
|