ff-admin-server/src/command/userPermissionCommandHandler.ts

97 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-09-01 14:55:05 +02:00
import { EntityManager } from "typeorm";
import { dataSource } from "../data-source";
2024-09-01 14:55:05 +02:00
import { user } from "../entity/user";
2024-08-27 17:54:59 +02:00
import { userPermission } from "../entity/user_permission";
import InternalException from "../exceptions/internalException";
2024-09-01 14:55:05 +02:00
import {
CreateUserPermissionCommand,
DeleteUserPermissionCommand,
UpdateUserPermissionsCommand,
} from "./userPermissionCommand";
import UserPermissionService from "../service/userPermissionService";
import PermissionHelper from "../helpers/permissionHelper";
2024-08-27 17:54:59 +02:00
export default abstract class UserPermissionCommandHandler {
2024-09-01 14:55:05 +02:00
/**
* @description update user permissions
* @param UpdateUserPermissionsCommand
* @returns {Promise<void>}
*/
static async updatePermissions(updateUserPermissions: UpdateUserPermissionsCommand): Promise<void> {
let currentPermissions = (await UserPermissionService.getByUser(updateUserPermissions.userId)).map(
(r) => r.permission
);
return await dataSource.manager
.transaction(async (manager) => {
let newPermissions = PermissionHelper.getWhatToAdd(currentPermissions, updateUserPermissions.permissions);
let removePermissions = PermissionHelper.getWhatToRemove(currentPermissions, updateUserPermissions.permissions);
for (let permission of newPermissions) {
await this.updatePermissionsAdd(manager, updateUserPermissions.userId, permission);
}
for (let permission of removePermissions) {
await this.updatePermissionsRemove(manager, updateUserPermissions.userId, permission);
}
})
.then(() => {})
.catch((err) => {
throw new InternalException("Failed saving user permissions");
});
}
private static async updatePermissionsAdd(manager: EntityManager, userId: number, permission: string): Promise<void> {
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).add(permission);
}
private static async updatePermissionsRemove(
manager: EntityManager,
userId: number,
permission: string
): Promise<void> {
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).remove(permission);
}
/**
* @description grant permission to user
2024-08-27 17:54:59 +02:00
* @param CreateUserPermissionCommand
* @returns {Promise<number>}
*/
2024-08-27 17:54:59 +02:00
static async create(createPermission: CreateUserPermissionCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
2024-08-27 17:54:59 +02:00
.into(userPermission)
.values({
permission: createPermission.permission,
2024-09-01 14:55:05 +02:00
userId: createPermission.userId,
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
2024-08-27 17:54:59 +02:00
throw new InternalException("Failed saving user permission");
});
}
/**
* @description remove permission to user
2024-08-27 17:54:59 +02:00
* @param DeleteUserPermissionCommand
* @returns {Promise<any>}
*/
2024-08-28 20:41:16 +02:00
static async delete(deletePermission: DeleteUserPermissionCommand): Promise<any> {
return await dataSource
.createQueryBuilder()
.delete()
2024-08-27 17:54:59 +02:00
.from(userPermission)
2024-09-01 14:55:05 +02:00
.where("userId = :id", { id: deletePermission.userId })
.andWhere("permission = :permission", { permission: deletePermission.permission })
.execute()
.then((res) => {})
.catch((err) => {
2024-08-27 17:54:59 +02:00
throw new InternalException("failed user permission removal");
});
}
}