1- import { ExecutionContext } from '@nestjs/common'
1+ import { ExecutionContext , Inject , Optional } from '@nestjs/common'
22import { Args , ArgsType , Context , Parent , Resolver } from '@nestjs/graphql'
3- import { AggregateQuery , AggregateResponse , Class , Filter , mergeFilter , QueryService } from '@ptc-org/nestjs-query-core'
3+ import { AggregateQuery , AggregateResponse , Class , mergeFilter , QueryService } from '@ptc-org/nestjs-query-core'
44
5- import { OperationGroup } from '../../auth'
5+ import { Authorizer , getAuthorizerToken , OperationGroup } from '../../auth'
66import { getDTONames } from '../../common'
7- import { AggregateQueryParam , RelationAuthorizerFilter , ResolverField } from '../../decorators'
7+ import { AggregateQueryParam , ResolverField } from '../../decorators'
88import { InjectDataLoaderConfig } from '../../decorators/inject-dataloader-config.decorator'
99import { AuthorizerInterceptor } from '../../interceptors'
1010import { AggregateRelationsLoader , DataLoaderFactory } from '../../loader'
@@ -39,6 +39,8 @@ const AggregateRelationMixin =
3939 const relationName = relation . relationName ?? baseNameLower
4040 const aggregateRelationLoaderName = `aggregate${ baseName } For${ dtoName } `
4141 const aggregateLoader = new AggregateRelationsLoader < DTO , Relation > ( relationDTO , relationName )
42+ const authorizerKey = Symbol ( `authorizerFor${ DTOClass . name } ` )
43+ const relationAuthorizerKey = Symbol ( `authorizerFor${ relation . dtoName } ` )
4244
4345 @ArgsType ( )
4446 class RelationQA extends AggregateArgsType ( relationDTO ) { }
@@ -47,6 +49,10 @@ const AggregateRelationMixin =
4749
4850 @Resolver ( ( ) => DTOClass , { isAbstract : true } )
4951 class AggregateMixin extends Base {
52+ @Optional ( ) @Inject ( getAuthorizerToken ( DTOClass ) ) [ authorizerKey ] ?: Authorizer < Relation > ;
53+
54+ @Optional ( ) @Inject ( getAuthorizerToken ( relationDTO ) ) [ relationAuthorizerKey ] ?: Authorizer < Relation >
55+
5056 @ResolverField (
5157 `${ baseNameLower } Aggregate` ,
5258 ( ) => [ AR ] ,
@@ -63,11 +69,6 @@ const AggregateRelationMixin =
6369 @Args ( ) q : RelationQA ,
6470 @AggregateQueryParam ( ) aggregateQuery : AggregateQuery < Relation > ,
6571 @Context ( ) context : ExecutionContext ,
66- @RelationAuthorizerFilter ( baseNameLower , {
67- operationGroup : OperationGroup . AGGREGATE ,
68- many : true
69- } )
70- relationFilter ?: Filter < Relation > ,
7172 @InjectDataLoaderConfig ( )
7273 dataLoaderConfig ?: DataLoaderOptions
7374 ) : Promise < AggregateResponse < Relation > > {
@@ -78,10 +79,20 @@ const AggregateRelationMixin =
7879 ( ) => aggregateLoader . createLoader ( this . service ) ,
7980 dataLoaderConfig
8081 )
82+ const authContext = {
83+ operationName : baseNameLower ,
84+ operationGroup : OperationGroup . AGGREGATE ,
85+ readonly : true ,
86+ many : true
87+ }
88+ const authFilter = relation . auth
89+ ? await relation . auth ?. authorize ( context , authContext )
90+ : ( ( await this [ authorizerKey ] ?. authorizeRelation ( baseNameLower , context , authContext ) ) ??
91+ ( await this [ relationAuthorizerKey ] ?. authorize ( context , authContext ) ) )
8192
8293 return loader . load ( {
8394 dto,
84- filter : mergeFilter ( qa . filter ?? { } , relationFilter ?? { } ) ,
95+ filter : mergeFilter ( qa . filter ?? { } , authFilter ?? { } ) ,
8596 aggregate : aggregateQuery
8697 } )
8798 }
0 commit comments