chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { db } from '@sim/db'
|
||||
import { member, organization } from '@sim/db/schema'
|
||||
import { generateId } from '@sim/utils/id'
|
||||
import { and, eq, ne } from 'drizzle-orm'
|
||||
|
||||
const ORGANIZATION_SLUG_REGEX = /^[a-z0-9-_]+$/
|
||||
|
||||
export class OrganizationSlugInvalidError extends Error {
|
||||
constructor(slug: string) {
|
||||
super(`Organization slug "${slug}" is invalid`)
|
||||
this.name = 'OrganizationSlugInvalidError'
|
||||
}
|
||||
}
|
||||
|
||||
export class OrganizationSlugTakenError extends Error {
|
||||
constructor(slug: string) {
|
||||
super(`Organization slug "${slug}" is already taken`)
|
||||
this.name = 'OrganizationSlugTakenError'
|
||||
}
|
||||
}
|
||||
|
||||
interface CreateOrganizationWithOwnerParams {
|
||||
ownerUserId: string
|
||||
name: string
|
||||
slug: string
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
interface EnsureOrganizationSlugAvailableParams {
|
||||
slug: string
|
||||
excludeOrganizationId?: string
|
||||
}
|
||||
|
||||
interface CreateOrganizationWithOwnerResult {
|
||||
organizationId: string
|
||||
memberId: string
|
||||
}
|
||||
|
||||
export function validateOrganizationSlugOrThrow(slug: string): void {
|
||||
if (!ORGANIZATION_SLUG_REGEX.test(slug)) {
|
||||
throw new OrganizationSlugInvalidError(slug)
|
||||
}
|
||||
}
|
||||
|
||||
export async function ensureOrganizationSlugAvailable({
|
||||
slug,
|
||||
excludeOrganizationId,
|
||||
}: EnsureOrganizationSlugAvailableParams): Promise<void> {
|
||||
const whereClause = excludeOrganizationId
|
||||
? and(eq(organization.slug, slug), ne(organization.id, excludeOrganizationId))
|
||||
: eq(organization.slug, slug)
|
||||
|
||||
const existingOrganization = await db
|
||||
.select({ id: organization.id })
|
||||
.from(organization)
|
||||
.where(whereClause)
|
||||
.limit(1)
|
||||
|
||||
if (existingOrganization.length > 0) {
|
||||
throw new OrganizationSlugTakenError(slug)
|
||||
}
|
||||
}
|
||||
|
||||
export async function createOrganizationWithOwner({
|
||||
ownerUserId,
|
||||
name,
|
||||
slug,
|
||||
metadata = {},
|
||||
}: CreateOrganizationWithOwnerParams): Promise<CreateOrganizationWithOwnerResult> {
|
||||
validateOrganizationSlugOrThrow(slug)
|
||||
|
||||
const organizationId = `org_${generateId()}`
|
||||
const memberId = generateId()
|
||||
const now = new Date()
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
const whereClause = eq(organization.slug, slug)
|
||||
const existingOrganization = await tx
|
||||
.select({ id: organization.id })
|
||||
.from(organization)
|
||||
.where(whereClause)
|
||||
.limit(1)
|
||||
|
||||
if (existingOrganization.length > 0) {
|
||||
throw new OrganizationSlugTakenError(slug)
|
||||
}
|
||||
|
||||
await tx.insert(organization).values({
|
||||
id: organizationId,
|
||||
name,
|
||||
slug,
|
||||
metadata,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
|
||||
await tx.insert(member).values({
|
||||
id: memberId,
|
||||
userId: ownerUserId,
|
||||
organizationId,
|
||||
role: 'owner',
|
||||
createdAt: now,
|
||||
})
|
||||
})
|
||||
|
||||
return {
|
||||
organizationId,
|
||||
memberId,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user