diff options
| author | Frank <[email protected]> | 2025-10-03 16:32:53 -0400 |
|---|---|---|
| committer | Frank <[email protected]> | 2025-10-03 16:32:53 -0400 |
| commit | 96718720595026aee814be05e8a29d6d07f30714 (patch) | |
| tree | 9c85a36fa1d3efdbf30bd8eafa421fea17134596 /packages/console/core/src | |
| parent | 6378e6c06f3296ec8fdea13271c092e9830401ea (diff) | |
| download | opencode-96718720595026aee814be05e8a29d6d07f30714.tar.gz opencode-96718720595026aee814be05e8a29d6d07f30714.zip | |
wip: zen
Diffstat (limited to 'packages/console/core/src')
| -rw-r--r-- | packages/console/core/src/schema/user.sql.ts | 2 | ||||
| -rw-r--r-- | packages/console/core/src/user.ts | 110 |
2 files changed, 37 insertions, 75 deletions
diff --git a/packages/console/core/src/schema/user.sql.ts b/packages/console/core/src/schema/user.sql.ts index cce0cb986..121199f72 100644 --- a/packages/console/core/src/schema/user.sql.ts +++ b/packages/console/core/src/schema/user.sql.ts @@ -10,9 +10,7 @@ export const UserTable = mysqlTable( ...workspaceColumns, ...timestamps, accountID: ulid("account_id"), - oldAccountID: ulid("old_account_id"), email: varchar("email", { length: 255 }), - oldEmail: varchar("old_email", { length: 255 }), name: varchar("name", { length: 255 }).notNull(), timeSeen: utc("time_seen"), color: int("color"), diff --git a/packages/console/core/src/user.ts b/packages/console/core/src/user.ts index 590f4427a..58f02b8df 100644 --- a/packages/console/core/src/user.ts +++ b/packages/console/core/src/user.ts @@ -10,6 +10,7 @@ import { AWS } from "./aws" import { Account } from "./account" import { AccountTable } from "./schema/account.sql" import { Key } from "./key" +import { KeyTable } from "./schema/key.sql" export namespace User { const assertAdmin = async () => { @@ -70,66 +71,41 @@ export namespace User { }), async ({ email, role }) => { await assertAdmin() - const workspaceID = Actor.workspace() - await Database.transaction(async (tx) => { - const account = await Account.fromEmail(email) - const existing = await tx - .select() - .from(UserTable) - .where( - and( - eq(UserTable.workspaceID, Actor.workspace()), - account ? eq(UserTable.oldAccountID, account.id) : eq(UserTable.oldEmail, email), - ), - ) - .then((rows) => rows[0]) - - // case: previously invited and removed - if (existing) { - await tx - .update(UserTable) - .set({ + + // create user + const account = await Account.fromEmail(email) + await Database.use((tx) => + tx + .insert(UserTable) + .values({ + id: Identifier.create("user"), + name: "", + ...(account + ? { + accountID: account.id, + } + : { + email, + }), + workspaceID, + role, + }) + .onDuplicateKeyUpdate({ + set: { role, timeDeleted: null, - ...(account - ? { - oldAccountID: null, - accountID: account.id, - } - : { - oldEmail: null, - email, - }), - }) - .where(and(eq(UserTable.workspaceID, existing.workspaceID), eq(UserTable.id, existing.id))) - } - // case: account previously not invited - else { - await tx - .insert(UserTable) - .values({ - id: Identifier.create("user"), - name: "", - ...(account - ? { - accountID: account.id, - } - : { - email, - }), - workspaceID, - role, - }) - .catch((e: any) => { - if (e.message.match(/Duplicate entry '.*' for key 'user.user_account_id'/)) - throw new Error("A user with this email has already been invited.") - if (e.message.match(/Duplicate entry '.*' for key 'user.user_email'/)) - throw new Error("A user with this email has already been invited.") - throw e - }) - } - }) + }, + }), + ) + + // create api key + //if (account) { + // const existing = await Database.use(tx => { + // const key = tx.select().from(KeyTable).where(and(eq(KeyTable.workspaceID, workspaceID), eq(KeyTable, account.id))).then((rows) => rows[0]) + // return key + // }) + //} // send email, ignore errors try { @@ -208,25 +184,13 @@ export namespace User { await assertAdmin() assertNotSelf(id) - return await Database.transaction(async (tx) => { - const user = await fromID(id) - if (!user) throw new Error("User not found") - - await tx + return await Database.use((tx) => + tx .update(UserTable) .set({ - ...(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()))) - }) + .where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace()))), + ) }) } |
