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

* Improve export by applying filters on accounts and tags

* Update changelog

---------

Co-authored-by: Thomas Kaul <4159106+dtslvr@users.noreply.github.com>
This commit is contained in:
csehatt741 2025-03-21 20:58:47 +01:00 committed by GitHub
parent 536b000ff9
commit 198f73db00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 42 deletions

View File

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Improved the export functionality by applying filters on accounts and tags
- Improved the symbol validation in the _Yahoo Finance_ service (get asset profiles) - Improved the symbol validation in the _Yahoo Finance_ service (get asset profiles)
- Refactored `lodash.uniq` with `Array.from(new Set(...))` - Refactored `lodash.uniq` with `Array.from(new Set(...))`
- Refreshed the cryptocurrencies list - Refreshed the cryptocurrencies list

View File

@ -21,10 +21,11 @@ export class ExportController {
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) @UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async export( public async export(
@Query('accounts') filterByAccounts?: string, @Query('accounts') filterByAccounts?: string,
@Query('activityIds') activityIds?: string[], @Query('activityIds') filterByActivityIds?: string,
@Query('assetClasses') filterByAssetClasses?: string, @Query('assetClasses') filterByAssetClasses?: string,
@Query('tags') filterByTags?: string @Query('tags') filterByTags?: string
): Promise<Export> { ): Promise<Export> {
const activityIds = filterByActivityIds?.split(',') ?? [];
const filters = this.apiService.buildFiltersFromQueryParams({ const filters = this.apiService.buildFiltersFromQueryParams({
filterByAccounts, filterByAccounts,
filterByAssetClasses, filterByAssetClasses,

View File

@ -28,6 +28,22 @@ export class ExportService {
}): Promise<Export> { }): Promise<Export> {
const platformsMap: { [platformId: string]: Platform } = {}; 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 = ( const accounts = (
await this.accountService.accounts({ await this.accountService.accounts({
include: { include: {
@ -39,57 +55,55 @@ export class ExportService {
}, },
where: { userId } where: { userId }
}) })
).map( )
({ .filter(({ id }) => {
balance, return activities.length > 0
balances, ? activities.some(({ accountId }) => {
comment, return accountId === id;
currency, })
id, : true;
isExcluded, })
name, .map(
Platform: platform, ({
platformId
}) => {
if (platformId) {
platformsMap[platformId] = platform;
}
return {
balance, balance,
balances: balances.map(({ date, value }) => { balances,
return { date: date.toISOString(), value };
}),
comment, comment,
currency, currency,
id, id,
isExcluded, isExcluded,
name, name,
Platform: platform,
platformId platformId
}; }) => {
} if (platformId) {
); platformsMap[platformId] = platform;
}
let { activities } = await this.orderService.getOrders({ return {
filters, balance,
userCurrency, balances: balances.map(({ date, value }) => {
userId, return { date: date.toISOString(), value };
includeDrafts: true, }),
sortColumn: 'date', comment,
sortDirection: 'asc', currency,
withExcludedAccounts: true id,
}); isExcluded,
name,
if (activityIds) { platformId
activities = activities.filter((activity) => { };
return activityIds.includes(activity.id); }
}); );
}
const tags = (await this.tagService.getTagsForUser(userId)) const tags = (await this.tagService.getTagsForUser(userId))
.filter(({ isUsed }) => { .filter(
return isUsed; ({ id, isUsed }) =>
}) isUsed &&
activities.some((activity) => {
return activity.tags.some(({ id: tagId }) => {
return tagId === id;
});
})
)
.map(({ id, name }) => { .map(({ id, name }) => {
return { return {
id, id,