mirror of
https://github.com/Infisical/infisical.git
synced 2026-01-07 22:53:55 -05:00
63 lines
1.3 KiB
TypeScript
63 lines
1.3 KiB
TypeScript
import * as Sentry from '@sentry/node';
|
|
import {
|
|
Workspace,
|
|
Membership,
|
|
Key,
|
|
Secret
|
|
} from '../models';
|
|
|
|
/**
|
|
* Create a workspace with name [name] in organization with id [organizationId]
|
|
* @param {String} name - name of workspace to create.
|
|
* @param {String} organizationId - id of organization to create workspace in
|
|
* @param {Object} workspace - new workspace
|
|
*/
|
|
const createWorkspace = async ({
|
|
name,
|
|
organizationId
|
|
}: {
|
|
name: string;
|
|
organizationId: string;
|
|
}) => {
|
|
let workspace;
|
|
try {
|
|
workspace = await new Workspace({
|
|
name,
|
|
organization: organizationId
|
|
}).save();
|
|
} catch (err) {
|
|
Sentry.setUser(null);
|
|
Sentry.captureException(err);
|
|
throw new Error('Failed to create workspace');
|
|
}
|
|
|
|
return workspace;
|
|
};
|
|
|
|
/**
|
|
* Delete workspace and all associated materials including memberships,
|
|
* secrets, keys, etc.
|
|
* @param {Object} obj
|
|
* @param {String} obj.id - id of workspace to delete
|
|
*/
|
|
const deleteWorkspace = async ({ id }: { id: string }) => {
|
|
try {
|
|
await Workspace.deleteOne({ _id: id });
|
|
await Membership.deleteMany({
|
|
workspace: id
|
|
});
|
|
await Secret.deleteMany({
|
|
workspace: id
|
|
});
|
|
await Key.deleteMany({
|
|
workspace: id
|
|
});
|
|
} catch (err) {
|
|
Sentry.setUser(null);
|
|
Sentry.captureException(err);
|
|
throw new Error('Failed to delete workspace');
|
|
}
|
|
};
|
|
|
|
export { createWorkspace, deleteWorkspace };
|