zen: track region

This commit is contained in:
Frank 2026-06-29 18:57:28 -04:00
commit e9d4ca7e0f
27 changed files with 3343 additions and 9 deletions

View file

@ -1,4 +1,4 @@
import { primaryKey, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
import { json, primaryKey, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
import { timestamps, ulid } from "../drizzle/types"
export const WorkspaceTable = mysqlTable(
@ -7,6 +7,7 @@ export const WorkspaceTable = mysqlTable(
id: ulid("id").notNull().primaryKey(),
slug: varchar("slug", { length: 255 }),
name: varchar("name", { length: 255 }).notNull(),
region: json("region").$type<("us" | "eu" | "sg" | "cn")[]>(),
...timestamps,
},
(table) => [uniqueIndex("slug").on(table.slug)],

View file

@ -11,6 +11,9 @@ import { Key } from "./key"
import { and, eq, isNull, sql } from "drizzle-orm"
export namespace Workspace {
export const Region = z.enum(["us", "eu", "sg", "cn"])
export type Region = z.infer<typeof Region>
export const create = fn(
z.object({
name: z.string().min(1),
@ -57,22 +60,41 @@ export namespace Workspace {
export const update = fn(
z.object({
name: z.string().min(1).max(255),
name: z.string().min(1).max(255).optional(),
region: z.array(Region).min(1).optional(),
}),
async ({ name }) => {
async (input) => {
Actor.assertAdmin()
const workspaceID = Actor.workspace()
return await Database.use((tx) =>
tx
.update(WorkspaceTable)
.set({
name,
...("name" in input ? { name: input.name } : {}),
...("region" in input ? { region: input.region } : {}),
})
.where(eq(WorkspaceTable.id, workspaceID)),
)
},
)
export const setDefaultRegion = fn(
z.object({
country: z.string().optional(),
}),
async (input) => {
const region: Workspace.Region[] =
input.country?.toUpperCase() === "CN" ? ["us", "eu", "sg", "cn"] : ["us", "eu", "sg"]
await Database.use((tx) =>
tx
.update(WorkspaceTable)
.set({ region })
.where(and(eq(WorkspaceTable.id, Actor.workspace()), isNull(WorkspaceTable.region))),
)
return region
},
)
export const remove = fn(z.void(), async () => {
await Database.use((tx) =>
tx