Remove transactions from delete organization, workspace, user

This commit is contained in:
Tuan Dang
2023-11-01 11:06:36 +02:00
parent 1be46b5e57
commit 7622a3f518
3 changed files with 366 additions and 588 deletions
+4 -100
View File
@@ -1,4 +1,4 @@
import mongoose, { Types, mongo } from "mongoose"; import { Types } from "mongoose";
import { import {
Bot, Bot,
BotKey, BotKey,
@@ -55,7 +55,7 @@ import {
import { import {
createBotOrg createBotOrg
} from "./botOrg"; } from "./botOrg";
import { InternalServerError, ResourceNotFoundError } from "../utils/errors"; import { ResourceNotFoundError } from "../utils/errors";
/** /**
* Create an organization with name [name] * Create an organization with name [name]
@@ -111,78 +111,47 @@ export const createOrganization = async ({
* @returns * @returns
*/ */
export const deleteOrganization = async ({ export const deleteOrganization = async ({
organizationId, organizationId
existingSession
}: { }: {
organizationId: Types.ObjectId; organizationId: Types.ObjectId;
existingSession?: mongo.ClientSession;
}) => { }) => {
let session;
if (existingSession) {
session = existingSession;
} else {
session = await mongoose.startSession();
session.startTransaction();
}
try {
const organization = await Organization.findByIdAndDelete( const organization = await Organization.findByIdAndDelete(
organizationId, organizationId
{
session
}
); );
if (!organization) throw ResourceNotFoundError(); if (!organization) throw ResourceNotFoundError();
await MembershipOrg.deleteMany({ await MembershipOrg.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await BotOrg.deleteMany({ await BotOrg.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await SSOConfig.deleteMany({ await SSOConfig.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await Role.deleteMany({ await Role.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await IncidentContactOrg.deleteMany({ await IncidentContactOrg.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await GitRisks.deleteMany({ await GitRisks.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await GitAppInstallationSession.deleteMany({ await GitAppInstallationSession.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await GitAppOrganizationInstallation.deleteMany({ await GitAppOrganizationInstallation.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
const workspaceIds = await Workspace.distinct("_id", { const workspaceIds = await Workspace.distinct("_id", {
@@ -191,208 +160,156 @@ export const deleteOrganization = async ({
await Workspace.deleteMany({ await Workspace.deleteMany({
organization: organization._id organization: organization._id
}, {
session
}); });
await Membership.deleteMany({ await Membership.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Key.deleteMany({ await Key.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Bot.deleteMany({ await Bot.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await BotKey.deleteMany({ await BotKey.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretBlindIndexData.deleteMany({ await SecretBlindIndexData.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Secret.deleteMany({ await Secret.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretVersion.deleteMany({ await SecretVersion.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretSnapshot.deleteMany({ await SecretSnapshot.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretImport.deleteMany({ await SecretImport.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Folder.deleteMany({ await Folder.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await FolderVersion.deleteMany({ await FolderVersion.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Webhook.deleteMany({ await Webhook.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await TrustedIP.deleteMany({ await TrustedIP.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Tag.deleteMany({ await Tag.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await IntegrationAuth.deleteMany({ await IntegrationAuth.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Integration.deleteMany({ await Integration.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await ServiceToken.deleteMany({ await ServiceToken.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await ServiceTokenData.deleteMany({ await ServiceTokenData.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await ServiceTokenDataV3.deleteMany({ await ServiceTokenDataV3.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await ServiceTokenDataV3Key.deleteMany({ await ServiceTokenDataV3Key.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await AuditLog.deleteMany({ await AuditLog.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Log.deleteMany({ await Log.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await Action.deleteMany({ await Action.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretApprovalPolicy.deleteMany({ await SecretApprovalPolicy.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
await SecretApprovalRequest.deleteMany({ await SecretApprovalRequest.deleteMany({
workspace: { workspace: {
$in: workspaceIds $in: workspaceIds
} }
}, {
session
}); });
if (organization.customerId) { if (organization.customerId) {
@@ -403,19 +320,6 @@ export const deleteOrganization = async ({
} }
return organization; return organization;
} catch (err) {
if (!existingSession) {
await session.abortTransaction();
}
throw InternalServerError({
message: "Failed to delete organization"
});
} finally {
if (!existingSession) {
await session.commitTransaction();
session.endSession();
}
}
} }
/** /**
+5 -54
View File
@@ -1,4 +1,4 @@
import mongoose, { Types, mongo } from "mongoose"; import { Types } from "mongoose";
import { import {
APIKeyData, APIKeyData,
BackupPrivateKey, BackupPrivateKey,
@@ -222,26 +222,12 @@ const checkDeleteUserConditions = async ({
* @returns {User} user - deleted user * @returns {User} user - deleted user
*/ */
export const deleteUser = async ({ export const deleteUser = async ({
userId, userId
existingSession
}: { }: {
userId: Types.ObjectId; userId: Types.ObjectId;
existingSession?: mongo.ClientSession;
}) => { }) => {
let session; const user = await User.findByIdAndDelete(userId);
if (existingSession) {
session = existingSession;
} else {
session = await mongoose.startSession();
session.startTransaction();
}
try {
const user = await User.findByIdAndDelete(userId, {
session
});
if (!user) throw ResourceNotFoundError(); if (!user) throw ResourceNotFoundError();
@@ -251,32 +237,22 @@ export const deleteUser = async ({
await UserAction.deleteMany({ await UserAction.deleteMany({
user: user._id user: user._id
}, {
session
}); });
await BackupPrivateKey.deleteMany({ await BackupPrivateKey.deleteMany({
user: user._id user: user._id
}, {
session
}); });
await APIKeyData.deleteMany({ await APIKeyData.deleteMany({
user: user._id user: user._id
}, {
session
}); });
await Action.deleteMany({ await Action.deleteMany({
user: user._id user: user._id
}, {
session
}); });
await Log.deleteMany({ await Log.deleteMany({
user: user._id user: user._id
}, {
session
}); });
await TokenVersion.deleteMany({ await TokenVersion.deleteMany({
@@ -285,14 +261,10 @@ export const deleteUser = async ({
await Key.deleteMany({ await Key.deleteMany({
receiver: user._id receiver: user._id
}, {
session
}); });
const membershipOrgs = await MembershipOrg.find({ const membershipOrgs = await MembershipOrg.find({
user: userId user: userId
}, null, {
session
}); });
// delete organizations where user is only member // delete organizations where user is only member
@@ -305,16 +277,13 @@ export const deleteUser = async ({
// organization only has 1 member (the current user) // organization only has 1 member (the current user)
await deleteOrganization({ await deleteOrganization({
organizationId: membershipOrg.organization, organizationId: membershipOrg.organization
existingSession: session
}); });
} }
} }
const memberships = await Membership.find({ const memberships = await Membership.find({
user: userId user: userId
}, null, {
session
}); });
// delete workspaces where user is only member // delete workspaces where user is only member
@@ -327,36 +296,18 @@ export const deleteUser = async ({
// workspace only has 1 member (the current user) -> delete workspace // workspace only has 1 member (the current user) -> delete workspace
await deleteWorkspace({ await deleteWorkspace({
workspaceId: membership.workspace, workspaceId: membership.workspace
existingSession: session
}); });
} }
} }
await MembershipOrg.deleteMany({ await MembershipOrg.deleteMany({
user: userId user: userId
}, {
session
}); });
await Membership.deleteMany({ await Membership.deleteMany({
user: userId user: userId
}, {
session
}); });
return user; return user;
} catch (err) {
if (!existingSession) {
await session.abortTransaction();
}
throw InternalServerError({
message: "Failed to delete account"
})
} finally {
if (!existingSession) {
await session.commitTransaction();
session.endSession();
}
}
} }
+3 -80
View File
@@ -1,4 +1,4 @@
import mongoose, { Types, mongo } from "mongoose"; import { Types } from "mongoose";
import { import {
Bot, Bot,
BotKey, BotKey,
@@ -34,7 +34,6 @@ import { createBot } from "../helpers/bot";
import { EELicenseService } from "../ee/services"; import { EELicenseService } from "../ee/services";
import { SecretService } from "../services"; import { SecretService } from "../services";
import { import {
InternalServerError,
ResourceNotFoundError ResourceNotFoundError
} from "../utils/errors"; } from "../utils/errors";
@@ -102,189 +101,113 @@ export const createWorkspace = async ({
* @param {String} obj.id - id of workspace to delete * @param {String} obj.id - id of workspace to delete
*/ */
export const deleteWorkspace = async ({ export const deleteWorkspace = async ({
workspaceId, workspaceId
existingSession
}: { }: {
workspaceId: Types.ObjectId; workspaceId: Types.ObjectId;
existingSession?: mongo.ClientSession;
}) => { }) => {
const workspace = await Workspace.findByIdAndDelete(workspaceId);
let session;
if (existingSession) {
session = existingSession;
} else {
session = await mongoose.startSession();
session.startTransaction();
}
try {
const workspace = await Workspace.findByIdAndDelete(workspaceId, { session });
if (!workspace) throw ResourceNotFoundError(); if (!workspace) throw ResourceNotFoundError();
await Membership.deleteMany({ await Membership.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Key.deleteMany({ await Key.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Bot.deleteMany({ await Bot.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await BotKey.deleteMany({ await BotKey.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretBlindIndexData.deleteMany({ await SecretBlindIndexData.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Secret.deleteMany({ await Secret.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretVersion.deleteMany({ await SecretVersion.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretSnapshot.deleteMany({ await SecretSnapshot.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretImport.deleteMany({ await SecretImport.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Folder.deleteMany({ await Folder.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await FolderVersion.deleteMany({ await FolderVersion.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Webhook.deleteMany({ await Webhook.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await TrustedIP.deleteMany({ await TrustedIP.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Tag.deleteMany({ await Tag.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await IntegrationAuth.deleteMany({ await IntegrationAuth.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Integration.deleteMany({ await Integration.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await ServiceToken.deleteMany({ await ServiceToken.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await ServiceTokenData.deleteMany({ await ServiceTokenData.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await ServiceTokenDataV3.deleteMany({ await ServiceTokenDataV3.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await ServiceTokenDataV3Key.deleteMany({ await ServiceTokenDataV3Key.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await AuditLog.deleteMany({ await AuditLog.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Log.deleteMany({ await Log.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await Action.deleteMany({ await Action.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretApprovalPolicy.deleteMany({ await SecretApprovalPolicy.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
await SecretApprovalRequest.deleteMany({ await SecretApprovalRequest.deleteMany({
workspace: workspace._id workspace: workspace._id
}, {
session
}); });
return workspace; return workspace;
} catch (err) {
if (!existingSession) {
await session.abortTransaction();
}
throw InternalServerError({
message: "Failed to delete organization"
});
} finally {
if (!existingSession) {
await session.commitTransaction();
session.endSession();
}
}
}; };