chore: Refactor user activation to command
This commit is contained in:
@@ -10,6 +10,10 @@ type Props = {
|
||||
ip: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* This command suspends an active user, this will cause them to lose access to
|
||||
* the team.
|
||||
*/
|
||||
export default async function userSuspender({
|
||||
user,
|
||||
actorId,
|
||||
|
||||
46
server/commands/userUnsuspender.test.ts
Normal file
46
server/commands/userUnsuspender.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { buildAdmin, buildUser } from "@server/test/factories";
|
||||
import { getTestDatabase } from "@server/test/support";
|
||||
import userUnsuspender from "./userUnsuspender";
|
||||
|
||||
const db = getTestDatabase();
|
||||
|
||||
afterAll(db.disconnect);
|
||||
|
||||
beforeEach(db.flush);
|
||||
|
||||
describe("userUnsuspender", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should not allow unsuspending self", async () => {
|
||||
const user = await buildUser();
|
||||
let error;
|
||||
|
||||
try {
|
||||
await userUnsuspender({
|
||||
actorId: user.id,
|
||||
user,
|
||||
ip,
|
||||
});
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("Unable to unsuspend the current user");
|
||||
});
|
||||
|
||||
it("should unsuspend the user", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const user = await buildUser({
|
||||
teamId: admin.teamId,
|
||||
suspendedAt: new Date(),
|
||||
suspendedById: admin.id,
|
||||
});
|
||||
await userUnsuspender({
|
||||
actorId: admin.id,
|
||||
user,
|
||||
ip,
|
||||
});
|
||||
expect(user.suspendedAt).toEqual(null);
|
||||
expect(user.suspendedById).toEqual(null);
|
||||
});
|
||||
});
|
||||
49
server/commands/userUnsuspender.ts
Normal file
49
server/commands/userUnsuspender.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import { User, Event } from "@server/models";
|
||||
import { ValidationError } from "../errors";
|
||||
|
||||
type Props = {
|
||||
user: User;
|
||||
actorId: string;
|
||||
ip: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* This command unsuspends a previously suspended user, allowing access to the
|
||||
* team again.
|
||||
*/
|
||||
export default async function userUnsuspender({
|
||||
user,
|
||||
actorId,
|
||||
ip,
|
||||
}: Props): Promise<void> {
|
||||
if (user.id === actorId) {
|
||||
throw ValidationError("Unable to unsuspend the current user");
|
||||
}
|
||||
|
||||
await sequelize.transaction(async (transaction: Transaction) => {
|
||||
await user.update(
|
||||
{
|
||||
suspendedById: null,
|
||||
suspendedAt: null,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
await Event.create(
|
||||
{
|
||||
name: "users.activate",
|
||||
actorId,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
data: {
|
||||
name: user.name,
|
||||
},
|
||||
ip,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user