Skip to content

Commit 02f6f34

Browse files
committed
refactor: remove transactional decorators and streamline command handlers
1 parent d359aea commit 02f6f34

File tree

17 files changed

+94
-234
lines changed

17 files changed

+94
-234
lines changed

src/modules/config-profiles/config-profile.service.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ export class ConfigProfileService {
164164
}
165165
}
166166

167-
@Transactional()
168167
public async createConfigProfile(
169168
name: string,
170169
config: object,
@@ -187,12 +186,9 @@ export class ConfigProfileService {
187186

188187
const inbounds = validatedConfig.getAllInbounds();
189188

190-
const configProfile = await this.configProfileRepository.create(profileEntity);
191-
192189
const inboundsEntities = inbounds.map(
193190
(inbound) =>
194191
new ConfigProfileInboundEntity({
195-
profileUuid: configProfile.uuid,
196192
tag: inbound.tag,
197193
type: inbound.type,
198194
network: inbound.network,
@@ -202,13 +198,12 @@ export class ConfigProfileService {
202198
}),
203199
);
204200

205-
if (inboundsEntities.length) {
206-
await this.configProfileRepository.createManyConfigProfileInbounds(
207-
inboundsEntities,
208-
);
209-
}
201+
const { uuid } = await this.configProfileRepository.create(
202+
profileEntity,
203+
inboundsEntities,
204+
);
210205

211-
return this.getConfigProfileByUUID(configProfile.uuid);
206+
return await this.getConfigProfileByUUID(uuid);
212207
} catch (error) {
213208
if (
214209
error instanceof PrismaClientKnownRequestError &&

src/modules/config-profiles/repositories/config-profile.repository.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { TransactionHost } from '@nestjs-cls/transactional';
99
import { Injectable } from '@nestjs/common';
1010

1111
import { TxKyselyService } from '@common/database';
12-
import { ICrud } from '@common/types/crud-port';
1312
import { getKyselyUuid } from '@common/helpers';
1413

1514
import { ConfigProfileWithInboundsAndNodesEntity } from '../entities/config-profile-with-inbounds-and-nodes.entity';
@@ -19,23 +18,36 @@ import { ConfigProfileEntity } from '../entities/config-profile.entity';
1918
import { ConfigProfileInboundWithSquadsEntity } from '../entities';
2019

2120
@Injectable()
22-
export class ConfigProfileRepository implements ICrud<ConfigProfileEntity> {
21+
export class ConfigProfileRepository {
2322
constructor(
2423
private readonly prisma: TransactionHost<TransactionalAdapterPrisma>,
2524
private readonly qb: TxKyselyService,
2625
private readonly configProfileConverter: ConfigProfileConverter,
2726
) {}
2827

29-
public async create(entity: ConfigProfileEntity): Promise<ConfigProfileEntity> {
28+
public async create(
29+
entity: ConfigProfileEntity,
30+
inbounds: ConfigProfileInboundEntity[],
31+
): Promise<{ uuid: string }> {
3032
const model = this.configProfileConverter.fromEntityToPrismaModel(entity);
3133
const result = await this.prisma.tx.configProfiles.create({
34+
select: {
35+
uuid: true,
36+
},
3237
data: {
3338
...model,
3439
config: model.config as Prisma.InputJsonValue,
40+
configProfileInbounds: {
41+
create: inbounds.map((inbound) => ({
42+
...inbound,
43+
})),
44+
},
3545
},
3646
});
3747

38-
return this.configProfileConverter.fromPrismaModelToEntity(result);
48+
return {
49+
uuid: result.uuid,
50+
};
3951
}
4052

4153
public async findByUUID(uuid: string): Promise<ConfigProfileEntity | null> {

src/modules/internal-squads/internal-squad.service.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class InternalSquadService {
7171
}
7272
}
7373

74-
@Transactional()
7574
public async createInternalSquad(
7675
name: string,
7776
inbounds: string[],
@@ -84,16 +83,11 @@ export class InternalSquadService {
8483
};
8584
}
8685

87-
const internalSquad = await this.internalSquadRepository.create(
88-
new InternalSquadEntity({
89-
name,
90-
}),
86+
const internalSquad = await this.internalSquadRepository.createWithInbounds(
87+
name,
88+
inbounds,
9189
);
9290

93-
if (inbounds.length > 0) {
94-
await this.internalSquadRepository.createInbounds(inbounds, internalSquad.uuid);
95-
}
96-
9791
return await this.getInternalSquadByUuid(internalSquad.uuid);
9892
} catch (error) {
9993
if (

src/modules/internal-squads/repositories/internal-squad.repository.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ export class InternalSquadRepository implements ICrud<InternalSquadEntity> {
3030
return this.internalSquadConverter.fromPrismaModelToEntity(result);
3131
}
3232

33+
public async createWithInbounds(
34+
name: string,
35+
inbounds: string[],
36+
): Promise<InternalSquadEntity> {
37+
const result = await this.prisma.tx.internalSquads.create({
38+
data: {
39+
name,
40+
internalSquadInbounds: {
41+
create: inbounds.map((inbound) => ({ inboundUuid: inbound })),
42+
},
43+
},
44+
});
45+
46+
return this.internalSquadConverter.fromPrismaModelToEntity(result);
47+
}
48+
3349
public async findByUUID(uuid: string): Promise<InternalSquadEntity | null> {
3450
const result = await this.prisma.tx.internalSquads.findUnique({
3551
where: { uuid },

src/modules/nodes-user-usage-history/commands/clean-old-usage-records/clean-old-usage-records.command.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/modules/nodes-user-usage-history/commands/clean-old-usage-records/clean-old-usage-records.handler.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/modules/nodes-user-usage-history/commands/clean-old-usage-records/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { TruncateNodesUserUsageHistoryHandler } from './truncate-nodes-user-usage-history';
22
import { VacuumNodesUserUsageHistoryHandler } from './vacuum-nodes-user-usage-history';
33
import { BulkUpsertUserHistoryEntryHandler } from './bulk-upsert-user-history-entry';
4-
import { CleanOldUsageRecordsHandler } from './clean-old-usage-records';
54

65
export const COMMANDS = [
76
BulkUpsertUserHistoryEntryHandler,
87
VacuumNodesUserUsageHistoryHandler,
9-
CleanOldUsageRecordsHandler,
108
TruncateNodesUserUsageHistoryHandler,
119
];

src/modules/users/commands/batch-reset-limited-users-traffic/batch-reset-limited-users-traffic.handler.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { ERRORS } from '@contract/constants';
22

3-
import { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
43
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
5-
import { Transactional } from '@nestjs-cls/transactional';
64
import { Logger } from '@nestjs/common';
75

86
import { BatchResetLimitedUsersTrafficCommand } from './batch-reset-limited-users-traffic.command';
@@ -14,10 +12,6 @@ export class BatchResetLimitedUsersTrafficHandler implements ICommandHandler<Bat
1412

1513
constructor(private readonly usersRepository: UsersRepository) {}
1614

17-
@Transactional<TransactionalAdapterPrisma>({
18-
maxWait: 20_000,
19-
timeout: 120_000,
20-
})
2115
async execute(command: BatchResetLimitedUsersTrafficCommand) {
2216
try {
2317
const result = await this.usersRepository.resetLimitedUsersTraffic(command.strategy);

src/modules/users/commands/batch-reset-user-traffic/batch-reset-user-traffic.handler.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { ERRORS } from '@contract/constants';
22

3-
import { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
43
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
5-
import { Transactional } from '@nestjs-cls/transactional';
64
import { Logger } from '@nestjs/common';
75

86
import { ICommandResponse } from '@common/types/command-response.type';
@@ -19,10 +17,6 @@ export class BatchResetUserTrafficHandler implements ICommandHandler<
1917

2018
constructor(private readonly usersRepository: UsersRepository) {}
2119

22-
@Transactional<TransactionalAdapterPrisma>({
23-
maxWait: 20_000,
24-
timeout: 120_000,
25-
})
2620
async execute(command: BatchResetUserTrafficCommand): Promise<ICommandResponse<void>> {
2721
try {
2822
await this.usersRepository.resetUserTraffic(command.strategy);

0 commit comments

Comments
 (0)