Skip to content

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

Feature/improve export by applying filters on accounts and tags (#4425)

csehatt741198f73db004f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d24ce5d6d..80f8311191 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
+- Improved the export functionality by applying filters on accounts and tags
- Improved the symbol validation in the _Yahoo Finance_ service (get asset profiles)
- Refactored `lodash.uniq` with `Array.from(new Set(...))`
- Refreshed the cryptocurrencies list
diff --git a/apps/api/src/app/export/export.controller.ts b/apps/api/src/app/export/export.controller.ts
index 551b3e489a..d807132c9c 100644
--- a/apps/api/src/app/export/export.controller.ts
+++ b/apps/api/src/app/export/export.controller.ts
@@ -21,10 +21,11 @@ export class ExportController {
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async export(
@Query('accounts') filterByAccounts?: string,
- @Query('activityIds') activityIds?: string[],
+ @Query('activityIds') filterByActivityIds?: string,
@Query('assetClasses') filterByAssetClasses?: string,
@Query('tags') filterByTags?: string
): Promise<Export> {
+ const activityIds = filterByActivityIds?.split(',') ?? [];
const filters = this.apiService.buildFiltersFromQueryParams({
filterByAccounts,
filterByAssetClasses,
diff --git a/apps/api/src/app/export/export.service.ts b/apps/api/src/app/export/export.service.ts
index 8b9d2c56c0..f0449dc14c 100644
--- a/apps/api/src/app/export/export.service.ts
+++ b/apps/api/src/app/export/export.service.ts
@@ -28,6 +28,22 @@ export class ExportService {
}): Promise<Export> {
const platformsMap: { [platformId: string]: Platform } = {};
+ let { activities } = await this.orderService.getOrders({
+ filters,
+ userCurrency,
+ userId,
+ includeDrafts: true,
+ sortColumn: 'date',
+ sortDirection: 'asc',
+ withExcludedAccounts: true
+ });
+
+ if (activityIds?.length > 0) {
+ activities = activities.filter(({ id }) => {
+ return activityIds.includes(id);
+ });
+ }
+
const accounts = (
await this.accountService.accounts({
include: {
@@ -39,57 +55,55 @@ export class ExportService {
},
where: { userId }
})
- ).map(
- ({
- balance,
- balances,
- comment,
- currency,
- id,
- isExcluded,
- name,
- Platform: platform,
- platformId
- }) => {
- if (platformId) {
- platformsMap[platformId] = platform;
- }
-
- return {
+ )
+ .filter(({ id }) => {
+ return activities.length > 0
+ ? activities.some(({ accountId }) => {
+ return accountId === id;
+ })
+ : true;
+ })
+ .map(
+ ({
balance,
- balances: balances.map(({ date, value }) => {
- return { date: date.toISOString(), value };
- }),
+ balances,
comment,
currency,
id,
isExcluded,
name,
+ Platform: platform,
platformId
- };
- }
- );
-
- let { activities } = await this.orderService.getOrders({
- filters,
- userCurrency,
- userId,
- includeDrafts: true,
- sortColumn: 'date',
- sortDirection: 'asc',
- withExcludedAccounts: true
- });
+ }) => {
+ if (platformId) {
+ platformsMap[platformId] = platform;
+ }
- if (activityIds) {
- activities = activities.filter((activity) => {
- return activityIds.includes(activity.id);
- });
- }
+ return {
+ balance,
+ balances: balances.map(({ date, value }) => {
+ return { date: date.toISOString(), value };
+ }),
+ comment,
+ currency,
+ id,
+ isExcluded,
+ name,
+ platformId
+ };
+ }
+ );
const tags = (await this.tagService.getTagsForUser(userId))
- .filter(({ isUsed }) => {
- return isUsed;
- })
+ .filter(
+ ({ id, isUsed }) =>
+ isUsed &&
+ activities.some((activity) => {
+ return activity.tags.some(({ id: tagId }) => {
+ return tagId === id;
+ });
+ })
+ )
.map(({ id, name }) => {
return {
id,