Skip to content

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

Bugfix/fix import of empty account balances (#4677)

Alison Hawk31f5c0de8852
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2e20e8da9..55befe0e2b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed an issue where the import button was not correctly enabled in the import activities dialog
+- Fixed an issue with empty account balances in the import activities dialog
- Fixed an issue in the annualized performance calculation
## 2.166.0 - 2025-06-05
diff --git a/apps/api/src/app/import/create-account-with-balances.dto.ts b/apps/api/src/app/import/create-account-with-balances.dto.ts
index fd4b8df488..6cf4057f88 100644
--- a/apps/api/src/app/import/create-account-with-balances.dto.ts
+++ b/apps/api/src/app/import/create-account-with-balances.dto.ts
@@ -6,5 +6,5 @@ import { IsArray, IsOptional } from 'class-validator';
export class CreateAccountWithBalancesDto extends CreateAccountDto {
@IsArray()
@IsOptional()
- balances?: AccountBalance;
+ balances?: AccountBalance[];
}
diff --git a/apps/api/src/app/import/import.controller.ts b/apps/api/src/app/import/import.controller.ts
index 15631a3e8f..07e017bce0 100644
--- a/apps/api/src/app/import/import.controller.ts
+++ b/apps/api/src/app/import/import.controller.ts
@@ -71,7 +71,7 @@ export class ImportController {
const activities = await this.importService.import({
isDryRun,
maxActivitiesToImport,
- accountsDto: importData.accounts ?? [],
+ accountsWithBalancesDto: importData.accounts ?? [],
activitiesDto: importData.activities,
user: this.request.user
});
diff --git a/apps/api/src/app/import/import.service.ts b/apps/api/src/app/import/import.service.ts
index 28c49ca709..6fd48e3eea 100644
--- a/apps/api/src/app/import/import.service.ts
+++ b/apps/api/src/app/import/import.service.ts
@@ -28,9 +28,11 @@ import { Injectable } from '@nestjs/common';
import { DataSource, Prisma, SymbolProfile } from '@prisma/client';
import { Big } from 'big.js';
import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns';
-import { uniqBy } from 'lodash';
+import { omit, uniqBy } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
+import { ImportDataDto } from './import-data.dto';
+
@Injectable()
export class ImportService {
public constructor(
@@ -138,14 +140,14 @@ export class ImportService {
}
public async import({
- accountsDto,
+ accountsWithBalancesDto,
activitiesDto,
isDryRun = false,
maxActivitiesToImport,
user
}: {
- accountsDto: Partial<CreateAccountDto>[];
- activitiesDto: Partial<CreateOrderDto>[];
+ accountsWithBalancesDto: ImportDataDto['accounts'];
+ activitiesDto: ImportDataDto['activities'];
isDryRun?: boolean;
maxActivitiesToImport: number;
user: UserWithSettings;
@@ -153,12 +155,12 @@ export class ImportService {
const accountIdMapping: { [oldAccountId: string]: string } = {};
const userCurrency = user.Settings.settings.baseCurrency;
- if (!isDryRun && accountsDto?.length) {
+ if (!isDryRun && accountsWithBalancesDto?.length) {
const [existingAccounts, existingPlatforms] = await Promise.all([
this.accountService.accounts({
where: {
id: {
- in: accountsDto.map(({ id }) => {
+ in: accountsWithBalancesDto.map(({ id }) => {
return id;
})
}
@@ -167,14 +169,19 @@ export class ImportService {
this.platformService.getPlatforms()
]);
- for (const account of accountsDto) {
+ for (const accountWithBalances of accountsWithBalancesDto) {
// Check if there is any existing account with the same ID
const accountWithSameId = existingAccounts.find((existingAccount) => {
- return existingAccount.id === account.id;
+ return existingAccount.id === accountWithBalances.id;
});
// If there is no account or if the account belongs to a different user then create a new account
if (!accountWithSameId || accountWithSameId.userId !== user.id) {
+ const account: CreateAccountDto = omit(
+ accountWithBalances,
+ 'balances'
+ );
+
let oldAccountId: string;
const platformId = account.platformId;
@@ -187,6 +194,9 @@ export class ImportService {
let accountObject: Prisma.AccountCreateInput = {
...account,
+ balances: {
+ create: accountWithBalances.balances ?? []
+ },
User: { connect: { id: user.id } }
};
@@ -251,7 +261,7 @@ export class ImportService {
);
if (isDryRun) {
- accountsDto.forEach(({ id, name }) => {
+ accountsWithBalancesDto.forEach(({ id, name }) => {
accounts.push({ id, name });
});
}
diff --git a/test/import/ok.json b/test/import/ok.json
index 4bce98ba29..d3e05e610c 100644
--- a/test/import/ok.json
+++ b/test/import/ok.json
@@ -6,6 +6,16 @@
"accounts": [
{
"balance": 2000,
+ "balances": [
+ {
+ "date": "2024-12-31T00:00:00.000Z",
+ "value": 2000
+ },
+ {
+ "date": "2023-12-31T00:00:00.000Z",
+ "value": 1000
+ }
+ ],
"currency": "USD",
"id": "b2d3fe1d-d6a8-41a3-be39-07ef5e9480f0",
"isExcluded": false,