Skip to content

Commit 40010aa

Browse files
committed
refactor: update user commands and repository methods to use tId
1 parent 2b3c417 commit 40010aa

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

src/modules/users/builders/trigger-threshold-notifications-builder/trigger-threshold-notifications-builder.builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class TriggerThresholdNotificationsBuilder {
1515
WITH thresholds(pct) AS (VALUES ${pctValues}),
1616
candidates AS (
1717
SELECT
18-
u."uuid",
18+
u."t_id",
1919
MIN(u."created_at") AS created_at_for_order,
2020
MAX(t.pct) AS new_threshold
2121
FROM "users" u
@@ -26,16 +26,16 @@ export class TriggerThresholdNotificationsBuilder {
2626
AND u."traffic_limit_bytes" > 0
2727
AND ut."used_traffic_bytes" >= (u."traffic_limit_bytes" * t.pct / 100)
2828
AND u."last_triggered_threshold" < t.pct
29-
GROUP BY u."uuid"
29+
GROUP BY u."t_id"
3030
ORDER BY created_at_for_order
3131
LIMIT 5000
3232
)
3333
UPDATE "users" AS u
3434
SET "last_triggered_threshold" = c.new_threshold,
3535
"updated_at" = NOW()
3636
FROM candidates c
37-
WHERE u."uuid" = c."uuid"
38-
RETURNING u."uuid";
37+
WHERE u."t_id" = c."t_id"
38+
RETURNING u."t_id" AS "tId";
3939
`;
4040
}
4141
}

src/modules/users/commands/trigger-threshold-notification/trigger-threshold-notification.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UsersRepository } from '../../repositories/users.repository';
1313
@CommandHandler(TriggerThresholdNotificationCommand)
1414
export class TriggerThresholdNotificationHandler
1515
implements
16-
ICommandHandler<TriggerThresholdNotificationCommand, ICommandResponse<{ uuid: string }[]>>
16+
ICommandHandler<TriggerThresholdNotificationCommand, ICommandResponse<{ tId: bigint }[]>>
1717
{
1818
public readonly logger = new Logger(TriggerThresholdNotificationHandler.name);
1919

@@ -25,7 +25,7 @@ export class TriggerThresholdNotificationHandler
2525
})
2626
async execute(
2727
command: TriggerThresholdNotificationCommand,
28-
): Promise<ICommandResponse<{ uuid: string }[]>> {
28+
): Promise<ICommandResponse<{ tId: bigint }[]>> {
2929
try {
3030
const result = await this.usersRepository.triggerThresholdNotifications(
3131
command.percentages,

src/modules/users/commands/update-exceeded-users/update-exceeded-users.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UsersRepository } from '../../repositories/users.repository';
1313
@CommandHandler(UpdateExceededTrafficUsersCommand)
1414
export class UpdateExceededTrafficUsersHandler
1515
implements
16-
ICommandHandler<UpdateExceededTrafficUsersCommand, ICommandResponse<{ uuid: string }[]>>
16+
ICommandHandler<UpdateExceededTrafficUsersCommand, ICommandResponse<{ tId: bigint }[]>>
1717
{
1818
public readonly logger = new Logger(UpdateExceededTrafficUsersHandler.name);
1919

@@ -23,7 +23,7 @@ export class UpdateExceededTrafficUsersHandler
2323
maxWait: 20_000,
2424
timeout: 120_000,
2525
})
26-
async execute(): Promise<ICommandResponse<{ uuid: string }[]>> {
26+
async execute(): Promise<ICommandResponse<{ tId: bigint }[]>> {
2727
try {
2828
const result = await this.usersRepository.updateExceededTrafficUsers();
2929

src/modules/users/commands/update-expired-users/update-expired-users.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { UsersRepository } from '../../repositories/users.repository';
1212

1313
@CommandHandler(UpdateExpiredUsersCommand)
1414
export class UpdateExpiredUsersHandler
15-
implements ICommandHandler<UpdateExpiredUsersCommand, ICommandResponse<{ uuid: string }[]>>
15+
implements ICommandHandler<UpdateExpiredUsersCommand, ICommandResponse<{ tId: bigint }[]>>
1616
{
1717
public readonly logger = new Logger(UpdateExpiredUsersHandler.name);
1818

@@ -22,7 +22,7 @@ export class UpdateExpiredUsersHandler
2222
maxWait: 20_000,
2323
timeout: 120_000,
2424
})
25-
async execute(): Promise<ICommandResponse<{ uuid: string }[]>> {
25+
async execute(): Promise<ICommandResponse<{ tId: bigint }[]>> {
2626
try {
2727
const result = await this.usersRepository.updateExpiredUsers();
2828

src/modules/users/repositories/users.repository.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ export class UsersRepository {
8383
return result;
8484
}
8585

86-
public async triggerThresholdNotifications(percentages: number[]): Promise<{ uuid: string }[]> {
86+
public async triggerThresholdNotifications(percentages: number[]): Promise<{ tId: bigint }[]> {
8787
const { query } = new TriggerThresholdNotificationsBuilder(percentages);
88-
return await this.prisma.tx.$queryRaw<{ uuid: string }[]>(query);
88+
return await this.prisma.tx.$queryRaw<{ tId: bigint }[]>(query);
8989
}
9090

9191
public async updateStatusAndTrafficAndResetAt(
@@ -121,7 +121,7 @@ export class UsersRepository {
121121
.execute();
122122
}
123123

124-
public async updateExceededTrafficUsers(): Promise<{ uuid: string }[]> {
124+
public async updateExceededTrafficUsers(): Promise<{ tId: bigint }[]> {
125125
const result = await this.qb.kysely
126126
.updateTable('users')
127127
.set({ status: USERS_STATUS.LIMITED })
@@ -130,7 +130,7 @@ export class UsersRepository {
130130
.where('users.status', '=', USERS_STATUS.ACTIVE)
131131
.where('users.trafficLimitBytes', '!=', 0n)
132132
.whereRef('userTraffic.usedTrafficBytes', '>=', 'users.trafficLimitBytes')
133-
.returning(['users.uuid'])
133+
.returning(['users.tId'])
134134
.execute();
135135

136136
return result;
@@ -148,11 +148,11 @@ export class UsersRepository {
148148
return result.map((value) => new UserEntity(value));
149149
}
150150

151-
public async updateExpiredUsers(): Promise<{ uuid: string }[]> {
151+
public async updateExpiredUsers(): Promise<{ tId: bigint }[]> {
152152
// UPDATE "public"."users" SET "status" = $1, "updated_at" = $2 WHERE ("public"."users"."status" IN ($3,$4) AND "public"."users"."expire_at" < $5) RETURNING "public"."users"."uuid"
153153
const result = await this.prisma.tx.users.updateManyAndReturn({
154154
select: {
155-
uuid: true,
155+
tId: true,
156156
},
157157
where: {
158158
AND: [

src/queue/user-jobs/user-jobs.processor.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ export class UserJobsQueueProcessor extends WorkerHost {
8787
`Job ${job.name} Found ${usersResponse.response.length} expired users.`,
8888
);
8989

90-
await pMap(usersResponse.response, async (userUuid) => {
90+
await pMap(usersResponse.response, async (userIterator) => {
9191
try {
9292
const userResponse = await this.queryBus.execute(
9393
new GetUserByUniqueFieldQuery(
9494
{
95-
uuid: userUuid.uuid,
95+
tId: userIterator.tId,
9696
},
9797
{
9898
activeInternalSquads: true,
@@ -161,11 +161,11 @@ export class UserJobsQueueProcessor extends WorkerHost {
161161
`Job ${job.name} has found ${usersResponse.response.length} exceeded traffic usage users.`,
162162
);
163163

164-
await pMap(usersResponse.response, async (userUuid) => {
164+
await pMap(usersResponse.response, async (userIterator) => {
165165
try {
166166
const userResponse = await this.queryBus.execute(
167167
new GetUserByUniqueFieldQuery({
168-
uuid: userUuid.uuid,
168+
tId: userIterator.tId,
169169
}),
170170
);
171171

@@ -246,11 +246,11 @@ export class UserJobsQueueProcessor extends WorkerHost {
246246

247247
await pMap(
248248
usersResponse.response,
249-
async (userUuid) => {
249+
async (userIterator) => {
250250
try {
251251
const userResponse = await this.queryBus.execute(
252252
new GetUserByUniqueFieldQuery({
253-
uuid: userUuid.uuid,
253+
tId: userIterator.tId,
254254
}),
255255
);
256256

@@ -275,7 +275,7 @@ export class UserJobsQueueProcessor extends WorkerHost {
275275
);
276276
}
277277
},
278-
{ concurrency: 100 },
278+
{ concurrency: 80 },
279279
);
280280

281281
continue;
@@ -358,26 +358,26 @@ export class UserJobsQueueProcessor extends WorkerHost {
358358
}
359359
}
360360

361-
private async updateExpiredUsers(): Promise<ICommandResponse<{ uuid: string }[]>> {
361+
private async updateExpiredUsers(): Promise<ICommandResponse<{ tId: bigint }[]>> {
362362
return this.commandBus.execute<
363363
UpdateExpiredUsersCommand,
364-
ICommandResponse<{ uuid: string }[]>
364+
ICommandResponse<{ tId: bigint }[]>
365365
>(new UpdateExpiredUsersCommand());
366366
}
367367

368-
private async updateExceededTrafficUsers(): Promise<ICommandResponse<{ uuid: string }[]>> {
368+
private async updateExceededTrafficUsers(): Promise<ICommandResponse<{ tId: bigint }[]>> {
369369
return this.commandBus.execute<
370370
UpdateExceededTrafficUsersCommand,
371-
ICommandResponse<{ uuid: string }[]>
371+
ICommandResponse<{ tId: bigint }[]>
372372
>(new UpdateExceededTrafficUsersCommand());
373373
}
374374

375375
private async triggerThresholdNotifications(
376376
percentages: number[],
377-
): Promise<ICommandResponse<{ uuid: string }[]>> {
377+
): Promise<ICommandResponse<{ tId: bigint }[]>> {
378378
return this.commandBus.execute<
379379
TriggerThresholdNotificationCommand,
380-
ICommandResponse<{ uuid: string }[]>
380+
ICommandResponse<{ tId: bigint }[]>
381381
>(new TriggerThresholdNotificationCommand(percentages));
382382
}
383383
}

0 commit comments

Comments
 (0)