@@ -6,12 +6,13 @@ import { Injectable, Logger } from '@nestjs/common';
66import { UserEvent } from '@intergration-modules/telegram-bot/events/users/interfaces' ;
77import { formatExecutionTime , getTime } from '@common/utils/get-elapsed-time' ;
88import { ICommandResponse } from '@common/types/command-response.type' ;
9- import { EVENTS , USERS_STATUS } from '@libs/contracts/constants' ;
9+ import { EVENTS } from '@libs/contracts/constants' ;
1010import { UserWithActiveInboundsEntity } from '@modules/users/entities/user-with-active-inbounds.entity' ;
11- import { ChangeUserStatusCommand } from '@modules/users/commands/change-user-status/change-user-status.command' ;
1211import { RemoveUserFromNodeEvent } from '@modules/nodes/events/remove-user-from-node' ;
1312import { JOBS_INTERVALS } from 'src/jobs/intervals' ;
14- import { GetExpiredUsersQuery } from '@modules/users/queries/get-expired-users' ;
13+ import { UpdateExpiredUsersCommand } from '@modules/users/commands/update-expired-users' ;
14+ import { GetUserByUuidQuery } from '@modules/users/queries/get-user-by-uuid' ;
15+ import { StartAllNodesEvent } from '@modules/nodes/events/start-all-nodes' ;
1516
1617@Injectable ( )
1718export class FindExpiredUsersService {
@@ -50,21 +51,39 @@ export class FindExpiredUsersService {
5051 const ct = getTime ( ) ;
5152 this . isJobRunning = true ;
5253
53- const usersResponse = await this . getAllExpiredUsers ( ) ;
54+ const usersResponse = await this . updateExpiredUsers ( ) ;
5455 if ( ! usersResponse . isOk || ! usersResponse . response ) {
5556 this . logger . error ( 'No expired users found' ) ;
5657 return ;
5758 }
5859
60+ const updatedUsers = usersResponse . response ;
61+
62+ if ( updatedUsers . length === 0 ) {
63+ this . logger . debug ( 'No expired users found' ) ;
64+ return ;
65+ }
66+
5967 const users = usersResponse . response ;
6068
61- for ( const user of users ) {
62- await this . changeUserStatus ( {
63- userUuid : user . uuid ,
64- status : USERS_STATUS . EXPIRED ,
65- } ) ;
69+ if ( users . length >= 10_000 ) {
70+ this . logger . log (
71+ 'More than 10,000 expired users found, skipping webhook/telegram events.' ,
72+ ) ;
73+
74+ this . eventBus . publish ( new StartAllNodesEvent ( ) ) ;
75+
76+ return ;
77+ }
78+
79+ for ( const userUuid of users ) {
80+ const userResponse = await this . getUserByUuid ( userUuid . uuid ) ;
81+ if ( ! userResponse . isOk || ! userResponse . response ) {
82+ this . logger . debug ( 'User not found' ) ;
83+ continue ;
84+ }
6685
67- user . status = USERS_STATUS . EXPIRED ;
86+ const user = userResponse . response ;
6887
6988 this . eventEmitter . emit (
7089 EVENTS . USER . EXPIRED ,
@@ -82,16 +101,19 @@ export class FindExpiredUsersService {
82101 }
83102 }
84103
85- private async getAllExpiredUsers ( ) : Promise < ICommandResponse < UserWithActiveInboundsEntity [ ] > > {
104+ private async getUserByUuid (
105+ uuid : string ,
106+ ) : Promise < ICommandResponse < UserWithActiveInboundsEntity > > {
86107 return this . queryBus . execute <
87- GetExpiredUsersQuery ,
88- ICommandResponse < UserWithActiveInboundsEntity [ ] >
89- > ( new GetExpiredUsersQuery ( ) ) ;
108+ GetUserByUuidQuery ,
109+ ICommandResponse < UserWithActiveInboundsEntity >
110+ > ( new GetUserByUuidQuery ( uuid ) ) ;
90111 }
91112
92- private async changeUserStatus ( dto : ChangeUserStatusCommand ) : Promise < ICommandResponse < void > > {
93- return this . commandBus . execute < ChangeUserStatusCommand , ICommandResponse < void > > (
94- new ChangeUserStatusCommand ( dto . userUuid , dto . status ) ,
95- ) ;
113+ private async updateExpiredUsers ( ) : Promise < ICommandResponse < { uuid : string } [ ] > > {
114+ return this . commandBus . execute <
115+ UpdateExpiredUsersCommand ,
116+ ICommandResponse < { uuid : string } [ ] >
117+ > ( new UpdateExpiredUsersCommand ( ) ) ;
96118 }
97119}
0 commit comments