chore: Refactor user activation to command

This commit is contained in:
Tom Moor
2022-08-18 11:24:27 +02:00
parent f620a9d34c
commit f32f07cdcc
5 changed files with 105 additions and 17 deletions

View File

@@ -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,

View 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);
});
});

View 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,
}
);
});
}