Skip to content

Commit

Permalink
[Poke] Plugin to downgrade a list of workspaces to NO_PLAN
Browse files Browse the repository at this point in the history
Description
---
Fraudulent workspaces are often found by batches. Manually swiching
each of them to NO_PLAN can be a pain.

This plugin allows deleting a list of workspaces

Risk
---
User error to downgrade wrong workspaces. Mitigated by warning message

Deploy
---
front
  • Loading branch information
philipperolet committed Dec 10, 2024
1 parent 3500423 commit 6332852
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions front/lib/api/poke/plugins/global/batch_downgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Err, Ok } from "@dust-tt/types";

import { createPlugin } from "@app/lib/api/poke/types";
import { Workspace } from "@app/lib/models/workspace";
import { Op } from "sequelize";
import { internalSubscribeWorkspaceToFreeNoPlan } from "@app/lib/plans/subscription";
import { launchScheduleWorkspaceScrubWorkflow } from "@app/temporal/scrub_workspace/client";

export const batchDowngradePlugin = createPlugin(
{
id: "batch-downgrade",
name: "Batch Downgrade Workspaces",
description:
"Downgrade a list of workspaces to NO_PLAN, used to block access to fraudulent users",
resourceTypes: ["global"],
args: {
sIds: {
type: "string",
label: "Workspace sIds",
description:
"Comma-separated list of sIds of the workspaces to downgrade",
},
},
},
async (_1, _2, args) => {
const { sIds } = args;

const sIdsArray = sIds.split(",").map((sId) => sId.trim());

const workspaces = await Workspace.findAll({
where: {
sId: {
[Op.in]: sIdsArray,
},
},
});

const missingWorkspaces = sIdsArray.filter(
(sId) => !workspaces.some((w) => w.sId === sId)
);

if (missingWorkspaces.length > 0) {
return new Err(
new Error(`Workspaces not found: ${missingWorkspaces.join(", ")}`)
);
}

for (const workspace of workspaces) {
await internalSubscribeWorkspaceToFreeNoPlan({
workspaceId: workspace.sId,
});

// On downgrade, start a worklflow to pause all connectors + scrub the data after a specified retention period.
await launchScheduleWorkspaceScrubWorkflow({
workspaceId: workspace.sId,
});
}

return new Ok({
display: "text",
value: `Workspaces downgraded.`,
});
}
);
1 change: 1 addition & 0 deletions front/lib/api/poke/plugins/global/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./create_workspace";
export * from "./batch_downgrade";

0 comments on commit 6332852

Please sign in to comment.