Skip to content

Commit 7c1d5d3

Browse files
committed
refactor: simplify user service methods and enhance queue handling
1 parent 02f6f34 commit 7c1d5d3

16 files changed

+235
-52
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Command } from '@nestjs/cqrs';
2+
3+
export class BulkAllExtendExpirationDateCommand extends Command<void> {
4+
constructor(public readonly extendDays: number) {
5+
super();
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
2+
import { Logger } from '@nestjs/common';
3+
4+
import { BulkAllExtendExpirationDateCommand } from './bulk-all-extend-expiration-date.command';
5+
import { UsersRepository } from '../../repositories/users.repository';
6+
7+
@CommandHandler(BulkAllExtendExpirationDateCommand)
8+
export class BulkAllExtendExpirationDateHandler implements ICommandHandler<BulkAllExtendExpirationDateCommand> {
9+
public readonly logger = new Logger(BulkAllExtendExpirationDateHandler.name);
10+
11+
constructor(private readonly usersRepository: UsersRepository) {}
12+
13+
async execute(command: BulkAllExtendExpirationDateCommand) {
14+
try {
15+
await this.usersRepository.bulkAllExtendExpirationDate(command.extendDays);
16+
17+
return;
18+
} catch (error: unknown) {
19+
this.logger.error(error);
20+
return;
21+
}
22+
}
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './bulk-all-extend-expiration-date.command';
2+
export * from './bulk-all-extend-expiration-date.handler';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Command } from '@nestjs/cqrs';
2+
3+
type TBulkSyncUsers = 'limited' | 'expired';
4+
5+
export class BulkSyncUsersCommand extends Command<void> {
6+
constructor(public readonly type: TBulkSyncUsers) {
7+
super();
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
2+
import { Logger } from '@nestjs/common';
3+
4+
import { UsersRepository } from '../../repositories/users.repository';
5+
import { BulkSyncUsersCommand } from './bulk-sync-users.command';
6+
7+
@CommandHandler(BulkSyncUsersCommand)
8+
export class BulkSyncUsersHandler implements ICommandHandler<BulkSyncUsersCommand> {
9+
public readonly logger = new Logger(BulkSyncUsersHandler.name);
10+
11+
constructor(private readonly usersRepository: UsersRepository) {}
12+
13+
async execute(command: BulkSyncUsersCommand) {
14+
try {
15+
if (command.type === 'limited') {
16+
await this.usersRepository.bulkSyncLimitedUsers();
17+
} else if (command.type === 'expired') {
18+
await this.usersRepository.bulkSyncExpiredUsers();
19+
}
20+
21+
return;
22+
} catch (error: unknown) {
23+
this.logger.error(error);
24+
return;
25+
}
26+
}
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './bulk-sync-users.command';
2+
export * from './bulk-sync-users.handler';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Command } from '@nestjs/cqrs';
2+
3+
import { BaseUserEntity } from '@modules/users/entities';
4+
5+
export class BulkUpdateAllUsersCommand extends Command<void> {
6+
constructor(public readonly fields: Partial<BaseUserEntity>) {
7+
super();
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
2+
import { Logger } from '@nestjs/common';
3+
4+
import { BulkUpdateAllUsersCommand } from './bulk-update-all-users.command';
5+
import { UsersRepository } from '../../repositories/users.repository';
6+
7+
@CommandHandler(BulkUpdateAllUsersCommand)
8+
export class BulkUpdateAllUsersHandler implements ICommandHandler<BulkUpdateAllUsersCommand> {
9+
public readonly logger = new Logger(BulkUpdateAllUsersHandler.name);
10+
11+
constructor(private readonly usersRepository: UsersRepository) {}
12+
13+
async execute(command: BulkUpdateAllUsersCommand) {
14+
try {
15+
await this.usersRepository.bulkUpdateAllUsersByRange({
16+
fields: command.fields,
17+
});
18+
19+
return;
20+
} catch (error: unknown) {
21+
this.logger.error(error);
22+
return;
23+
}
24+
}
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './bulk-update-all-users.command';
2+
export * from './bulk-update-all-users.handler';

src/modules/users/commands/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { UpdateSubLastOpenedAndUserAgentHandler } from './update-sub-last-opened-and-user-agent';
22
import { BatchResetLimitedUsersTrafficHandler } from './batch-reset-limited-users-traffic';
33
import { TriggerThresholdNotificationHandler } from './trigger-threshold-notification';
4+
import { BulkAllExtendExpirationDateHandler } from './bulk-all-extend-expiration-date';
45
import { BulkIncrementUsedTrafficHandler } from './bulk-increment-used-traffic';
56
import { UpdateExceededTrafficUsersHandler } from './update-exceeded-users';
67
import { RevokeUserSubscriptionHandler } from './revoke-user-subscription';
78
import { BatchResetUserTrafficHandler } from './batch-reset-user-traffic';
89
import { UpdateUserWithServiceHandler } from './update-user-with-service';
910
import { BulkDeleteByStatusHandler } from './bulk-delete-by-status';
11+
import { BulkUpdateAllUsersHandler } from './bulk-update-all-users';
1012
import { UpdateExpiredUsersHandler } from './update-expired-users';
1113
import { ResetUserTrafficHandler } from './reset-user-traffic';
14+
import { BulkSyncUsersHandler } from './bulk-sync-users';
1215

1316
export const COMMANDS = [
1417
UpdateSubLastOpenedAndUserAgentHandler,
@@ -22,4 +25,7 @@ export const COMMANDS = [
2225
UpdateUserWithServiceHandler,
2326
TriggerThresholdNotificationHandler,
2427
BulkDeleteByStatusHandler,
28+
BulkUpdateAllUsersHandler,
29+
BulkSyncUsersHandler,
30+
BulkAllExtendExpirationDateHandler,
2531
];

0 commit comments

Comments
 (0)