This commit is contained in:
Frank
2025-10-02 17:55:54 -04:00
parent 73115efab1
commit a8341e2b8b
10 changed files with 43 additions and 38 deletions

View File

@@ -54,7 +54,7 @@ export namespace Account {
.select(getTableColumns(WorkspaceTable))
.from(WorkspaceTable)
.innerJoin(UserTable, eq(UserTable.workspaceID, WorkspaceTable.id))
.where(and(eq(UserTable.email, actor.properties.email), isNull(WorkspaceTable.timeDeleted)))
.where(and(eq(UserTable.accountID, actor.properties.accountID), isNull(WorkspaceTable.timeDeleted)))
.execute(),
)
}

View File

@@ -1,6 +1,7 @@
import { mysqlTable, uniqueIndex, varchar, int, mysqlEnum } from "drizzle-orm/mysql-core"
import { mysqlTable, uniqueIndex, varchar, int, mysqlEnum, foreignKey } from "drizzle-orm/mysql-core"
import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"
import { workspaceIndexes } from "./workspace.sql"
import { AccountTable } from "./account.sql"
export const UserRole = ["admin", "member"] as const
@@ -22,5 +23,10 @@ export const UserTable = mysqlTable(
...workspaceIndexes(table),
uniqueIndex("user_account_id").on(table.workspaceID, table.accountID),
uniqueIndex("user_email").on(table.workspaceID, table.email),
foreignKey({
columns: [table.accountID],
foreignColumns: [AccountTable.id],
name: "global_account_id",
}),
],
)

View File

@@ -1,5 +1,5 @@
import { z } from "zod"
import { and, eq, isNull, sql } from "drizzle-orm"
import { and, eq, getTableColumns, isNull, sql } from "drizzle-orm"
import { fn } from "./util/fn"
import { Database } from "./drizzle"
import { UserRole, UserTable } from "./schema/user.sql"
@@ -9,6 +9,7 @@ import { render } from "@jsx-email/render"
import { InviteEmail } from "@opencode/console-mail/InviteEmail.jsx"
import { AWS } from "./aws"
import { Account } from "./account"
import { AccountTable } from "./schema/account.sql"
export namespace User {
const assertAdmin = async () => {
@@ -29,8 +30,12 @@ export namespace User {
export const list = fn(z.void(), () =>
Database.use((tx) =>
tx
.select()
.select({
...getTableColumns(UserTable),
accountEmail: AccountTable.email,
})
.from(UserTable)
.leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
.where(and(eq(UserTable.workspaceID, Actor.workspace()), isNull(UserTable.timeDeleted))),
),
)
@@ -159,19 +164,22 @@ export namespace User {
await assertAdmin()
assertNotSelf(id)
return await Database.use(async (tx) => {
const email = await tx
.select({ email: UserTable.email })
.from(UserTable)
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace())))
.then((rows) => rows[0]?.email)
if (!email) throw new Error("User not found")
return await Database.transaction(async (tx) => {
const user = await fromID(id)
if (!user) throw new Error("User not found")
await tx
.update(UserTable)
.set({
oldEmail: email,
email: null,
...(user.email
? {
oldEmail: user.email,
email: null,
}
: {
oldAccountID: user.accountID,
accountID: null,
}),
timeDeleted: sql`now()`,
})
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace())))

View File

@@ -1,7 +1,7 @@
import { z } from "zod"
import { fn } from "./util/fn"
import { Actor } from "./actor"
import { Database, sql } from "./drizzle"
import { Database } from "./drizzle"
import { Identifier } from "./identifier"
import { UserTable } from "./schema/user.sql"
import { BillingTable } from "./schema/billing.sql"
@@ -20,7 +20,6 @@ export namespace Workspace {
workspaceID,
id: Identifier.create("user"),
accountID: account.properties.accountID,
email: account.properties.email,
name: "",
role: "admin",
})
@@ -35,9 +34,7 @@ export namespace Workspace {
{
workspaceID,
},
async () => {
await Key.create({ name: "Default API Key" })
},
() => Key.create({ name: "Default API Key" }),
)
return workspaceID
})