This commit is contained in:
Frank
2025-10-06 16:15:10 -04:00
parent 1b17d8070b
commit 9e8fd16e6e
13 changed files with 437 additions and 62 deletions

View File

@@ -1,12 +1,9 @@
import { z } from "zod"
import { and, eq, getTableColumns, isNull } from "drizzle-orm"
import { eq } from "drizzle-orm"
import { fn } from "./util/fn"
import { Database } from "./drizzle"
import { Identifier } from "./identifier"
import { AccountTable } from "./schema/account.sql"
import { Actor } from "./actor"
import { WorkspaceTable } from "./schema/workspace.sql"
import { UserTable } from "./schema/user.sql"
export namespace Account {
export const create = fn(
@@ -46,16 +43,4 @@ export namespace Account {
.then((rows) => rows[0])
}),
)
export const workspaces = async () => {
const actor = Actor.assert("account")
return Database.transaction(async (tx) =>
tx
.select(getTableColumns(WorkspaceTable))
.from(WorkspaceTable)
.innerJoin(UserTable, eq(UserTable.workspaceID, WorkspaceTable.id))
.where(and(eq(UserTable.accountID, actor.properties.accountID), isNull(WorkspaceTable.timeDeleted)))
.execute(),
)
}
}

View File

@@ -20,6 +20,7 @@ export namespace Actor {
properties: {
userID: string
workspaceID: string
accountID: string
}
}
@@ -71,4 +72,12 @@ export namespace Actor {
}
throw new Error(`actor of type "${actor.type}" is not associated with a workspace`)
}
export function account() {
const actor = use()
if ("accountID" in actor.properties) {
return actor.properties.accountID
}
throw new Error(`actor of type "${actor.type}" is not associated with an account`)
}
}

View File

@@ -1,4 +1,4 @@
import { primaryKey, mysqlTable, uniqueIndex, varchar, boolean } from "drizzle-orm/mysql-core"
import { primaryKey, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
import { timestamps, ulid } from "../drizzle/types"
export const WorkspaceTable = mysqlTable(

View File

@@ -172,8 +172,6 @@ export namespace User {
),
),
)
return invitations.length
})
export const updateRole = fn(

View File

@@ -9,34 +9,40 @@ import { WorkspaceTable } from "./schema/workspace.sql"
import { Key } from "./key"
export namespace Workspace {
export const create = fn(z.void(), async () => {
const account = Actor.assert("account")
const workspaceID = Identifier.create("workspace")
const userID = Identifier.create("user")
await Database.transaction(async (tx) => {
await tx.insert(WorkspaceTable).values({
id: workspaceID,
export const create = fn(
z.object({
name: z.string(),
}),
async ({ name }) => {
const account = Actor.assert("account")
const workspaceID = Identifier.create("workspace")
const userID = Identifier.create("user")
await Database.transaction(async (tx) => {
await tx.insert(WorkspaceTable).values({
id: workspaceID,
name,
})
await tx.insert(UserTable).values({
workspaceID,
id: userID,
accountID: account.properties.accountID,
name: "",
role: "admin",
})
await tx.insert(BillingTable).values({
workspaceID,
id: Identifier.create("billing"),
balance: 0,
})
})
await tx.insert(UserTable).values({
workspaceID,
id: userID,
accountID: account.properties.accountID,
name: "",
role: "admin",
})
await tx.insert(BillingTable).values({
workspaceID,
id: Identifier.create("billing"),
balance: 0,
})
})
await Actor.provide(
"system",
{
workspaceID,
},
() => Key.create({ userID, name: "Default API Key" }),
)
return workspaceID
})
await Actor.provide(
"system",
{
workspaceID,
},
() => Key.create({ userID, name: "Default API Key" }),
)
return workspaceID
},
)
}