summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core
diff options
context:
space:
mode:
authorFrank <[email protected]>2025-10-02 17:55:54 -0400
committerFrank <[email protected]>2025-10-02 17:55:54 -0400
commita8341e2b8be40f93aa4dddf5349a420a03966fcf (patch)
tree954f074a87949ea6684a65d4210e43d162721f22 /packages/console/core
parent73115efab13840612bc3e6d218679e1bdf6ae3c0 (diff)
downloadopencode-a8341e2b8be40f93aa4dddf5349a420a03966fcf.tar.gz
opencode-a8341e2b8be40f93aa4dddf5349a420a03966fcf.zip
wip: zen
Diffstat (limited to 'packages/console/core')
-rw-r--r--packages/console/core/src/account.ts2
-rw-r--r--packages/console/core/src/schema/user.sql.ts8
-rw-r--r--packages/console/core/src/user.ts30
-rw-r--r--packages/console/core/src/workspace.ts7
4 files changed, 29 insertions, 18 deletions
diff --git a/packages/console/core/src/account.ts b/packages/console/core/src/account.ts
index 3bed2bef1..cd1eed4b1 100644
--- a/packages/console/core/src/account.ts
+++ b/packages/console/core/src/account.ts
@@ -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(),
)
}
diff --git a/packages/console/core/src/schema/user.sql.ts b/packages/console/core/src/schema/user.sql.ts
index e1da69ee6..861c14b47 100644
--- a/packages/console/core/src/schema/user.sql.ts
+++ b/packages/console/core/src/schema/user.sql.ts
@@ -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",
+ }),
],
)
diff --git a/packages/console/core/src/user.ts b/packages/console/core/src/user.ts
index ecf592297..8f00722e6 100644
--- a/packages/console/core/src/user.ts
+++ b/packages/console/core/src/user.ts
@@ -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())))
diff --git a/packages/console/core/src/workspace.ts b/packages/console/core/src/workspace.ts
index e6356e49d..6119f51df 100644
--- a/packages/console/core/src/workspace.ts
+++ b/packages/console/core/src/workspace.ts
@@ -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
})