workspace.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { z } from "zod"
  2. import { fn } from "./util/fn"
  3. import { Actor } from "./actor"
  4. import { Database, sql } from "./drizzle"
  5. import { Identifier } from "./identifier"
  6. import { UserTable } from "./schema/user.sql"
  7. import { BillingTable } from "./schema/billing.sql"
  8. import { WorkspaceTable } from "./schema/workspace.sql"
  9. import { Key } from "./key"
  10. export namespace Workspace {
  11. export const create = fn(z.void(), async () => {
  12. const account = Actor.assert("account")
  13. const workspaceID = Identifier.create("workspace")
  14. await Database.transaction(async (tx) => {
  15. await tx.insert(WorkspaceTable).values({
  16. id: workspaceID,
  17. })
  18. await tx.insert(UserTable).values({
  19. workspaceID,
  20. id: Identifier.create("user"),
  21. email: account.properties.email,
  22. name: "",
  23. role: "admin",
  24. })
  25. await tx.insert(BillingTable).values({
  26. workspaceID,
  27. id: Identifier.create("billing"),
  28. balance: 0,
  29. })
  30. })
  31. await Actor.provide(
  32. "system",
  33. {
  34. workspaceID,
  35. },
  36. async () => {
  37. await Key.create({ name: "Default API Key" })
  38. },
  39. )
  40. return workspaceID
  41. })
  42. }