11import { BadRequestException , Injectable } from '@nestjs/common' ;
22import { InstrumentUsecase } from '@novu/application-generic' ;
3- import { AgentRepository } from '@novu/dal' ;
3+ import { AgentIntegrationRepository , AgentRepository , IntegrationRepository } from '@novu/dal' ;
44import { DirectionEnum } from '@novu/shared' ;
5+ import type { AgentIntegrationSummaryDto } from '../../dtos/agent-integration-summary.dto' ;
56import { ListAgentsResponseDto } from '../../dtos/list-agents-response.dto' ;
6- import { toAgentResponse } from '../../mappers/agent-response.mapper' ;
7+ import { toAgentIntegrationSummary , toAgentResponse } from '../../mappers/agent-response.mapper' ;
78import { ListAgentsCommand } from './list-agents.command' ;
89
910@Injectable ( )
1011export class ListAgents {
11- constructor ( private readonly agentRepository : AgentRepository ) { }
12+ constructor (
13+ private readonly agentRepository : AgentRepository ,
14+ private readonly agentIntegrationRepository : AgentIntegrationRepository ,
15+ private readonly integrationRepository : IntegrationRepository
16+ ) { }
1217
1318 @InstrumentUsecase ( )
1419 async execute ( command : ListAgentsCommand ) : Promise < ListAgentsResponseDto > {
@@ -28,12 +33,101 @@ export class ListAgents {
2833 identifier : command . identifier ,
2934 } ) ;
3035
36+ const integrationsByAgentId = await this . loadIntegrationsForAgents (
37+ command . environmentId ,
38+ command . organizationId ,
39+ pagination . agents
40+ ) ;
41+
3142 return {
32- data : pagination . agents . map ( ( agent ) => toAgentResponse ( agent ) ) ,
43+ data : pagination . agents . map ( ( agent ) => ( {
44+ ...toAgentResponse ( agent ) ,
45+ integrations : integrationsByAgentId . get ( agent . _id ) ?? [ ] ,
46+ } ) ) ,
3347 next : pagination . next ,
3448 previous : pagination . previous ,
3549 totalCount : pagination . totalCount ,
3650 totalCountCapped : pagination . totalCountCapped ,
3751 } ;
3852 }
53+
54+ private async loadIntegrationsForAgents (
55+ environmentId : string ,
56+ organizationId : string ,
57+ agents : { _id : string } [ ]
58+ ) : Promise < Map < string , AgentIntegrationSummaryDto [ ] > > {
59+ const result = new Map < string , AgentIntegrationSummaryDto [ ] > ( ) ;
60+
61+ if ( agents . length === 0 ) {
62+ return result ;
63+ }
64+
65+ const agentIds = agents . map ( ( a ) => a . _id ) ;
66+ const links = await this . agentIntegrationRepository . findLinksForAgents ( {
67+ environmentId,
68+ organizationId,
69+ agentIds,
70+ } ) ;
71+
72+ const integrationIds = [ ...new Set ( links . map ( ( l ) => l . _integrationId ) ) ] ;
73+
74+ if ( integrationIds . length === 0 ) {
75+ for ( const id of agentIds ) {
76+ result . set ( id , [ ] ) ;
77+ }
78+
79+ return result ;
80+ }
81+
82+ const integrations = await this . integrationRepository . find (
83+ {
84+ _id : { $in : integrationIds } ,
85+ _environmentId : environmentId ,
86+ _organizationId : organizationId ,
87+ } ,
88+ '_id identifier name providerId channel active'
89+ ) ;
90+
91+ const summaryByIntegrationId = new Map ( integrations . map ( ( i ) => [ i . _id , toAgentIntegrationSummary ( i ) ] as const ) ) ;
92+
93+ const seen = new Map < string , Set < string > > ( ) ;
94+
95+ for ( const link of links ) {
96+ const summary = summaryByIntegrationId . get ( link . _integrationId ) ;
97+
98+ if ( ! summary ) {
99+ continue ;
100+ }
101+
102+ let dedupe = seen . get ( link . _agentId ) ;
103+
104+ if ( ! dedupe ) {
105+ dedupe = new Set < string > ( ) ;
106+ seen . set ( link . _agentId , dedupe ) ;
107+ }
108+
109+ if ( dedupe . has ( summary . integrationId ) ) {
110+ continue ;
111+ }
112+
113+ dedupe . add ( summary . integrationId ) ;
114+ const list = result . get ( link . _agentId ) ?? [ ] ;
115+ list . push ( summary ) ;
116+
117+ result . set ( link . _agentId , list ) ;
118+ }
119+
120+ for ( const id of agentIds ) {
121+ if ( ! result . has ( id ) ) {
122+ result . set ( id , [ ] ) ;
123+ } else {
124+ const list = result . get ( id ) ?? [ ] ;
125+ const sorted = [ ...list ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
126+
127+ result . set ( id , sorted ) ;
128+ }
129+ }
130+
131+ return result ;
132+ }
39133}
0 commit comments