import { EntityManager } from "typeorm"; import { dataSource } from "../data-source"; import { user } from "../entity/user"; import InternalException from "../exceptions/internalException"; import { CreateUserCommand, DeleteUserCommand, UpdateUserCommand, UpdateUserRolesCommand } from "./userCommand"; import UserService from "../service/userService"; export default abstract class UserCommandHandler { /** * @description create user * @param CreateUserCommand * @returns {Promise} */ static async create(createUser: CreateUserCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(user) .values({ username: createUser.username, mail: createUser.mail, firstname: createUser.firstname, lastname: createUser.lastname, secret: createUser.secret, }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed saving user"); }); } /** * @description update user * @param UpdateUserCommand * @returns {Promise} */ static async update(updateUser: UpdateUserCommand): Promise { return await dataSource .createQueryBuilder() .update(user) .set({ mail: updateUser.mail, firstname: updateUser.firstname, lastname: updateUser.lastname, username: updateUser.username, }) .where("id = :id", { id: updateUser.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating user"); }); } /** * @description update user roles * @param UpdateUserRolesCommand * @returns {Promise} */ static async updateRoles(updateUserRoles: UpdateUserRolesCommand): Promise { let currentRoles = (await UserService.getAssignedRolesByUserId(updateUserRoles.id)).map((r) => r.id); return await dataSource.manager .transaction(async (manager) => { let newRoles = updateUserRoles.roleIds.filter((r) => !currentRoles.includes(r)); let removeRoles = currentRoles.filter((r) => !updateUserRoles.roleIds.includes(r)); for (let role of newRoles) { await this.updateRolesAdd(manager, updateUserRoles.id, role); } for (let role of removeRoles) { await this.updateRolesRemove(manager, updateUserRoles.id, role); } }) .then(() => {}) .catch((err) => { throw new InternalException("Failed saving user roles"); }); } private static async updateRolesAdd(manager: EntityManager, userId: number, roleId: number): Promise { return await manager.createQueryBuilder().relation(user, "roles").of(userId).add(roleId); } private static async updateRolesRemove(manager: EntityManager, userId: number, roleId: number): Promise { return await manager.createQueryBuilder().relation(user, "roles").of(userId).remove(roleId); } /** * @description delete user * @param DeleteUserCommand * @returns {Promise} */ static async delete(deleteUser: DeleteUserCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(user) .where("id = :id", { id: deleteUser.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting user"); }); } }