49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
|
import { dataSource } from "../data-source";
|
||
|
import { permission } from "../entity/permission";
|
||
|
import InternalException from "../exceptions/internalException";
|
||
|
import UserService from "../service/userService";
|
||
|
import { CreatePermissionCommand, DeletePermissionCommand } from "./permissionCommand";
|
||
|
|
||
|
export default abstract class PermissionCommandHandler {
|
||
|
/**
|
||
|
* @description grant permission to user
|
||
|
* @param CreatePermissionCommand
|
||
|
* @returns {Promise<number>}
|
||
|
*/
|
||
|
static async create(createPermission: CreatePermissionCommand): Promise<number> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.insert()
|
||
|
.into(permission)
|
||
|
.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 permission");
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description remove permission to user
|
||
|
* @param DeletePermissionCommand
|
||
|
* @returns {Promise<any>}
|
||
|
*/
|
||
|
static async deleteByToken(deletePermission: DeletePermissionCommand): Promise<any> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.delete()
|
||
|
.from(permission)
|
||
|
.where("permission.id = :id", { id: deletePermission.id })
|
||
|
.execute()
|
||
|
.then((res) => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("failed permission removal");
|
||
|
});
|
||
|
}
|
||
|
}
|