Skip to content

Commit e549667

Browse files
ChmaraXcursoragent
andauthored
refactor(api): move DTO mapping from usecases to controllers (#9133)
Co-authored-by: Cursor Agent <[email protected]>
1 parent 3ae323b commit e549667

File tree

10 files changed

+44
-58
lines changed

10 files changed

+44
-58
lines changed

apps/api/src/app/channel-connections/usecases/create-channel-connection/create-channel-connection.usecase.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
SubscriberRepository,
99
} from '@novu/dal';
1010
import { parseResourceKey } from '@novu/shared';
11-
import { mapChannelConnectionEntityToDto } from '../../dtos/dto.mapper';
12-
import { GetChannelConnectionResponseDto } from '../../dtos/get-channel-connection-response.dto';
1311
import { CreateChannelConnectionCommand } from './create-channel-connection.command';
1412

1513
@Injectable()
@@ -21,7 +19,7 @@ export class CreateChannelConnection {
2119
) {}
2220

2321
@InstrumentUsecase()
24-
async execute(command: CreateChannelConnectionCommand): Promise<GetChannelConnectionResponseDto> {
22+
async execute(command: CreateChannelConnectionCommand): Promise<ChannelConnectionEntity> {
2523
const integration = await this.findIntegration(command);
2624

2725
await this.assertSingleConnectionPerResourceAndIntegration(command, integration);
@@ -44,7 +42,7 @@ export class CreateChannelConnection {
4442

4543
const channelConnection = await this.createChannelConnection(command, identifier, integration);
4644

47-
return mapChannelConnectionEntityToDto(channelConnection);
45+
return channelConnection;
4846
}
4947

5048
private async assertSingleConnectionPerResourceAndIntegration(

apps/api/src/app/channel-connections/usecases/get-channel-connection/get-channel-connection.usecase.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { InstrumentUsecase } from '@novu/application-generic';
3-
import { ChannelConnectionRepository } from '@novu/dal';
4-
import { mapChannelConnectionEntityToDto } from '../../dtos/dto.mapper';
5-
import { GetChannelConnectionResponseDto } from '../../dtos/get-channel-connection-response.dto';
3+
import { ChannelConnectionEntity, ChannelConnectionRepository } from '@novu/dal';
64
import { GetChannelConnectionCommand } from './get-channel-connection.command';
75

86
@Injectable()
97
export class GetChannelConnection {
108
constructor(private readonly channelConnectionRepository: ChannelConnectionRepository) {}
119

1210
@InstrumentUsecase()
13-
async execute(command: GetChannelConnectionCommand): Promise<GetChannelConnectionResponseDto> {
11+
async execute(command: GetChannelConnectionCommand): Promise<ChannelConnectionEntity> {
1412
const channelConnection = await this.channelConnectionRepository.findOne({
1513
_organizationId: command.organizationId,
1614
_environmentId: command.environmentId,
@@ -24,6 +22,6 @@ export class GetChannelConnection {
2422
);
2523
}
2624

27-
return mapChannelConnectionEntityToDto(channelConnection);
25+
return channelConnection;
2826
}
2927
}

apps/api/src/app/channel-connections/usecases/get-channel-connections/get-channel-connections.usecase.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ import {
88
} from '@novu/dal';
99
import { ProvidersIdEnum } from '@novu/shared';
1010
import { FilterQuery } from 'mongoose';
11-
import { mapChannelConnectionEntityToDto } from '../../dtos/dto.mapper';
12-
import { GetChannelConnectionResponseDto } from '../../dtos/get-channel-connection-response.dto';
1311
import { GetChannelConnectionsCommand } from './get-channel-connections.command';
1412

1513
@Injectable()
1614
export class GetChannelConnections {
1715
constructor(private readonly channelConnectionRepository: ChannelConnectionRepository) {}
1816

1917
@InstrumentUsecase()
20-
async execute(command: GetChannelConnectionsCommand): Promise<GetChannelConnectionResponseDto[]> {
18+
async execute(command: GetChannelConnectionsCommand): Promise<ChannelConnectionEntity[]> {
2119
const channelConnections = await this.fetchChannelConnections(command);
2220

23-
if (channelConnections.length === 0) {
24-
return [];
25-
}
26-
27-
return this.mapAndFilterConnections(channelConnections);
21+
return channelConnections;
2822
}
2923

3024
private async fetchChannelConnections(command: GetChannelConnectionsCommand): Promise<ChannelConnectionEntity[]> {
@@ -44,8 +38,4 @@ export class GetChannelConnections {
4438

4539
return await this.channelConnectionRepository.find(query);
4640
}
47-
48-
private mapAndFilterConnections(channelConnections: ChannelConnectionEntity[]): GetChannelConnectionResponseDto[] {
49-
return channelConnections.map((conn) => mapChannelConnectionEntityToDto(conn));
50-
}
5141
}

apps/api/src/app/channel-connections/usecases/update-channel-connection/update-channel-connection.usecase.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { InstrumentUsecase } from '@novu/application-generic';
33
import { ChannelConnectionEntity, ChannelConnectionRepository } from '@novu/dal';
4-
import { mapChannelConnectionEntityToDto } from '../../dtos/dto.mapper';
5-
import { GetChannelConnectionResponseDto } from '../../dtos/get-channel-connection-response.dto';
64
import { UpdateChannelConnectionCommand } from './update-channel-connection.command';
75

86
@Injectable()
97
export class UpdateChannelConnection {
108
constructor(private readonly channelConnectionRepository: ChannelConnectionRepository) {}
119

1210
@InstrumentUsecase()
13-
async execute(command: UpdateChannelConnectionCommand): Promise<GetChannelConnectionResponseDto> {
11+
async execute(command: UpdateChannelConnectionCommand): Promise<ChannelConnectionEntity> {
1412
const updatedChannelConnection = await this.updateChannelConnection(command);
1513

16-
return mapChannelConnectionEntityToDto(updatedChannelConnection);
14+
return updatedChannelConnection;
1715
}
1816

1917
private async updateChannelConnection(command: UpdateChannelConnectionCommand): Promise<ChannelConnectionEntity> {

apps/api/src/app/channel-endpoints/usecases/create-channel-endpoint/create-channel-endpoint.usecase.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import {
1010
SubscriberRepository,
1111
} from '@novu/dal';
1212
import { parseResourceKey } from '@novu/shared';
13-
import { mapChannelEndpointEntityToDto } from '../../dtos/dto.mapper';
14-
import { GetChannelEndpointResponseDto } from '../../dtos/get-channel-endpoint-response.dto';
1513
import { CreateChannelEndpointCommand } from './create-channel-endpoint.command';
1614

1715
@Injectable()
@@ -24,7 +22,7 @@ export class CreateChannelEndpoint {
2422
) {}
2523

2624
@InstrumentUsecase()
27-
async execute(command: CreateChannelEndpointCommand): Promise<GetChannelEndpointResponseDto> {
25+
async execute(command: CreateChannelEndpointCommand): Promise<ChannelEndpointEntity> {
2826
const integration = await this.findIntegration(command);
2927

3028
await this.assertResourceExists(command);
@@ -52,7 +50,7 @@ export class CreateChannelEndpoint {
5250

5351
const channelEndpoint = await this.createChannelEndpoint(command, identifier, integration, connection);
5452

55-
return mapChannelEndpointEntityToDto(channelEndpoint);
53+
return channelEndpoint;
5654
}
5755

5856
private async createChannelEndpoint(
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { InstrumentUsecase } from '@novu/application-generic';
3-
import { ChannelEndpointRepository } from '@novu/dal';
4-
import { mapChannelEndpointEntityToDto } from '../../dtos/dto.mapper';
5-
import { GetChannelEndpointResponseDto } from '../../dtos/get-channel-endpoint-response.dto';
3+
import { ChannelEndpointEntity, ChannelEndpointRepository } from '@novu/dal';
64
import { GetChannelEndpointCommand } from './get-channel-endpoint.command';
75

86
@Injectable()
97
export class GetChannelEndpoint {
108
constructor(private readonly channelEndpointRepository: ChannelEndpointRepository) {}
119

1210
@InstrumentUsecase()
13-
async execute(command: GetChannelEndpointCommand): Promise<GetChannelEndpointResponseDto> {
11+
async execute(command: GetChannelEndpointCommand): Promise<ChannelEndpointEntity> {
1412
const channelEndpoint = await this.channelEndpointRepository.findOne({
1513
identifier: command.identifier,
1614
_organizationId: command.organizationId,
@@ -21,6 +19,6 @@ export class GetChannelEndpoint {
2119
throw new NotFoundException(`Channel endpoint with identifier '${command.identifier}' not found`);
2220
}
2321

24-
return mapChannelEndpointEntityToDto(channelEndpoint);
22+
return channelEndpoint;
2523
}
2624
}

apps/api/src/app/channel-endpoints/usecases/get-channel-endpoints/get-channel-endpoints.usecase.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@ import type { EnforceEnvOrOrgIds } from '@novu/dal';
44
import { ChannelEndpointDBModel, ChannelEndpointEntity, ChannelEndpointRepository } from '@novu/dal';
55
import { ProvidersIdEnum } from '@novu/shared';
66
import { FilterQuery } from 'mongoose';
7-
import { mapChannelEndpointEntityToDto } from '../../dtos/dto.mapper';
8-
import { GetChannelEndpointResponseDto } from '../../dtos/get-channel-endpoint-response.dto';
97
import { GetChannelEndpointsCommand } from './get-channel-endpoints.command';
108

119
@Injectable()
1210
export class GetChannelEndpoints {
1311
constructor(private readonly channelEndpointRepository: ChannelEndpointRepository) {}
1412

1513
@InstrumentUsecase()
16-
async execute(command: GetChannelEndpointsCommand): Promise<GetChannelEndpointResponseDto[]> {
14+
async execute(command: GetChannelEndpointsCommand): Promise<ChannelEndpointEntity[]> {
1715
const channelEndpoints = await this.fetchChannelEndpoints(command);
1816

19-
if (channelEndpoints.length === 0) {
20-
return [];
21-
}
22-
23-
return this.mapAndFilterEndpoints(channelEndpoints);
17+
return channelEndpoints;
2418
}
2519

2620
private async fetchChannelEndpoints(command: GetChannelEndpointsCommand): Promise<ChannelEndpointEntity[]> {
@@ -47,8 +41,4 @@ export class GetChannelEndpoints {
4741

4842
return await this.channelEndpointRepository.find(query);
4943
}
50-
51-
private mapAndFilterEndpoints(channelEndpoints: ChannelEndpointEntity[]): GetChannelEndpointResponseDto[] {
52-
return channelEndpoints.map((endpoint) => mapChannelEndpointEntityToDto(endpoint));
53-
}
5444
}

apps/api/src/app/channel-endpoints/usecases/update-channel-endpoint/update-channel-endpoint.usecase.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { InstrumentUsecase, validateEndpointForType } from '@novu/application-generic';
33
import { ChannelEndpointEntity, ChannelEndpointRepository } from '@novu/dal';
4-
import { mapChannelEndpointEntityToDto } from '../../dtos/dto.mapper';
5-
import { GetChannelEndpointResponseDto } from '../../dtos/get-channel-endpoint-response.dto';
64
import { UpdateChannelEndpointCommand } from './update-channel-endpoint.command';
75

86
@Injectable()
97
export class UpdateChannelEndpoint {
108
constructor(private readonly channelEndpointRepository: ChannelEndpointRepository) {}
119

1210
@InstrumentUsecase()
13-
async execute(command: UpdateChannelEndpointCommand): Promise<GetChannelEndpointResponseDto> {
11+
async execute(command: UpdateChannelEndpointCommand): Promise<ChannelEndpointEntity> {
1412
// Check if the channel endpoint exists
1513
const existingChannelEndpoint = await this.channelEndpointRepository.findOne({
1614
identifier: command.identifier,
@@ -29,7 +27,7 @@ export class UpdateChannelEndpoint {
2927

3028
const updatedChannelEndpoint = await this.updateChannelEndpoint(command);
3129

32-
return mapChannelEndpointEntityToDto(updatedChannelEndpoint);
30+
return updatedChannelEndpoint;
3331
}
3432

3533
private async updateChannelEndpoint(command: UpdateChannelEndpointCommand): Promise<ChannelEndpointEntity> {

apps/api/src/app/subscribers-v2/channel-connections.controller.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from '@novu/shared';
2525
import { RequireAuthentication } from '../auth/framework/auth.decorator';
2626
import { CreateChannelConnectionRequestDto } from '../channel-connections/dtos/create-channel-connection-request.dto';
27+
import { mapChannelConnectionEntityToDto } from '../channel-connections/dtos/dto.mapper';
2728
import { GetChannelConnectionResponseDto } from '../channel-connections/dtos/get-channel-connection-response.dto';
2829
import { GetChannelConnectionsQueryDto } from '../channel-connections/dtos/get-channel-connections-query.dto';
2930
import { UpdateChannelConnectionRequestDto } from '../channel-connections/dtos/update-channel-connection-request.dto';
@@ -84,14 +85,16 @@ export class ChannelConnectionsController {
8485
): Promise<GetChannelConnectionResponseDto> {
8586
await this.checkFeatureEnabled(user);
8687

87-
return await this.getChannelConnectionUsecase.execute(
88+
const channelConnection = await this.getChannelConnectionUsecase.execute(
8889
GetChannelConnectionCommand.create({
8990
environmentId: user.environmentId,
9091
organizationId: user.organizationId,
9192
resource: makeResourceKey(RESOURCE.SUBSCRIBER, subscriberId),
9293
integrationIdentifier,
9394
})
9495
);
96+
97+
return mapChannelConnectionEntityToDto(channelConnection);
9598
}
9699

97100
@Get('/:subscriberId/channel-connections')
@@ -109,7 +112,7 @@ export class ChannelConnectionsController {
109112
): Promise<GetChannelConnectionResponseDto[]> {
110113
await this.checkFeatureEnabled(user);
111114

112-
return await this.getChannelConnectionsUsecase.execute(
115+
const channelConnections = await this.getChannelConnectionsUsecase.execute(
113116
GetChannelConnectionsCommand.create({
114117
environmentId: user.environmentId,
115118
organizationId: user.organizationId,
@@ -118,6 +121,8 @@ export class ChannelConnectionsController {
118121
provider: query.provider,
119122
})
120123
);
124+
125+
return channelConnections.map((connection) => mapChannelConnectionEntityToDto(connection));
121126
}
122127

123128
@Post('/:subscriberId/channel-connections')
@@ -135,7 +140,7 @@ export class ChannelConnectionsController {
135140
): Promise<GetChannelConnectionResponseDto> {
136141
await this.checkFeatureEnabled(user);
137142

138-
return await this.createChannelConnectionUsecase.execute(
143+
const channelConnection = await this.createChannelConnectionUsecase.execute(
139144
CreateChannelConnectionCommand.create({
140145
environmentId: user.environmentId,
141146
organizationId: user.organizationId,
@@ -146,6 +151,8 @@ export class ChannelConnectionsController {
146151
auth: body.auth,
147152
})
148153
);
154+
155+
return mapChannelConnectionEntityToDto(channelConnection);
149156
}
150157

151158
@Patch('channel-connections/:identifier')
@@ -164,7 +171,7 @@ export class ChannelConnectionsController {
164171
): Promise<GetChannelConnectionResponseDto> {
165172
await this.checkFeatureEnabled(user);
166173

167-
return await this.updateChannelConnectionUsecase.execute(
174+
const channelConnection = await this.updateChannelConnectionUsecase.execute(
168175
UpdateChannelConnectionCommand.create({
169176
environmentId: user.environmentId,
170177
organizationId: user.organizationId,
@@ -173,6 +180,8 @@ export class ChannelConnectionsController {
173180
auth: body.auth,
174181
})
175182
);
183+
184+
return mapChannelConnectionEntityToDto(channelConnection);
176185
}
177186

178187
@Delete('channel-connections/:identifier')

apps/api/src/app/subscribers-v2/channel-endpoints.controller.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from '@novu/shared';
2525
import { RequireAuthentication } from '../auth/framework/auth.decorator';
2626
import { CreateChannelEndpointRequestDto } from '../channel-endpoints/dtos/create-channel-endpoint-request.dto';
27+
import { mapChannelEndpointEntityToDto } from '../channel-endpoints/dtos/dto.mapper';
2728
import { GetChannelEndpointResponseDto } from '../channel-endpoints/dtos/get-channel-endpoint-response.dto';
2829
import { GetChannelEndpointsQueryDto } from '../channel-endpoints/dtos/get-channel-endpoints-query.dto';
2930
import { UpdateChannelEndpointRequestDto } from '../channel-endpoints/dtos/update-channel-endpoint-request.dto';
@@ -84,7 +85,7 @@ export class ChannelEndpointsController {
8485
): Promise<GetChannelEndpointResponseDto[]> {
8586
await this.checkFeatureEnabled(user);
8687

87-
return await this.getChannelEndpointsUsecase.execute(
88+
const channelEndpoints = await this.getChannelEndpointsUsecase.execute(
8889
GetChannelEndpointsCommand.create({
8990
environmentId: user.environmentId,
9091
organizationId: user.organizationId,
@@ -94,6 +95,8 @@ export class ChannelEndpointsController {
9495
type: query.type,
9596
})
9697
);
98+
99+
return channelEndpoints.map((endpoint) => mapChannelEndpointEntityToDto(endpoint));
97100
}
98101

99102
@Get('/channel-endpoints/:identifier')
@@ -111,13 +114,15 @@ export class ChannelEndpointsController {
111114
): Promise<GetChannelEndpointResponseDto> {
112115
await this.checkFeatureEnabled(user);
113116

114-
return await this.getChannelEndpointUsecase.execute(
117+
const channelEndpoint = await this.getChannelEndpointUsecase.execute(
115118
GetChannelEndpointCommand.create({
116119
environmentId: user.environmentId,
117120
organizationId: user.organizationId,
118121
identifier,
119122
})
120123
);
124+
125+
return mapChannelEndpointEntityToDto(channelEndpoint);
121126
}
122127

123128
@Post('/:subscriberId/channel-endpoints')
@@ -135,7 +140,7 @@ export class ChannelEndpointsController {
135140
): Promise<GetChannelEndpointResponseDto> {
136141
await this.checkFeatureEnabled(user);
137142

138-
return await this.createChannelEndpointUsecase.execute(
143+
const channelEndpoint = await this.createChannelEndpointUsecase.execute(
139144
CreateChannelEndpointCommand.create({
140145
environmentId: user.environmentId,
141146
organizationId: user.organizationId,
@@ -147,6 +152,8 @@ export class ChannelEndpointsController {
147152
endpoint: body.endpoint,
148153
})
149154
);
155+
156+
return mapChannelEndpointEntityToDto(channelEndpoint);
150157
}
151158

152159
@Patch('/channel-endpoints/:identifier')
@@ -165,14 +172,16 @@ export class ChannelEndpointsController {
165172
): Promise<GetChannelEndpointResponseDto> {
166173
await this.checkFeatureEnabled(user);
167174

168-
return await this.updateChannelEndpointUsecase.execute(
175+
const channelEndpoint = await this.updateChannelEndpointUsecase.execute(
169176
UpdateChannelEndpointCommand.create({
170177
environmentId: user.environmentId,
171178
organizationId: user.organizationId,
172179
identifier,
173180
endpoint: body.endpoint,
174181
})
175182
);
183+
184+
return mapChannelEndpointEntityToDto(channelEndpoint);
176185
}
177186

178187
@Delete('/channel-endpoints/:identifier')

0 commit comments

Comments
 (0)