From 852677d1dc68df8cc4272477ef3542261a69f3e6 Mon Sep 17 00:00:00 2001 From: stack72 Date: Fri, 26 Jul 2024 17:42:57 +0100 Subject: [PATCH] feat(auth-stack): Add an endpoint to be able to create a workspace by userID --- .../src/pages/SetupProductionUser.vue | 43 +++++++++++++++++-- app/auth-portal/src/store/workspaces.store.ts | 12 ++++++ bin/auth-api/src/routes/workspace.routes.ts | 43 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/app/auth-portal/src/pages/SetupProductionUser.vue b/app/auth-portal/src/pages/SetupProductionUser.vue index 24ea7797ff..43578a03d7 100644 --- a/app/auth-portal/src/pages/SetupProductionUser.vue +++ b/app/auth-portal/src/pages/SetupProductionUser.vue @@ -7,6 +7,7 @@ +
Create by User Email
- Create Workspace + Create Workspace For User Email Address + + +
+ Create by User Id - this must be an ID that is in our auth-api database +
+ + + + + + Create Workspace for Know SystemInit UserID @@ -52,18 +77,30 @@ const router = useRouter(); const workspacesStore = useWorkspacesStore(); const featureFlagsStore = useFeatureFlagsStore(); -const invitedUser = { +const invitedUserByEmail = { userEmail: "", }; -const workspace = reactive(_.cloneDeep(invitedUser)); +const invitedUserById = { + userId: "", +}; +const workspace = reactive(_.cloneDeep(invitedUserByEmail)); +const workspaceById = reactive(_.cloneDeep(invitedUserById)); const createWorkspaceReqStatus = workspacesStore.getRequestStatus( "SETUP_PRODUCTION_WORKSPACE", ); +const createWorkspaceByUserIdReqStatus = workspacesStore.getRequestStatus( + "SETUP_PRODUCTION_WORKSPACE_BY_USER_ID", +); const createWorkspace = async () => { await workspacesStore.SETUP_PRODUCTION_WORKSPACE(workspace.userEmail); }; +const createWorkspaceById = async () => { + await workspacesStore.SETUP_PRODUCTION_WORKSPACE_BY_USER_ID( + workspaceById.userId, + ); +}; onMounted(async () => { if (!authStore.userIsLoggedIn) return; diff --git a/app/auth-portal/src/store/workspaces.store.ts b/app/auth-portal/src/store/workspaces.store.ts index e447a95176..6a9134394d 100644 --- a/app/auth-portal/src/store/workspaces.store.ts +++ b/app/auth-portal/src/store/workspaces.store.ts @@ -118,6 +118,18 @@ export const useWorkspacesStore = defineStore("workspaces", { }); }, + async SETUP_PRODUCTION_WORKSPACE_BY_USER_ID(userId: string) { + return new ApiRequest<{ + newWorkspace: Workspace; + }>({ + method: "post", + url: "/workspaces/setup-production-workspace-by-userid", + params: { + userId, + }, + }); + }, + async DELETE_WORKSPACE(workspaceId: WorkspaceId) { return new ApiRequest({ method: "delete", diff --git a/bin/auth-api/src/routes/workspace.routes.ts b/bin/auth-api/src/routes/workspace.routes.ts index afda3a7760..058fb20b48 100644 --- a/bin/auth-api/src/routes/workspace.routes.ts +++ b/bin/auth-api/src/routes/workspace.routes.ts @@ -124,6 +124,49 @@ router.post("/workspaces/setup-production-workspace", async (ctx) => { } }); +router.post("/workspaces/setup-production-workspace-by-userid", async (ctx) => { + if (!ctx.state.authUser) { + throw new ApiError("Unauthorized", "You are not logged in"); + } + + if (!ctx.state.authUser.email.includes("@systeminit.com")) { + throw new ApiError( + "Forbidden", + "You are not allowed to perform this operation", + ); + } + + const reqBody = validate( + ctx.request.body, + z.object({ + userId: z.string(), + }), + ); + + const user = await getUserById(reqBody.userId); + if (user) { + const userWorkspaces = await getUserWorkspaces(user.id); + const hasDefaultWorkspace = _.head( + _.filter( + userWorkspaces, + (w) => w.isDefault && w.creatorUserId === user.id, + ), + ); + + const workspaceDetails = await createWorkspace( + user, + InstanceEnvType.SI, + "https://app.systeminit.com", + `${user.nickname}'s Production Workspace`, + hasDefaultWorkspace === null || hasDefaultWorkspace === undefined, + ); + + ctx.body = { + newWorkspace: workspaceDetails, + }; + } +}); + router.post("/workspaces/new", async (ctx) => { // user must be logged in if (!ctx.state.authUser) {