Skip to content

Commit

Permalink
Merge pull request #4193 from systeminit/set-default-workspace
Browse files Browse the repository at this point in the history
feat(auth-api): Let workspaces have a default set
  • Loading branch information
stack72 authored Jul 23, 2024
2 parents dc2b9a9 + b75289f commit 9d5e092
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 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
9 changes: 8 additions & 1 deletion bin/auth-api/src/routes/workspace.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ router.post("/workspaces/new", async (ctx) => {
displayName: z.string(),
}));

// isDefaultWorkspace: z.boolean(),
let workspaceEnvType;
if (reqBody.instanceUrl === "https://app.systeminit.com") {
workspaceEnvType = InstanceEnvType.SI;
Expand All @@ -92,7 +93,13 @@ 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,
false,
);

ctx.body = {
workspaces: await getUserWorkspaces(ctx.state.authUser.id),
Expand Down
1 change: 1 addition & 0 deletions bin/auth-api/src/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export async function createOrUpdateUserFromAuth0Details(
InstanceEnvType.LOCAL,
"http://localhost:8080",
`${user.nickname}'s Dev Workspace`,
false,
);
}

Expand Down
3 changes: 3 additions & 0 deletions bin/auth-api/src/services/workspaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function createWorkspace(
workspaceEnvType: InstanceEnvType,
instanceUrl: string,
displayName: string,
isDefault: boolean,
) {
const newWorkspace = await prisma.workspace.create({
data: {
Expand All @@ -41,12 +42,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
2 changes: 1 addition & 1 deletion bin/auth-api/test/auth-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ t.test('Auth routes', async () => {
if (!user) {
t.bailout("User Fetch has failed");
}
const workspace = await createWorkspace(user!, InstanceEnvType.SI, "https://app.systeminit.com", `${user!.nickname}'s Testing Workspace`);
const workspace = await createWorkspace(user!, InstanceEnvType.SI, "https://app.systeminit.com", `${user!.nickname}'s Testing Workspace`, false);
const sdfToken = createSdfAuthToken(authData.userId, workspace.id);
await request.get('/whoami')
.set('cookie', `si-auth=${sdfToken}`)
Expand Down
2 changes: 1 addition & 1 deletion bin/auth-api/test/helpers/dummy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function createDummyUser(options?: {
await saveTosAgreement(user, LATEST_TOS_VERSION_ID, '1.2.3.4');
}

const workspace = await createWorkspace(user, InstanceEnvType.SI, "https://app.systeminit.com", `${user.nickname}'s Testing Workspace`);
const workspace = await createWorkspace(user, InstanceEnvType.SI, "https://app.systeminit.com", `${user.nickname}'s Testing Workspace`, false);

return { user, workspace };
}

0 comments on commit 9d5e092

Please sign in to comment.