Skip to content

Commit

Permalink
feat(auth-api, auth-portal): Let workspaces have a default set
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitbutler-client committed Jul 22, 2024
1 parent dc2b9a9 commit 0d79192
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "workspaces" ADD COLUMN "is_default" BOOLEAN NOT NULL DEFAULT false;
39 changes: 21 additions & 18 deletions bin/auth-api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bin/auth-api/src/routes/workspace.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions bin/auth-api/src/services/workspaces.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -32,6 +30,7 @@ export async function createWorkspace(
workspaceEnvType: InstanceEnvType,
instanceUrl: string,
displayName: string,
isDefault: boolean,
) {
const newWorkspace = await prisma.workspace.create({
data: {
Expand All @@ -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
});

Expand Down

0 comments on commit 0d79192

Please sign in to comment.