mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 01:34:22 +01:00
wip: zen
This commit is contained in:
@@ -8,7 +8,6 @@ import { AccountTable } from "./schema/account.sql"
|
||||
export namespace Account {
|
||||
export const create = fn(
|
||||
z.object({
|
||||
email: z.string().email(),
|
||||
id: z.string().optional(),
|
||||
}),
|
||||
async (input) =>
|
||||
@@ -16,7 +15,6 @@ export namespace Account {
|
||||
const id = input.id ?? Identifier.create("account")
|
||||
await tx.insert(AccountTable).values({
|
||||
id,
|
||||
email: input.email,
|
||||
})
|
||||
return id
|
||||
}),
|
||||
@@ -32,15 +30,4 @@ export namespace Account {
|
||||
.then((rows) => rows[0])
|
||||
}),
|
||||
)
|
||||
|
||||
export const fromEmail = fn(z.string().email(), async (email) =>
|
||||
Database.transaction(async (tx) => {
|
||||
return tx
|
||||
.select()
|
||||
.from(AccountTable)
|
||||
.where(eq(AccountTable.email, email))
|
||||
.execute()
|
||||
.then((rows) => rows[0])
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ export namespace Billing {
|
||||
const user = Actor.assert("user")
|
||||
const { successUrl, cancelUrl } = input
|
||||
|
||||
const email = await User.getAccountEmail(user.properties.userID)
|
||||
const email = await User.getAuthEmail(user.properties.userID)
|
||||
const customer = await Billing.get()
|
||||
const session = await Billing.stripe().checkout.sessions.create({
|
||||
mode: "payment",
|
||||
|
||||
@@ -4,9 +4,8 @@ import { Actor } from "./actor"
|
||||
import { and, Database, eq, isNull, sql } from "./drizzle"
|
||||
import { Identifier } from "./identifier"
|
||||
import { KeyTable } from "./schema/key.sql"
|
||||
import { AccountTable } from "./schema/account.sql"
|
||||
import { UserTable } from "./schema/user.sql"
|
||||
import { User } from "./user"
|
||||
import { AuthTable } from "./schema/auth.sql"
|
||||
|
||||
export namespace Key {
|
||||
export const list = fn(z.void(), async () => {
|
||||
@@ -18,11 +17,11 @@ export namespace Key {
|
||||
key: KeyTable.key,
|
||||
timeUsed: KeyTable.timeUsed,
|
||||
userID: KeyTable.userID,
|
||||
email: AccountTable.email,
|
||||
email: AuthTable.subject,
|
||||
})
|
||||
.from(KeyTable)
|
||||
.innerJoin(UserTable, and(eq(KeyTable.userID, UserTable.id), eq(KeyTable.workspaceID, UserTable.workspaceID)))
|
||||
.innerJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
|
||||
.innerJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
|
||||
.where(
|
||||
and(
|
||||
...[
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import { mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
|
||||
import { id, timestamps } from "../drizzle/types"
|
||||
|
||||
export const AccountTable = mysqlTable(
|
||||
"account",
|
||||
{
|
||||
id: id(),
|
||||
...timestamps,
|
||||
email: varchar("email", { length: 255 }).notNull(),
|
||||
},
|
||||
(table) => [uniqueIndex("email").on(table.email)],
|
||||
)
|
||||
export const AccountTable = mysqlTable("account", {
|
||||
id: id(),
|
||||
...timestamps,
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
|
||||
import { index, mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
|
||||
import { id, timestamps, ulid } from "../drizzle/types"
|
||||
|
||||
export const AuthProvider = ["email", "github", "google"] as const
|
||||
@@ -12,5 +12,5 @@ export const AuthTable = mysqlTable(
|
||||
subject: varchar("subject", { length: 255 }).notNull(),
|
||||
accountID: ulid("account_id").notNull(),
|
||||
},
|
||||
(table) => [uniqueIndex("provider").on(table.provider, table.subject)],
|
||||
(table) => [uniqueIndex("provider").on(table.provider, table.subject), index("account_id").on(table.accountID)],
|
||||
)
|
||||
|
||||
@@ -7,11 +7,10 @@ import { Actor } from "./actor"
|
||||
import { Identifier } from "./identifier"
|
||||
import { render } from "@jsx-email/render"
|
||||
import { AWS } from "./aws"
|
||||
import { Account } from "./account"
|
||||
import { AccountTable } from "./schema/account.sql"
|
||||
import { Key } from "./key"
|
||||
import { KeyTable } from "./schema/key.sql"
|
||||
import { WorkspaceTable } from "./schema/workspace.sql"
|
||||
import { AuthTable } from "./schema/auth.sql"
|
||||
|
||||
export namespace User {
|
||||
const assertNotSelf = (id: string) => {
|
||||
@@ -24,10 +23,10 @@ export namespace User {
|
||||
tx
|
||||
.select({
|
||||
...getTableColumns(UserTable),
|
||||
accountEmail: AccountTable.email,
|
||||
authEmail: AuthTable.subject,
|
||||
})
|
||||
.from(UserTable)
|
||||
.leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
|
||||
.leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
|
||||
.where(and(eq(UserTable.workspaceID, Actor.workspace()), isNull(UserTable.timeDeleted))),
|
||||
),
|
||||
)
|
||||
@@ -42,14 +41,14 @@ export namespace User {
|
||||
),
|
||||
)
|
||||
|
||||
export const getAccountEmail = fn(z.string(), (id) =>
|
||||
export const getAuthEmail = fn(z.string(), (id) =>
|
||||
Database.use((tx) =>
|
||||
tx
|
||||
.select({
|
||||
email: AccountTable.email,
|
||||
email: AuthTable.subject,
|
||||
})
|
||||
.from(UserTable)
|
||||
.leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
|
||||
.leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
|
||||
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id)))
|
||||
.then((rows) => rows[0]?.email),
|
||||
),
|
||||
@@ -66,16 +65,24 @@ export namespace User {
|
||||
const workspaceID = Actor.workspace()
|
||||
|
||||
// create user
|
||||
const account = await Account.fromEmail(email)
|
||||
const accountID = await Database.use((tx) =>
|
||||
tx
|
||||
.select({
|
||||
accountID: AuthTable.accountID,
|
||||
})
|
||||
.from(AuthTable)
|
||||
.where(and(eq(AuthTable.provider, "email"), eq(AuthTable.subject, email)))
|
||||
.then((rows) => rows[0]?.accountID),
|
||||
)
|
||||
await Database.use((tx) =>
|
||||
tx
|
||||
.insert(UserTable)
|
||||
.values({
|
||||
id: Identifier.create("user"),
|
||||
name: "",
|
||||
...(account
|
||||
...(accountID
|
||||
? {
|
||||
accountID: account.id,
|
||||
accountID,
|
||||
}
|
||||
: {
|
||||
email,
|
||||
@@ -94,12 +101,12 @@ export namespace User {
|
||||
)
|
||||
|
||||
// create api key
|
||||
if (account) {
|
||||
if (accountID) {
|
||||
await Database.use(async (tx) => {
|
||||
const user = await tx
|
||||
.select()
|
||||
.from(UserTable)
|
||||
.where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, account.id)))
|
||||
.where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, accountID)))
|
||||
.then((rows) => rows[0])
|
||||
|
||||
const key = await tx
|
||||
@@ -119,11 +126,11 @@ export namespace User {
|
||||
const emailInfo = await Database.use((tx) =>
|
||||
tx
|
||||
.select({
|
||||
email: AccountTable.email,
|
||||
inviterEmail: AuthTable.subject,
|
||||
workspaceName: WorkspaceTable.name,
|
||||
})
|
||||
.from(UserTable)
|
||||
.innerJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
|
||||
.innerJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
|
||||
.innerJoin(WorkspaceTable, eq(WorkspaceTable.id, workspaceID))
|
||||
.where(
|
||||
and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.id, Actor.assert("user").properties.userID)),
|
||||
@@ -138,7 +145,7 @@ export namespace User {
|
||||
body: render(
|
||||
// @ts-ignore
|
||||
InviteEmail({
|
||||
inviter: emailInfo.email,
|
||||
inviter: emailInfo.inviterEmail,
|
||||
assetsUrl: `https://opencode.ai/email`,
|
||||
workspaceID: workspaceID,
|
||||
workspaceName: emailInfo.workspaceName,
|
||||
|
||||
Reference in New Issue
Block a user