Skip to content

Commit

Permalink
Merge pull request #4524 from systeminit/fix(auth-portal)-Track-Auth-…
Browse files Browse the repository at this point in the history
…API-Events-More-Consistently

fix(auth-portal): Track Auth API Events More Consistently
  • Loading branch information
stack72 authored Sep 9, 2024
2 parents 7a71503 + c2faf62 commit 582da93
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 148 deletions.
6 changes: 0 additions & 6 deletions app/auth-portal/src/pages/WorkspaceDetailsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,6 @@ const inviteButtonHandler = async () => {
const res = await workspacesStore.INVITE_USER(newMember, props.workspaceId);
if (res.result.success) {
tracker.trackEvent("workspace_invitation", {
inviteeEmail: newMember.email,
workspaceId: props.workspaceId,
inviterName: authStore.invitersName,
workspaceName: draftWorkspace.displayName,
});
newMember.email = "";
}
};
Expand Down
148 changes: 148 additions & 0 deletions bin/auth-api/src/routes/admin.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import _ from "lodash";
import { z } from "zod";
import { InstanceEnvType } from "@prisma/client";
import { getUserByEmail, getUserById } from "../services/users.service";
import {
createWorkspace,
getUserWorkspaces,
patchWorkspace,
SAAS_WORKSPACE_URL,
} from "../services/workspaces.service";
import { validate } from "../lib/validation-helpers";
import { tracker } from "../lib/tracker";
import { extractWorkspaceIdParam } from "./workspace.routes";
import { extractAdminAuthUser, router } from ".";

export type WorkspaceLookup = {
firstName?: string | null;
lastName?: string | null;
email?: string | null;
displayName: string;
instanceUrl: string | null;
};
router.get("/workspaces/admin-lookup/:workspaceId", async (ctx) => {
// Just for authorization, result is discarded
extractAdminAuthUser(ctx);

const workspace = await extractWorkspaceIdParam(ctx);
const user = await getUserById(workspace.creatorUserId);

const workspaceDetails: WorkspaceLookup = {
firstName: user?.firstName,
lastName: user?.lastName,
email: user?.email,
displayName: workspace.displayName,
instanceUrl: workspace.instanceUrl,
};

ctx.body = workspaceDetails;
});

router.post("/workspaces/setup-production-workspace", async (ctx) => {
// Just for authorization, result is discarded
extractAdminAuthUser(ctx);

const reqBody = validate(
ctx.request.body,
z.object({
userEmail: z.string(),
}),
);

const user = await getUserByEmail(reqBody.userEmail);
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,
SAAS_WORKSPACE_URL,
`${user.nickname}'s Production Workspace`,
hasDefaultWorkspace === null || hasDefaultWorkspace === undefined,
);

ctx.body = {
newWorkspace: workspaceDetails,
};
}
});

router.post("/workspaces/setup-production-workspace-by-userid", async (ctx) => {
// Just for authorization, result is discarded
extractAdminAuthUser(ctx);

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,
SAAS_WORKSPACE_URL,
`${user.nickname}'s Production Workspace`,
hasDefaultWorkspace === null || hasDefaultWorkspace === undefined,
);

ctx.body = {
newWorkspace: workspaceDetails,
};
}
});

router.patch("/workspaces/:workspaceId/quarantine", async (ctx) => {
const authUser = extractAdminAuthUser(ctx);

const workspace = await extractWorkspaceIdParam(ctx);

const reqBody = validate(
ctx.request.body,
z.object({
isQuarantined: z.boolean(),
}),
);

const quarantineDate = new Date();
if (reqBody.isQuarantined) {
tracker.trackEvent(authUser, "quarantine_workspace", {
quarantinedBy: authUser.email,
quarantinedAt: quarantineDate,
workspaceId: workspace.id,
});
} else {
tracker.trackEvent(authUser, "unquarantine_workspace", {
unQuarantinedBy: authUser.email,
unQuarantinedAt: quarantineDate,
workspaceId: workspace.id,
});
}

const quarantinedAt = reqBody.isQuarantined ? quarantineDate : null;

await patchWorkspace(
workspace.id,
workspace.instanceUrl,
workspace.displayName,
quarantinedAt,
);

ctx.body = await getUserWorkspaces(authUser.id);
});
Loading

0 comments on commit 582da93

Please sign in to comment.