feat: Add team deletion flow for cloud-hosted (#5717)
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
import { Op } from "sequelize";
|
||||
import { Op, Transaction } from "sequelize";
|
||||
import { Event, User } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { ValidationError } from "../errors";
|
||||
|
||||
export default async function userDestroyer({
|
||||
user,
|
||||
actor,
|
||||
ip,
|
||||
transaction,
|
||||
}: {
|
||||
user: User;
|
||||
actor: User;
|
||||
ip: string;
|
||||
transaction?: Transaction;
|
||||
}) {
|
||||
const { teamId } = user;
|
||||
const usersCount = await User.count({
|
||||
@@ -20,7 +21,9 @@ export default async function userDestroyer({
|
||||
});
|
||||
|
||||
if (usersCount === 1) {
|
||||
throw ValidationError("Cannot delete last user on the team.");
|
||||
throw ValidationError(
|
||||
"Cannot delete last user on the team, delete the workspace instead."
|
||||
);
|
||||
}
|
||||
|
||||
if (user.isAdmin) {
|
||||
@@ -41,33 +44,23 @@ export default async function userDestroyer({
|
||||
}
|
||||
}
|
||||
|
||||
const transaction = await sequelize.transaction();
|
||||
let response;
|
||||
|
||||
try {
|
||||
response = await user.destroy({
|
||||
transaction,
|
||||
});
|
||||
await Event.create(
|
||||
{
|
||||
name: "users.delete",
|
||||
actorId: actor.id,
|
||||
userId: user.id,
|
||||
teamId,
|
||||
data: {
|
||||
name: user.name,
|
||||
},
|
||||
ip,
|
||||
await Event.create(
|
||||
{
|
||||
name: "users.delete",
|
||||
actorId: actor.id,
|
||||
userId: user.id,
|
||||
teamId,
|
||||
data: {
|
||||
name: user.name,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
ip,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
|
||||
return response;
|
||||
return user.destroy({
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user