opencode/packages/console/core/src/user.ts

204 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-08-29 04:44:55 +08:00
import { z } from "zod"
2025-10-09 06:59:41 +08:00
import { and, eq, getTableColumns, isNull, sql } from "drizzle-orm"
2025-08-29 04:44:55 +08:00
import { fn } from "./util/fn"
import { Database } from "./drizzle"
2025-10-03 01:58:38 +08:00
import { UserRole, UserTable } from "./schema/user.sql"
import { Actor } from "./actor"
import { Identifier } from "./identifier"
import { render } from "@jsx-email/render"
import { AWS } from "./aws"
import { Account } from "./account"
2025-10-03 05:55:54 +08:00
import { AccountTable } from "./schema/account.sql"
2025-10-04 00:54:52 +08:00
import { Key } from "./key"
2025-10-04 04:32:53 +08:00
import { KeyTable } from "./schema/key.sql"
2025-08-29 04:44:55 +08:00
export namespace User {
2025-10-09 06:59:41 +08:00
const assertAdmin = () => {
if (Actor.userRole() === "admin") return
throw new Error(`Expected admin user, got ${Actor.userRole()}`)
2025-10-03 01:58:38 +08:00
}
const assertNotSelf = (id: string) => {
2025-10-09 06:59:41 +08:00
if (Actor.userID() !== id) return
throw new Error(`Expected not self actor, got self actor`)
2025-10-03 01:58:38 +08:00
}
export const list = fn(z.void(), () =>
Database.use((tx) =>
tx
2025-10-03 05:55:54 +08:00
.select({
...getTableColumns(UserTable),
accountEmail: AccountTable.email,
})
2025-08-29 04:44:55 +08:00
.from(UserTable)
2025-10-03 05:55:54 +08:00
.leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
2025-10-03 01:58:38 +08:00
.where(and(eq(UserTable.workspaceID, Actor.workspace()), isNull(UserTable.timeDeleted))),
),
)
export const fromID = fn(z.string(), (id) =>
Database.use((tx) =>
tx
.select()
.from(UserTable)
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id), isNull(UserTable.timeDeleted)))
.then((rows) => rows[0]),
),
)
2025-10-03 19:45:35 +08:00
export const getAccountEmail = fn(z.string(), (id) =>
Database.use((tx) =>
tx
.select({
email: AccountTable.email,
})
.from(UserTable)
.leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id)))
.then((rows) => rows[0]?.email),
),
)
2025-10-03 01:58:38 +08:00
export const invite = fn(
z.object({
email: z.string(),
role: z.enum(UserRole),
}),
async ({ email, role }) => {
2025-10-09 06:59:41 +08:00
assertAdmin()
2025-10-03 01:58:38 +08:00
const workspaceID = Actor.workspace()
2025-10-04 04:32:53 +08:00
// 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: {
2025-10-04 00:54:52 +08:00
role,
timeDeleted: null,
2025-10-04 04:32:53 +08:00
},
}),
)
// create api key
2025-10-04 13:12:22 +08:00
if (account) {
await Database.use(async (tx) => {
const user = await tx
.select()
.from(UserTable)
.where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, account.id)))
.then((rows) => rows[0])
const key = await tx
.select()
.from(KeyTable)
.where(and(eq(KeyTable.workspaceID, workspaceID), eq(KeyTable.userID, user.id)))
.then((rows) => rows[0])
if (key) return
await Key.create({ userID: user.id, name: "Default API Key" })
})
}
2025-10-03 01:58:38 +08:00
// send email, ignore errors
try {
2025-10-05 09:33:39 +08:00
const { InviteEmail } = await import("@opencode-ai/console-mail/InviteEmail.jsx")
2025-10-03 01:58:38 +08:00
await AWS.sendEmail({
to: email,
subject: `You've been invited to join the ${workspaceID} workspace on OpenCode Zen`,
body: render(
// @ts-ignore
InviteEmail({
assetsUrl: `https://opencode.ai/email`,
workspace: workspaceID,
}),
),
})
} catch (e) {
console.error(e)
}
},
)
2025-10-04 00:54:52 +08:00
export const joinInvitedWorkspaces = fn(z.void(), async () => {
const account = Actor.assert("account")
const invitations = await Database.use(async (tx) => {
const invitations = await tx
.select({
id: UserTable.id,
workspaceID: UserTable.workspaceID,
})
.from(UserTable)
.where(eq(UserTable.email, account.properties.email))
await tx
.update(UserTable)
.set({
accountID: account.properties.accountID,
email: null,
})
.where(eq(UserTable.email, account.properties.email))
return invitations
})
await Promise.all(
invitations.map((invite) =>
Actor.provide(
2025-10-04 13:12:22 +08:00
"system",
2025-10-04 00:54:52 +08:00
{
workspaceID: invite.workspaceID,
},
2025-10-04 13:12:22 +08:00
() => Key.create({ userID: invite.id, name: "Default API Key" }),
2025-10-04 00:54:52 +08:00
),
),
)
})
2025-10-07 21:17:05 +08:00
export const update = fn(
2025-10-03 01:58:38 +08:00
z.object({
id: z.string(),
role: z.enum(UserRole),
2025-10-07 21:17:05 +08:00
monthlyLimit: z.number().nullable(),
2025-08-29 04:44:55 +08:00
}),
2025-10-07 21:17:05 +08:00
async ({ id, role, monthlyLimit }) => {
2025-10-09 06:59:41 +08:00
assertAdmin()
2025-10-03 01:58:38 +08:00
if (role === "member") assertNotSelf(id)
return await Database.use((tx) =>
tx
.update(UserTable)
2025-10-07 21:17:05 +08:00
.set({ role, monthlyLimit })
2025-10-03 01:58:38 +08:00
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace()))),
)
},
2025-08-29 04:44:55 +08:00
)
2025-10-03 01:58:38 +08:00
export const remove = fn(z.string(), async (id) => {
2025-10-09 06:59:41 +08:00
assertAdmin()
2025-10-03 01:58:38 +08:00
assertNotSelf(id)
2025-10-04 04:32:53 +08:00
return await Database.use((tx) =>
tx
2025-10-03 01:58:38 +08:00
.update(UserTable)
.set({
timeDeleted: sql`now()`,
})
2025-10-04 04:32:53 +08:00
.where(and(eq(UserTable.id, id), eq(UserTable.workspaceID, Actor.workspace()))),
)
2025-10-03 01:58:38 +08:00
})
2025-08-29 04:44:55 +08:00
}