From ad714b4653ad138c45fd0834742ee1dbc711f5a5 Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Fri, 10 Sep 2021 22:43:51 +0200 Subject: [PATCH] drop commands in dev/tool Signed-off-by: Andrey Platov --- dev/tool/src/index.ts | 20 +++++++++++++++++++- server/account/src/index.ts | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/dev/tool/src/index.ts b/dev/tool/src/index.ts index d9b366e659..78ae988b10 100644 --- a/dev/tool/src/index.ts +++ b/dev/tool/src/index.ts @@ -16,7 +16,7 @@ import { program } from 'commander' import { MongoClient, Db } from 'mongodb' -import { getAccount, createAccount, assignWorkspace, createWorkspace, ACCOUNT_DB } from '@anticrm/account' +import { getAccount, createAccount, assignWorkspace, createWorkspace, ACCOUNT_DB, dropWorkspace, dropAccount } from '@anticrm/account' import { createContributingClient } from '@anticrm/contrib' import core, { TxOperations } from '@anticrm/core' import { encode } from 'jwt-simple' @@ -148,4 +148,22 @@ program }) }) +program + .command('drop-workspace ') + .description('drop workspace') + .action(async (workspace, cmd) => { + return await withDatabase(mongodbUri, async (db) => { + await dropWorkspace(db, workspace) + }) + }) + +program + .command('drop-account ') + .description('drop account') + .action(async (email, cmd) => { + return await withDatabase(mongodbUri, async (db) => { + await dropAccount(db, email) + }) + }) + program.parse(process.argv) diff --git a/server/account/src/index.ts b/server/account/src/index.ts index 05fed154ef..35c969da84 100644 --- a/server/account/src/index.ts +++ b/server/account/src/index.ts @@ -250,6 +250,30 @@ export async function removeWorkspace (db: Db, email: string, workspace: string) await db.collection(ACCOUNT_COLLECTION).updateOne({ _id: accountId }, { $pull: { workspaces: workspaceId } }) } +/** + * @public + */ +export async function dropWorkspace (db: Db, workspace: string): Promise { + const ws = await getWorkspace(db, workspace) + if (ws === null) { + throw new PlatformError(new Status(Severity.ERROR, accountPlugin.status.WorkspaceNotFound, { workspace })) + } + await db.collection(WORKSPACE_COLLECTION).deleteOne({ _id: ws._id }) + await db.collection(ACCOUNT_COLLECTION).updateMany({ _id: { $in: ws.accounts } }, { $pull: { workspaces: ws._id } }) +} + +/** + * @public + */ +export async function dropAccount (db: Db, email: string): Promise { + const account = await getAccount(db, email) + if (account === null) { + throw new PlatformError(new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: email })) + } + await db.collection(ACCOUNT_COLLECTION).deleteOne({ _id: account._id }) + await db.collection(WORKSPACE_COLLECTION).updateMany({ _id: { $in: account.workspaces } }, { $pull: { accounts: account._id } }) +} + function wrap (f: (db: Db, ...args: any[]) => Promise) { return async function (db: Db, request: Request): Promise> { return await f(db, ...request.params)