import { RedisCacheService } from '../../cache/redis-cache/redis-cache.service' ; import { Injectable } from '@nestjs/common' ; import GroupPermission from '../entity/groupPermission.entity' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import Group from '../entity/group.entity' ; @ Injectable ( ) export default class GroupCacheService { constructor ( private cacheManager , @ InjectRepository ( GroupPermission ) private groupPermissionRepository , @ InjectRepository ( Group ) private groupRepository ) { } async getGroupPermissionsFromGroupId ( groupId ) { const permissionsFromCache = await this . cacheManager . get < string [ ] > ( `GROUP:${ groupId }:PERMISSIONS` ) ; const permissions = permissionsFromCache || ( await this . groupRepository . findOneOrFail ( { where : { id : groupId } } ) . then ( ( ) => this . groupPermissionRepository . find ( { where : { groupId : groupId } , } ) ) ) . map ( ( x ) => x . permissionId ) ; permissionsFromCache || ( await this . cacheManager . set ( `GROUP:${ groupId }:PERMISSIONS` , permissions ) ) ; return permissionsFromCache || permissions ; } async invalidateGroupPermissionsByGroupId ( id ) { this . cacheManager . del ( `GROUP:${ id }:PERMISSIONS` ) ; } }