permission system - permission formatting
This commit is contained in:
parent
d889f92643
commit
2f5d9d3f01
15 changed files with 352 additions and 18 deletions
10
src/command/permissionCommand.ts
Normal file
10
src/command/permissionCommand.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { PermissionString } from "../type/permissionTypes";
|
||||
|
||||
export interface CreatePermissionCommand {
|
||||
permission: PermissionString;
|
||||
userId: number;
|
||||
}
|
||||
|
||||
export interface DeletePermissionCommand {
|
||||
id: number;
|
||||
}
|
48
src/command/permissionCommandHandler.ts
Normal file
48
src/command/permissionCommandHandler.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
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");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue