Core Functions
Workspace Creation
createWorkspaceForTenant
Creates a new workspace within an organization and automatically adds the creator as an owner.Configuration object for workspace creation
The created workspace object containing:
id: Unique workspace identifier (format:workspace_*)name: Workspace display nameslug: URL-friendly slugdomain: Associated domaintenantId: Parent organization IDschedule: Cron schedule (null initially)enabledProviders: JSON array of enabled AI providerscreatedAt: Timestamp of creationdeletedAt: Soft deletion timestamp (null for active workspaces)
addWorkspaceToExistingOrg
Adds a new workspace to an existing organization. Verifies user membership before creating the workspace.The newly created workspace object
ValidationErrorif user is not a member of the organization
checkIsFirstWorkspace
Checks if this is the user’s first workspace (used for onboarding flows).User ID to check
true if the user has no active workspace memberships, false otherwiseWorkspace Queries
getWorkspaceById
Retrieves a workspace by its ID. Only returns active (non-deleted) workspaces.Workspace ID to retrieve
Complete workspace object with all fields
ValidationErrorif workspaceId is empty or undefinedNotFoundErrorif workspace doesn’t exist or is deleted
getWorkspacesForUser
Fetches all workspaces within a specific organization that the user is a member of.Array of workspaces where the user is an active member
getAllWorkspacesForUser
Retrieves all workspaces across all organizations for a user, grouped by organization.User ID
Array of objects, each containing:
organization: Organization metadata (id, name, slug)workspaces: Array of workspaces in that organization
getWorkspaceMembersWithUsers
Fetches all members of a workspace with their user details.Workspace ID
Array of member objects with:
memberId: Workspace member record IDuserId: User IDrole: “owner” or “member”joinedAt: Membership creation timestampuserName: User’s display nameuserEmail: User’s email addressuserImage: User’s profile image URL
getWorkspaceJoinInfo
Generates join codes and metadata for inviting users to a workspace.Workspace ID
Contains:
orgCode: Organization slug or ID for joiningworkspaceCode: Workspace ID (globally unique)organization: Organization metadataworkspace: Workspace metadata
Member Management
addMemberToWorkspace
Adds a user to a workspace with a specified role.Contains workspaceId, userId, and assigned role
ValidationErrorif user is already a member
removeMemberFromWorkspace
Removes a member from a workspace (soft delete). Prevents removing the last owner.NotFoundErrorif member not foundValidationErrorif attempting to remove the last owner
addMemberToWorkspaceByEmail
Adds a user to a workspace by their email address. Creates organization membership if needed.Union type with one of:
{ status: "added", workspaceId, userId, role }- User was added{ status: "already-member", workspaceId, userId }- Already a member{ status: "not-found" }- User account doesn’t exist
joinWorkspaceByCode
Join a workspace using an invite code. Supports organization codes, workspace codes, or combined codes.Union type:
{ status: "joined", organization, workspace }- Successfully joined{ status: "select-workspace", organization, workspaces[] }- Multiple workspaces available
- Direct workspace ID:
workspace_abc123 - Organization slug:
acme-corp(prompts workspace selection if multiple exist) - Combined format:
acme-corp/marketing-team
Workspace Settings
updateWorkspaceDetails
Updates workspace name and domain. Automatically resets analysis if branding changes.workspace: Updated workspace objectanalysisReset: boolean indicating if analysis was cleared
updateOrganizationName
Updates the organization name and generates a new slug scoped to the workspace.Updated organization object with new name and slug
setWorkspaceEnabledProviders
Configures which AI providers are enabled for prompt execution.The updated array of enabled providers
updateWorkspaceSchedule
Sets or clears the cron schedule for automatic prompt execution.The updated schedule value
pg_cron extension. Failures to configure cron are non-fatal and logged as warnings.
Account Management
deleteUserAccount
Permanently deletes a user account and cleans up associated workspaces and organizations.User ID to delete
- If user is the sole owner of an organization:
- Soft-deletes all workspaces in that org
- Soft-deletes all workspace members
- Deletes the organization (cascades member and invitation rows)
- If user is one of multiple owners:
- Only removes the user’s membership
- Leaves organization and workspaces intact
- Soft-deletes remaining workspace memberships
- Deletes user account (cascades sessions and auth records)
Usage in tRPC Routers
Example fromapps/web/src/server/api/routers/workspace/_routes/protectedRoutes.ts:
Type Definitions
All types are exported from@oneglanse/types:
Source Files
Workspace service functions are implemented in:packages/services/src/workspace/create.ts- Workspace creation functionspackages/services/src/workspace/query.ts- Query and retrieval functionspackages/services/src/workspace/members.ts- Member management functionspackages/services/src/workspace/settings.ts- Settings and configurationpackages/services/src/workspace/account.ts- Account deletion logic
packages/services/src/workspace/index.ts.