Skip to content

Commit 26038f1

Browse files
authored
Enhance account filtering by adding 'withBalance' query option to account endpoints and update ElasticIndexerHelper to support balance filtering. This allows users to filter accounts based on their balance status. (#1521)
1 parent a8dafce commit 26038f1

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/common/indexer/elastic/elastic.indexer.helper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AddressUtils, BinaryUtils } from "@multiversx/sdk-nestjs-common";
2-
import { AbstractQuery, ElasticQuery, MatchQuery, QueryConditionOptions, QueryOperator, QueryType, RangeGreaterThanOrEqual, RangeLowerThan, RangeLowerThanOrEqual } from "@multiversx/sdk-nestjs-elastic";
2+
import { AbstractQuery, ElasticQuery, MatchQuery, QueryConditionOptions, QueryOperator, QueryType, RangeGreaterThan, RangeGreaterThanOrEqual, RangeLowerThan, RangeLowerThanOrEqual } from "@multiversx/sdk-nestjs-elastic";
33
import { Injectable } from "@nestjs/common";
44
import { ApiConfigService } from "src/common/api-config/api.config.service";
55
import { QueryPagination } from "src/common/entities/query.pagination";
@@ -717,6 +717,14 @@ export class ElasticIndexerHelper {
717717
elasticQuery = elasticQuery.withSearchWildcardCondition(filter.search, ['address', 'api_assets.name']);
718718
}
719719

720+
if (filter.withBalance !== undefined) {
721+
if (filter.withBalance) {
722+
elasticQuery = elasticQuery.withRangeFilter('balanceNum', new RangeGreaterThan(0));
723+
} else {
724+
elasticQuery = elasticQuery.withMustCondition(QueryType.Match('balanceNum', 0));
725+
}
726+
}
727+
720728
return elasticQuery;
721729
}
722730

src/endpoints/accounts/account.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export class AccountController {
150150
@ApiQuery({ name: 'search', description: 'Search by account address, assets name', required: false })
151151
@ApiQuery({ name: 'excludeTags', description: 'Exclude specific tags from result', required: false })
152152
@ApiQuery({ name: 'hasAssets', description: 'Returns a list of accounts that have assets', required: false })
153+
@ApiQuery({ name: 'withBalance', description: 'Filter accounts by balance (true = balance > 0, false = balance = 0)', required: false, type: Boolean })
153154
async getAccountsCount(
154155
@Query("ownerAddress", ParseAddressPipe) ownerAddress?: string,
155156
@Query("isSmartContract", ParseBoolPipe) isSmartContract?: boolean,
@@ -158,6 +159,7 @@ export class AccountController {
158159
@Query("excludeTags", ParseArrayPipe) excludeTags?: string[],
159160
@Query("hasAssets", ParseBoolPipe) hasAssets?: boolean,
160161
@Query("search") search?: string,
162+
@Query("withBalance", ParseBoolPipe) withBalance?: boolean,
161163
): Promise<number> {
162164
return await this.accountService.getAccountsCount(
163165
new AccountQueryOptions(
@@ -169,6 +171,7 @@ export class AccountController {
169171
excludeTags,
170172
hasAssets,
171173
search,
174+
withBalance,
172175
}));
173176
}
174177

@@ -182,6 +185,7 @@ export class AccountController {
182185
@Query("excludeTags", ParseArrayPipe) excludeTags?: string[],
183186
@Query("hasAssets", ParseBoolPipe) hasAssets?: boolean,
184187
@Query("search") search?: string,
188+
@Query("withBalance", ParseBoolPipe) withBalance?: boolean,
185189
): Promise<number> {
186190
return await this.accountService.getAccountsCount(
187191
new AccountQueryOptions(
@@ -193,6 +197,7 @@ export class AccountController {
193197
excludeTags,
194198
hasAssets,
195199
search,
200+
withBalance,
196201
}));
197202
}
198203

src/endpoints/accounts/entities/account.query.options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class AccountQueryOptions {
2222
excludeTags?: string[];
2323
hasAssets?: boolean;
2424
search?: string;
25+
withBalance?: boolean;
2526

2627
validate(size: number) {
2728
if (this.withDeployInfo && size > 25) {
@@ -53,6 +54,7 @@ export class AccountQueryOptions {
5354
this.excludeTags !== undefined ||
5455
this.hasAssets !== undefined ||
5556
this.search !== undefined ||
56-
this.addresses !== undefined;
57+
this.addresses !== undefined ||
58+
this.withBalance !== undefined;
5759
}
5860
}

0 commit comments

Comments
 (0)