Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Feature/extend GET all accounts endpoint by search query filter (#5329)

Attila Cseh6de3db08caac
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3cd842aed..0903fd7cdf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Extended the import functionality by tags
+- Introduced fuzzy search for the accounts endpoint
- Improved the language localization for Polish (`pl`)
- Improved the language localization for Spanish (`es`)
diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts
index 51a33fa9ea..7b24ccdcb4 100644
--- a/apps/api/src/app/account/account.controller.ts
+++ b/apps/api/src/app/account/account.controller.ts
@@ -89,6 +89,7 @@ export class AccountController {
public async getAllAccounts(
@Headers(HEADER_KEY_IMPERSONATION.toLowerCase()) impersonationId: string,
@Query('dataSource') filterByDataSource?: string,
+ @Query('query') filterBySearchQuery?: string,
@Query('symbol') filterBySymbol?: string
): Promise<AccountsResponse> {
const impersonationUserId =
@@ -96,6 +97,7 @@ export class AccountController {
const filters = this.apiService.buildFiltersFromQueryParams({
filterByDataSource,
+ filterBySearchQuery,
filterBySymbol
});
diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts
index d1b9af8926..5ad291238e 100644
--- a/apps/api/src/app/portfolio/portfolio.service.ts
+++ b/apps/api/src/app/portfolio/portfolio.service.ts
@@ -220,11 +220,27 @@ export class PortfolioService {
userId: string;
withExcludedAccounts?: boolean;
}): Promise<AccountsResponse> {
- const accounts = await this.getAccounts({
+ let accounts = await this.getAccounts({
filters,
userId,
withExcludedAccounts
});
+
+ const searchQuery = filters.find(({ type }) => {
+ return type === 'SEARCH_QUERY';
+ })?.id;
+
+ if (searchQuery) {
+ const fuse = new Fuse(accounts, {
+ keys: ['name', 'platform.name'],
+ threshold: 0.3
+ });
+
+ accounts = fuse.search(searchQuery).map(({ item }) => {
+ return item;
+ });
+ }
+
let totalBalanceInBaseCurrency = new Big(0);
let totalValueInBaseCurrency = new Big(0);
let transactionCount = 0;