import { Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { NewPermissionInput , UpdatePermissionInput , } from '../../schema/graphql.schema' ; import { Repository } from 'typeorm' ; import { PermissionNotFoundException , PermissionDeleteNotAllowedException , } from '../exception/permission.exception' ; import Permission from '../entity/permission.entity' ; import UserPermission from '../entity/userPermission.entity' ; import GroupPermission from '../entity/groupPermission.entity' ; import PermissionCacheService from './permissioncache.service' ; @ Injectable ( ) export class PermissionService { constructor ( @ InjectRepository ( Permission ) private permissionsRepository , @ InjectRepository ( UserPermission ) private userPermissionsRepository , @ InjectRepository ( GroupPermission ) private groupPermissionRepository , private permissionCacheService ) { } getAllPermissions ( ) { return this . permissionsRepository . find ( ) ; } async getPermissionById ( id ) { const permission = await this . permissionsRepository . findOne ( id ) ; if ( permission ) { return permission ; } throw new PermissionNotFoundException ( id ) ; } async createPermission ( permission ) { const newPermission = await this . permissionsRepository . create ( permission ) ; const result = await this . permissionsRepository . insert ( newPermission ) ; return { ... newPermission , id : result . raw [ 0 ] . id } ; } async updatePermission ( id , permission ) { const existingPermission = await this . permissionsRepository . findOne ( id ) ; if ( ! existingPermission ) { throw new PermissionNotFoundException ( id ) ; } const permissionToUpdate = this . permissionsRepository . create ( permission ) ; await this . permissionsRepository . update ( id , permissionToUpdate ) ; return { ... existingPermission , ... permissionToUpdate , } ; } async deletePermission ( id ) { const existingPermission = await this . permissionsRepository . findOne ( id ) ; if ( ! existingPermission ) { throw new PermissionNotFoundException ( id ) ; } if ( await this . checkPermissionUsage ( id ) ) { throw new PermissionDeleteNotAllowedException ( id ) ; } await this . permissionsRepository . softDelete ( id ) ; await this . permissionCacheService . invalidatePermissionsCache ( existingPermission . name ) ; return existingPermission ; } private async checkPermissionUsage ( id ) { const userCount = await this . userPermissionsRepository . count ( { where : { permissionId : id } , } ) ; const groupCount = await this . groupPermissionRepository . count ( { where : { permissionId : id } , } ) ; const totalCount = userCount + groupCount ; return totalCount != 0 ; } }