From 0d79192c6462ede824175fa9628e1a05d66e79ea Mon Sep 17 00:00:00 2001 From: GitButler Date: Mon, 22 Jul 2024 21:32:12 +0100 Subject: [PATCH] feat(auth-api, auth-portal): Let workspaces have a default set This is the first step of the work for this and it's related to the auth-api - we don't ever set this value right now, we just have a notion of it in the system --- .../migration.sql | 2 + bin/auth-api/prisma/schema.prisma | 39 ++++++++++--------- bin/auth-api/src/routes/workspace.routes.ts | 2 +- .../src/services/workspaces.service.ts | 7 ++-- 4 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 bin/auth-api/prisma/migrations/20240722190334_default_workspace/migration.sql diff --git a/bin/auth-api/prisma/migrations/20240722190334_default_workspace/migration.sql b/bin/auth-api/prisma/migrations/20240722190334_default_workspace/migration.sql new file mode 100644 index 0000000000..f91ef51b85 --- /dev/null +++ b/bin/auth-api/prisma/migrations/20240722190334_default_workspace/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "workspaces" ADD COLUMN "is_default" BOOLEAN NOT NULL DEFAULT false; diff --git a/bin/auth-api/prisma/schema.prisma b/bin/auth-api/prisma/schema.prisma index 5d165d68b3..eafc3d2809 100644 --- a/bin/auth-api/prisma/schema.prisma +++ b/bin/auth-api/prisma/schema.prisma @@ -6,18 +6,18 @@ generator client { } datasource db { - provider = "postgresql" - url = env("DATABASE_URL") - directUrl = env("DIRECT_DATABASE_URL") + provider = "postgresql" + url = env("DATABASE_URL") + directUrl = env("DIRECT_DATABASE_URL") } model User { /// SI's id for the user (ULID) id String @id @db.Char(26) /// Auth0's id - auth0Id String? @unique @map("auth0_id") + auth0Id String? @unique @map("auth0_id") /// raw json blob of Auth0 data - auth0Details Json? @map("auth0_details") + auth0Details Json? @map("auth0_details") /// single name string we can use as label for the user nickname String? /// user's email @@ -45,7 +45,7 @@ model User { /// array of workspaces the user created CreatedWorkspaces Workspace[] /// array of the workspaces that the user has access to - WorkspaceMembers WorkspaceMembers[] + WorkspaceMembers WorkspaceMembers[] TosAgreement TosAgreement[] @@index(fields: [email]) @@ -76,28 +76,31 @@ model Workspace { /// secret token for the workspace (ULID) token String? @db.Char(26) + /// Whether the workspace is the default or not + isDefault Boolean @default(false) @map("is_default") + @@index(fields: [creatorUserId]) @@map("workspaces") } model WorkspaceMembers { - id String @id @db.Char(26) + id String @id @db.Char(26) - // id of the User - userId String @map("user_id") - user User @relation(fields: [userId], references: [id]) + // id of the User + userId String @map("user_id") + user User @relation(fields: [userId], references: [id]) - // id of the Workspace - workspaceId String @map("workspace_id") - workspace Workspace @relation(fields: [workspaceId], references: [id]) + // id of the Workspace + workspaceId String @map("workspace_id") + workspace Workspace @relation(fields: [workspaceId], references: [id]) - // Role of the user - roleType RoleType @map("role_type") + // Role of the user + roleType RoleType @map("role_type") - // Invitation to workspace date - invitedAt DateTime? @map("invited_at") + // Invitation to workspace date + invitedAt DateTime? @map("invited_at") - @@unique([userId, workspaceId]) + @@unique([userId, workspaceId]) } model TosAgreement { diff --git a/bin/auth-api/src/routes/workspace.routes.ts b/bin/auth-api/src/routes/workspace.routes.ts index 46ccba8dfa..54ae0a7d82 100644 --- a/bin/auth-api/src/routes/workspace.routes.ts +++ b/bin/auth-api/src/routes/workspace.routes.ts @@ -92,7 +92,7 @@ router.post("/workspaces/new", async (ctx) => { } const workspaceDetails = await - createWorkspace(ctx.state.authUser, workspaceEnvType, reqBody.instanceUrl, reqBody.displayName); + createWorkspace(ctx.state.authUser, workspaceEnvType, reqBody.instanceUrl, reqBody.displayName, reqBody.isDefaultWorkspace); ctx.body = { workspaces: await getUserWorkspaces(ctx.state.authUser.id), diff --git a/bin/auth-api/src/services/workspaces.service.ts b/bin/auth-api/src/services/workspaces.service.ts index eba45fe541..e8530eb636 100644 --- a/bin/auth-api/src/services/workspaces.service.ts +++ b/bin/auth-api/src/services/workspaces.service.ts @@ -1,7 +1,5 @@ import _ from "lodash"; -import { - InstanceEnvType, PrismaClient, User, RoleType, -} from "@prisma/client"; +import { InstanceEnvType, PrismaClient, User, RoleType } from "@prisma/client"; import { ulid } from "ulidx"; import { tracker } from "../lib/tracker"; import { createInvitedUser, getUserByEmail, UserId } from "./users.service"; @@ -32,6 +30,7 @@ export async function createWorkspace( workspaceEnvType: InstanceEnvType, instanceUrl: string, displayName: string, + isDefault: boolean, ) { const newWorkspace = await prisma.workspace.create({ data: { @@ -41,12 +40,14 @@ export async function createWorkspace( instanceUrl, displayName, creatorUserId: creatorUser.id, + isDefault, }, }); tracker.trackEvent(creatorUser, "create_workspace", { workspaceId: newWorkspace.id, instanceUrl, instanceEnvType: newWorkspace.instanceEnvType, + isDefaultWorkspace: newWorkspace.isDefault, // TODO: track env type and other data when it becomes useful });