-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathlist-agents.usecase.ts
More file actions
133 lines (109 loc) · 3.96 KB
/
list-agents.usecase.ts
File metadata and controls
133 lines (109 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { BadRequestException, Injectable } from '@nestjs/common';
import { InstrumentUsecase } from '@novu/application-generic';
import { AgentIntegrationRepository, AgentRepository, IntegrationRepository } from '@novu/dal';
import { DirectionEnum } from '@novu/shared';
import type { AgentIntegrationSummaryDto } from '../../dtos/agent-integration-summary.dto';
import { ListAgentsResponseDto } from '../../dtos/list-agents-response.dto';
import { toAgentIntegrationSummary, toAgentResponse } from '../../mappers/agent-response.mapper';
import { ListAgentsCommand } from './list-agents.command';
@Injectable()
export class ListAgents {
constructor(
private readonly agentRepository: AgentRepository,
private readonly agentIntegrationRepository: AgentIntegrationRepository,
private readonly integrationRepository: IntegrationRepository
) {}
@InstrumentUsecase()
async execute(command: ListAgentsCommand): Promise<ListAgentsResponseDto> {
if (command.before && command.after) {
throw new BadRequestException('Cannot specify both "before" and "after" cursors at the same time.');
}
const pagination = await this.agentRepository.listAgents({
after: command.after,
before: command.before,
limit: command.limit,
sortDirection: command.orderDirection === DirectionEnum.ASC ? 1 : -1,
sortBy: command.orderBy,
environmentId: command.environmentId,
organizationId: command.organizationId,
includeCursor: command.includeCursor,
identifier: command.identifier,
});
const integrationsByAgentId = await this.loadIntegrationsForAgents(
command.environmentId,
command.organizationId,
pagination.agents
);
return {
data: pagination.agents.map((agent) => ({
...toAgentResponse(agent),
integrations: integrationsByAgentId.get(agent._id) ?? [],
})),
next: pagination.next,
previous: pagination.previous,
totalCount: pagination.totalCount,
totalCountCapped: pagination.totalCountCapped,
};
}
private async loadIntegrationsForAgents(
environmentId: string,
organizationId: string,
agents: { _id: string }[]
): Promise<Map<string, AgentIntegrationSummaryDto[]>> {
const result = new Map<string, AgentIntegrationSummaryDto[]>();
if (agents.length === 0) {
return result;
}
const agentIds = agents.map((a) => a._id);
const links = await this.agentIntegrationRepository.findLinksForAgents({
environmentId,
organizationId,
agentIds,
});
const integrationIds = [...new Set(links.map((l) => l._integrationId))];
if (integrationIds.length === 0) {
for (const id of agentIds) {
result.set(id, []);
}
return result;
}
const integrations = await this.integrationRepository.find(
{
_id: { $in: integrationIds },
_environmentId: environmentId,
_organizationId: organizationId,
},
'_id identifier name providerId channel active'
);
const summaryByIntegrationId = new Map(integrations.map((i) => [i._id, toAgentIntegrationSummary(i)] as const));
const seen = new Map<string, Set<string>>();
for (const link of links) {
const summary = summaryByIntegrationId.get(link._integrationId);
if (!summary) {
continue;
}
let dedupe = seen.get(link._agentId);
if (!dedupe) {
dedupe = new Set<string>();
seen.set(link._agentId, dedupe);
}
if (dedupe.has(summary.integrationId)) {
continue;
}
dedupe.add(summary.integrationId);
const list = result.get(link._agentId) ?? [];
list.push(summary);
result.set(link._agentId, list);
}
for (const id of agentIds) {
if (!result.has(id)) {
result.set(id, []);
} else {
const list = result.get(id) ?? [];
const sorted = [...list].sort((a, b) => a.name.localeCompare(b.name));
result.set(id, sorted);
}
}
return result;
}
}