Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Remove transactions from delete organization, workspace, user

Tuan Dang7622a3f518f8
diff --git a/backend/src/helpers/organization.ts b/backend/src/helpers/organization.ts
index 2e5ee6ebb1..a527c2c21b 100644
--- a/backend/src/helpers/organization.ts
+++ b/backend/src/helpers/organization.ts
@@ -1,4 +1,4 @@
-import mongoose, { Types, mongo } from "mongoose";
+import { Types } from "mongoose";
import {
Bot,
BotKey,
@@ -55,7 +55,7 @@ import {
import {
createBotOrg
} from "./botOrg";
-import { InternalServerError, ResourceNotFoundError } from "../utils/errors";
+import { ResourceNotFoundError } from "../utils/errors";
/**
* Create an organization with name [name]
@@ -111,311 +111,215 @@ export const createOrganization = async ({
* @returns
*/
export const deleteOrganization = async ({
- organizationId,
- existingSession
+ organizationId
}: {
organizationId: Types.ObjectId;
- existingSession?: mongo.ClientSession;
}) => {
- let session;
-
- if (existingSession) {
- session = existingSession;
- } else {
- session = await mongoose.startSession();
- session.startTransaction();
- }
+ const organization = await Organization.findByIdAndDelete(
+ organizationId
+ );
- try {
- const organization = await Organization.findByIdAndDelete(
- organizationId,
- {
- session
- }
- );
+ if (!organization) throw ResourceNotFoundError();
- if (!organization) throw ResourceNotFoundError();
-
- await MembershipOrg.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await BotOrg.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await SSOConfig.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await Role.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await IncidentContactOrg.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await GitRisks.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await GitAppInstallationSession.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await GitAppOrganizationInstallation.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- const workspaceIds = await Workspace.distinct("_id", {
- organization: organization._id
- });
-
- await Workspace.deleteMany({
- organization: organization._id
- }, {
- session
- });
-
- await Membership.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await MembershipOrg.deleteMany({
+ organization: organization._id
+ });
+
+ await BotOrg.deleteMany({
+ organization: organization._id
+ });
+
+ await SSOConfig.deleteMany({
+ organization: organization._id
+ });
+
+ await Role.deleteMany({
+ organization: organization._id
+ });
- await Key.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await Bot.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await BotKey.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await IncidentContactOrg.deleteMany({
+ organization: organization._id
+ });
+
+ await GitRisks.deleteMany({
+ organization: organization._id
+ });
+
+ await GitAppInstallationSession.deleteMany({
+ organization: organization._id
+ });
+
+ await GitAppOrganizationInstallation.deleteMany({
+ organization: organization._id
+ });
- await SecretBlindIndexData.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await Secret.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await SecretVersion.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ const workspaceIds = await Workspace.distinct("_id", {
+ organization: organization._id
+ });
+
+ await Workspace.deleteMany({
+ organization: organization._id
+ });
+
+ await Membership.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await SecretSnapshot.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await SecretImport.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await Key.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await Bot.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await BotKey.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await Folder.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await SecretBlindIndexData.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await Secret.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await SecretVersion.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await FolderVersion.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await SecretSnapshot.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await SecretImport.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await Webhook.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await Folder.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await TrustedIP.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await Tag.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await FolderVersion.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await IntegrationAuth.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await Webhook.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await Integration.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await TrustedIP.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await Tag.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await ServiceToken.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await IntegrationAuth.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await ServiceTokenData.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await Integration.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await ServiceTokenDataV3.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- await ServiceTokenDataV3Key.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await ServiceToken.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await AuditLog.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await ServiceTokenData.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await Log.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await ServiceTokenDataV3.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
+
+ await ServiceTokenDataV3Key.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await Action.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await AuditLog.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await SecretApprovalPolicy.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
+ await Log.deleteMany({
+ workspace: {
+ $in: workspaceIds
+ }
+ });
- await SecretApprovalRequest.deleteMany({
- workspace: {
- $in: workspaceIds
- }
- }, {
- session
- });
-
- if (organization.customerId) {
- // delete from stripe here
- await licenseServerKeyRequest.delete(
- `${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}`
- );
+ await Action.deleteMany({
+ workspace: {
+ $in: workspaceIds
}
-
- return organization;
- } catch (err) {
- if (!existingSession) {
- await session.abortTransaction();
+ });
+
+ await SecretApprovalPolicy.deleteMany({
+ workspace: {
+ $in: workspaceIds
}
- throw InternalServerError({
- message: "Failed to delete organization"
- });
- } finally {
- if (!existingSession) {
- await session.commitTransaction();
- session.endSession();
+ });
+
+ await SecretApprovalRequest.deleteMany({
+ workspace: {
+ $in: workspaceIds
}
+ });
+
+ if (organization.customerId) {
+ // delete from stripe here
+ await licenseServerKeyRequest.delete(
+ `${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}`
+ );
}
+
+ return organization;
}
/**
diff --git a/backend/src/helpers/user.ts b/backend/src/helpers/user.ts
index b78f471f1d..5c840e906a 100644
--- a/backend/src/helpers/user.ts
+++ b/backend/src/helpers/user.ts
@@ -1,4 +1,4 @@
-import mongoose, { Types, mongo } from "mongoose";
+import { Types } from "mongoose";
import {
APIKeyData,
BackupPrivateKey,
@@ -222,141 +222,92 @@ const checkDeleteUserConditions = async ({
* @returns {User} user - deleted user
*/
export const deleteUser = async ({
- userId,
- existingSession
+ userId
}: {
userId: Types.ObjectId;
- existingSession?: mongo.ClientSession;
}) => {
+
+ const user = await User.findByIdAndDelete(userId);
- let session;
+ if (!user) throw ResourceNotFoundError();
+
+ await checkDeleteUserConditions({
+ userId: user._id
+ });
- if (existingSession) {
- session = existingSession;
- } else {
- session = await mongoose.startSession();
- session.startTransaction();
- }
+ await UserAction.deleteMany({
+ user: user._id
+ });
- try {
- const user = await User.findByIdAndDelete(userId, {
- session
- });
-
- if (!user) throw ResourceNotFoundError();
+ await BackupPrivateKey.deleteMany({
+ user: user._id
+ });
- await checkDeleteUserConditions({
- userId: user._id
- });
-
- await UserAction.deleteMany({
- user: user._id
- }, {
- session
- });
+ await APIKeyData.deleteMany({
+ user: user._id
+ });
- await BackupPrivateKey.deleteMany({
- user: user._id
- }, {
- session
- });
+ await Action.deleteMany({
+ user: user._id
+ });
+
+ await Log.deleteMany({
+ user: user._id
+ });
- await APIKeyData.deleteMany({
- user: user._id
- }, {
- session
- });
+ await TokenVersion.deleteMany({
+ user: user._id
+ });
- await Action.deleteMany({
- user: user._id
- }, {
- session
- });
-
- await Log.deleteMany({
- user: user._id
- }, {
- session
- });
+ await Key.deleteMany({
+ receiver: user._id
+ });
- await TokenVersion.deleteMany({
- user: user._id
- });
+ const membershipOrgs = await MembershipOrg.find({
+ user: userId
+ });
- await Key.deleteMany({
- receiver: user._id
- }, {
- session
+ // delete organizations where user is only member
+ for await (const membershipOrg of membershipOrgs) {
+ const memberCount = await MembershipOrg.countDocuments({
+ organization: membershipOrg.organization
});
+
+ if (memberCount === 1) {
+ // organization only has 1 member (the current user)
- const membershipOrgs = await MembershipOrg.find({
- user: userId
- }, null, {
- session
- });
-
- // delete organizations where user is only member
- for await (const membershipOrg of membershipOrgs) {
- const memberCount = await MembershipOrg.countDocuments({
- organization: membershipOrg.organization
+ await deleteOrganization({
+ organizationId: membershipOrg.organization
});
-
- if (memberCount === 1) {
- // organization only has 1 member (the current user)
-
- await deleteOrganization({
- organizationId: membershipOrg.organization,
- existingSession: session
- });
- }
}
+ }
- const memberships = await Membership.find({
- user: userId
- }, null, {
- session
- });
-
- // delete workspaces where user is only member
- for await (const membership of memberships) {
- const memberCount = await Membership.countDocuments({
- workspace: membership.workspace
- });
-
- if (memberCount === 1) {
- // workspace only has 1 member (the current user) -> delete workspace
-
- await deleteWorkspace({
- workspaceId: membership.workspace,
- existingSession: session
- });
- }
- }
-
- await MembershipOrg.deleteMany({
- user: userId
- }, {
- session
+ const memberships = await Membership.find({
+ user: userId
+ });
+
+ // delete workspaces where user is only member
+ for await (const membership of memberships) {
+ const memberCount = await Membership.countDocuments({
+ workspace: membership.workspace
});
- await Membership.deleteMany({
- user: userId
- }, {
- session
- });
+ if (memberCount === 1) {
+ // workspace only has 1 member (the current user) -> delete workspace
- return user;
- } catch (err) {
- if (!existingSession) {
- await session.abortTransaction();
- }
- throw InternalServerError({
- message: "Failed to delete account"
- })
- } finally {
- if (!existingSession) {
- await session.commitTransaction();
- session.endSession();
+ await deleteWorkspace({
+ workspaceId: membership.workspace
+ });
}
}
+
+ await MembershipOrg.deleteMany({
+ user: userId
+ });
+
+ await Membership.deleteMany({
+ user: userId
+ });
+
+ return user;
}
\ No newline at end of file
diff --git a/backend/src/helpers/workspace.ts b/backend/src/helpers/workspace.ts
index 71d945e785..413b187d13 100644
--- a/backend/src/helpers/workspace.ts
+++ b/backend/src/helpers/workspace.ts
@@ -1,4 +1,4 @@
-import mongoose, { Types, mongo } from "mongoose";
+import { Types } from "mongoose";
import {
Bot,
BotKey,
@@ -33,8 +33,7 @@ import {
import { createBot } from "../helpers/bot";
import { EELicenseService } from "../ee/services";
import { SecretService } from "../services";
-import {
- InternalServerError,
+import {
ResourceNotFoundError
} from "../utils/errors";
@@ -102,189 +101,113 @@ export const createWorkspace = async ({
* @param {String} obj.id - id of workspace to delete
*/
export const deleteWorkspace = async ({
- workspaceId,
- existingSession
+ workspaceId
}: {
workspaceId: Types.ObjectId;
- existingSession?: mongo.ClientSession;
}) => {
-
- let session;
-
- if (existingSession) {
- session = existingSession;
- } else {
- session = await mongoose.startSession();
- session.startTransaction();
- }
+ const workspace = await Workspace.findByIdAndDelete(workspaceId);
- try {
- const workspace = await Workspace.findByIdAndDelete(workspaceId, { session });
-
- if (!workspace) throw ResourceNotFoundError();
-
- await Membership.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
-
- await Key.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
-
- await Bot.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ if (!workspace) throw ResourceNotFoundError();
+
+ await Membership.deleteMany({
+ workspace: workspace._id
+ });
+
+ await Key.deleteMany({
+ workspace: workspace._id
+ });
+
+ await Bot.deleteMany({
+ workspace: workspace._id
+ });
- await BotKey.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await BotKey.deleteMany({
+ workspace: workspace._id
+ });
- await SecretBlindIndexData.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await SecretBlindIndexData.deleteMany({
+ workspace: workspace._id
+ });
- await Secret.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
-
- await SecretVersion.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Secret.deleteMany({
+ workspace: workspace._id
+ });
+
+ await SecretVersion.deleteMany({
+ workspace: workspace._id
+ });
- await SecretSnapshot.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await SecretSnapshot.deleteMany({
+ workspace: workspace._id
+ });
- await SecretImport.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await SecretImport.deleteMany({
+ workspace: workspace._id
+ });
- await Folder.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Folder.deleteMany({
+ workspace: workspace._id
+ });
- await FolderVersion.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await FolderVersion.deleteMany({
+ workspace: workspace._id
+ });
- await Webhook.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Webhook.deleteMany({
+ workspace: workspace._id
+ });
- await TrustedIP.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await TrustedIP.deleteMany({
+ workspace: workspace._id
+ });
- await Tag.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Tag.deleteMany({
+ workspace: workspace._id
+ });
- await IntegrationAuth.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await IntegrationAuth.deleteMany({
+ workspace: workspace._id
+ });
- await Integration.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Integration.deleteMany({
+ workspace: workspace._id
+ });
- await ServiceToken.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await ServiceToken.deleteMany({
+ workspace: workspace._id
+ });
- await ServiceTokenData.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await ServiceTokenData.deleteMany({
+ workspace: workspace._id
+ });
- await ServiceTokenDataV3.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await ServiceTokenDataV3.deleteMany({
+ workspace: workspace._id
+ });
- await ServiceTokenDataV3Key.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await ServiceTokenDataV3Key.deleteMany({
+ workspace: workspace._id
+ });
- await AuditLog.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await AuditLog.deleteMany({
+ workspace: workspace._id
+ });
- await Log.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Log.deleteMany({
+ workspace: workspace._id
+ });
- await Action.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await Action.deleteMany({
+ workspace: workspace._id
+ });
- await SecretApprovalPolicy.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
+ await SecretApprovalPolicy.deleteMany({
+ workspace: workspace._id
+ });
- await SecretApprovalRequest.deleteMany({
- workspace: workspace._id
- }, {
- session
- });
-
- return workspace;
- } catch (err) {
- if (!existingSession) {
- await session.abortTransaction();
- }
- throw InternalServerError({
- message: "Failed to delete organization"
- });
- } finally {
- if (!existingSession) {
- await session.commitTransaction();
- session.endSession();
- }
- }
+ await SecretApprovalRequest.deleteMany({
+ workspace: workspace._id
+ });
+
+ return workspace;
};