48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { dataSource } from "../data-source";
|
|
import { userPermission } from "../entity/user_permission";
|
|
import InternalException from "../exceptions/internalException";
|
|
import UserService from "../service/userService";
|
|
import { CreateUserPermissionCommand, DeleteUserPermissionCommand } from "./userPermissionCommand";
|
|
|
|
export default abstract class UserPermissionCommandHandler {
|
|
/**
|
|
* @description grant permission to user
|
|
* @param CreateUserPermissionCommand
|
|
* @returns {Promise<number>}
|
|
*/
|
|
static async create(createPermission: CreateUserPermissionCommand): Promise<number> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.insert()
|
|
.into(userPermission)
|
|
.values({
|
|
permission: createPermission.permission,
|
|
user: await UserService.getById(createPermission.userId),
|
|
})
|
|
.execute()
|
|
.then((result) => {
|
|
return result.identifiers[0].id;
|
|
})
|
|
.catch((err) => {
|
|
throw new InternalException("Failed saving user permission");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description remove permission to user
|
|
* @param DeleteUserPermissionCommand
|
|
* @returns {Promise<any>}
|
|
*/
|
|
static async deleteByToken(deletePermission: DeleteUserPermissionCommand): Promise<any> {
|
|
return await dataSource
|
|
.createQueryBuilder()
|
|
.delete()
|
|
.from(userPermission)
|
|
.where("permission.id = :id", { id: deletePermission.id })
|
|
.execute()
|
|
.then((res) => {})
|
|
.catch((err) => {
|
|
throw new InternalException("failed user permission removal");
|
|
});
|
|
}
|
|
}
|