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

118 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-09-02 15:57:13 +02:00
import { DeleteResult, EntityManager, InsertResult } 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-09-02 15:57:13 +02:00
import { PermissionString } from "../type/permissionTypes";
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);
2024-09-10 17:11:15 +02:00
if (newPermissions.length != 0) {
await this.updatePermissionsAdd(manager, updateUserPermissions.userId, newPermissions);
}
if (removePermissions.length != 0) {
await this.updatePermissionsRemove(manager, updateUserPermissions.userId, removePermissions);
}
2024-09-01 14:55:05 +02:00
})
.then(() => {})
.catch((err) => {
2024-09-06 10:08:19 +02:00
throw new InternalException("Failed saving user permissions", err);
2024-09-01 14:55:05 +02:00
});
}
2024-09-02 15:57:13 +02:00
private static async updatePermissionsAdd(
manager: EntityManager,
userId: number,
2024-09-06 10:08:19 +02:00
permissions: Array<PermissionString>
2024-09-02 15:57:13 +02:00
): Promise<InsertResult> {
return await manager
.createQueryBuilder()
.insert()
.into(userPermission)
2024-09-06 10:08:19 +02:00
.values(
permissions.map((p) => ({
permission: p,
userId: userId,
}))
)
.orIgnore()
2024-09-02 15:57:13 +02:00
.execute();
2024-09-01 14:55:05 +02:00
}
private static async updatePermissionsRemove(
manager: EntityManager,
userId: number,
2024-09-06 10:08:19 +02:00
permissions: Array<PermissionString>
2024-09-02 15:57:13 +02:00
): Promise<DeleteResult> {
return await manager
.createQueryBuilder()
.delete()
.from(userPermission)
.where("userId = :id", { id: userId })
2024-09-06 10:08:19 +02:00
.andWhere("permission IN (:...permission)", { permission: permissions })
2024-09-02 15:57:13 +02:00
.execute();
2024-09-01 14:55:05 +02:00
}
/**
* @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-09-06 10:08:19 +02:00
throw new InternalException("Failed saving user permission", err);
});
}
/**
* @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-09-06 10:08:19 +02:00
throw new InternalException("failed user permission removal", err);
});
}
}