From 87f0944f923fa2fedb998b18d30cf6bb71717750 Mon Sep 17 00:00:00 2001 From: Leonard Mocanu Date: Wed, 8 Nov 2023 12:09:41 +0200 Subject: [PATCH] delegation: fixes get rewards totals route --- services/delegation/package.json | 2 +- .../src/modules/reward/reward.controller.ts | 2 -- .../src/modules/reward/reward.repository.ts | 26 ++++++++++++------- .../src/modules/reward/reward.service.ts | 5 ++-- yarn.lock | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/services/delegation/package.json b/services/delegation/package.json index 66659d486..618434ddd 100644 --- a/services/delegation/package.json +++ b/services/delegation/package.json @@ -48,7 +48,7 @@ "rlp": "^3.0.0", "rxjs": "^7.5.5", "swagger-ui-express": "^4.3.0", - "typeorm": "^0.2.45", + "typeorm": "^0.2.34", "web3": "^1.7.1", "yargs": "^17.3.1" }, diff --git a/services/delegation/src/modules/reward/reward.controller.ts b/services/delegation/src/modules/reward/reward.controller.ts index 8e728e1ea..6e2be49e5 100644 --- a/services/delegation/src/modules/reward/reward.controller.ts +++ b/services/delegation/src/modules/reward/reward.controller.ts @@ -6,13 +6,11 @@ import { TotalReward, RewardRepository } from './reward.repository'; import { RewardService } from './reward.service'; import { Epoch, getEpochs } from './helper/epoch'; import { RewardQueryDto } from './dto/reward-query.dto'; -import { Inject } from '@nestjs/common'; @ApiTags('rewards') @Controller('rewards') export class RewardsController { constructor( - @Inject('RewardRepository') private rewardRepository: RewardRepository, private rewardService: RewardService, ) {} diff --git a/services/delegation/src/modules/reward/reward.repository.ts b/services/delegation/src/modules/reward/reward.repository.ts index 1f5697e98..b2e8b0707 100644 --- a/services/delegation/src/modules/reward/reward.repository.ts +++ b/services/delegation/src/modules/reward/reward.repository.ts @@ -1,18 +1,25 @@ -import { EntityRepository, Raw, Repository } from 'typeorm'; +import { Repository } from 'typeorm'; import { RewardQueryDto } from './dto/reward-query.dto'; import { Reward } from './reward.entity'; +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; export interface TotalReward { address: string; amount: number; } -@EntityRepository(Reward) -export class RewardRepository extends Repository { +@Injectable() +export class RewardRepository { + constructor( + @InjectRepository(Reward) + private readonly usersRepository: Repository, + ) {} async groupByAddress(query: RewardQueryDto): Promise { const { type, epoch, user, address } = query; - let rewardsQuery = this.createQueryBuilder() + let rewardsQuery = this.usersRepository + .createQueryBuilder() .select(['SUM(value) as amount', 'LOWER(user_address) as address']) .groupBy('address') .where('user_address IS NOT NULL') @@ -29,11 +36,12 @@ export class RewardRepository extends Repository { } if (address) { - rewardsQuery = rewardsQuery.andWhere({ - userAddress: Raw((alias) => `LOWER(${alias}) LIKE LOWER(:address)`, { - address, - }), - }); + rewardsQuery = rewardsQuery.andWhere( + `LOWER(user_Address) LIKE LOWER(:address)`, + { + address: `%${address}%`, + }, + ); } return rewardsQuery.getRawMany(); diff --git a/services/delegation/src/modules/reward/reward.service.ts b/services/delegation/src/modules/reward/reward.service.ts index 4d13f3c6a..3f34deecd 100644 --- a/services/delegation/src/modules/reward/reward.service.ts +++ b/services/delegation/src/modules/reward/reward.service.ts @@ -1,7 +1,7 @@ import assert from 'assert'; import moment from 'moment'; import * as ethers from 'ethers'; -import { Raw } from 'typeorm'; +import { Raw, Repository } from 'typeorm'; import { Interval } from '@flatten-js/interval-tree'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; @@ -21,7 +21,6 @@ import { Delegation } from '../delegation/delegation.entity'; import { Node } from '../node/node.entity'; import { NodeCommission } from '../node/node-commission.entity'; import { Reward } from './reward.entity'; -import { RewardRepository } from './reward.repository'; import { RewardType } from './reward-type.enum'; @Injectable() @@ -31,7 +30,7 @@ export class RewardService { private stakingDataService: StakingDataService, private delegationDataService: DelegationDataService, @InjectRepository(Reward) - private rewardRepository: RewardRepository, + private rewardRepository: Repository, private config: ConfigService, private userService: UserService, ) { diff --git a/yarn.lock b/yarn.lock index 4d8a25a27..14495d073 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22541,7 +22541,7 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== -typeorm@^0.2.34, typeorm@^0.2.45: +typeorm@^0.2.34: version "0.2.45" resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.45.tgz#e5bbb3af822dc4646bad96cfa48cd22fa4687cea" integrity sha512-c0rCO8VMJ3ER7JQ73xfk0zDnVv0WDjpsP6Q1m6CVKul7DB9iVdWLRjPzc8v2eaeBuomsbZ2+gTaYr8k1gm3bYA==