Skip to content

Commit

Permalink
Merge pull request #4234 from systeminit/feat(auth-stack)--Add-an-end…
Browse files Browse the repository at this point in the history
…point-to-be-able-to-create-a-workspace-by-userID

feat(auth-stack): Add an endpoint to be able to create a workspace by userID
  • Loading branch information
stack72 authored Jul 26, 2024
2 parents 26564c1 + 852677d commit 78d62c0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
43 changes: 40 additions & 3 deletions app/auth-portal/src/pages/SetupProductionUser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</div>
</div>

<div>Create by User Email</div>
<Stack>
<ErrorMessage :requestStatus="createWorkspaceReqStatus" />
<VormInput
Expand All @@ -25,7 +26,31 @@
variant="solid"
@click="createWorkspace()"
>
Create Workspace
Create Workspace For User Email Address
</VButton>
</Stack>
<div class="pt-4">
Create by User Id - this must be an ID that is in our auth-api database
</div>
<Stack>
<ErrorMessage :requestStatus="createWorkspaceByUserIdReqStatus" />
<VormInput
v-model="workspaceById.userId"
label="User ID"
placeholder="The user id of the user to invite"
required
:maxLength="500"
/>

<VButton
iconRight="chevron--right"
:requestStatus="createWorkspaceByUserIdReqStatus"
loadingText="Creating..."
tone="action"
variant="solid"
@click="createWorkspaceById()"
>
Create Workspace for Know SystemInit UserID
</VButton>
</Stack>
</template>
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions app/auth-portal/src/store/workspaces.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions bin/auth-api/src/routes/workspace.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 78d62c0

Please sign in to comment.