From 4158bd46aa4c2c4c889c823b8e681da5fb210845 Mon Sep 17 00:00:00 2001 From: Divit Periwal Date: Mon, 9 Nov 2026 19:49:06 +0530 Subject: [PATCH 1/5] Fix text columns in CSV import of activities --- .../import-activities.service.spec.ts | 83 +++++++++++++++++++ .../app/services/import-activities.service.ts | 31 ++++--- 2 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 apps/client/src/app/services/import-activities.service.spec.ts diff --git a/apps/client/src/app/services/import-activities.service.spec.ts b/apps/client/src/app/services/import-activities.service.spec.ts new file mode 100644 index 0000000000..32fc365432 --- /dev/null +++ b/apps/client/src/app/services/import-activities.service.spec.ts @@ -0,0 +1,83 @@ +import { HttpClient } from '@angular/common/http'; +import { TestBed } from '@angular/core/testing'; +import { of } from 'rxjs'; + +import { ImportActivitiesService } from './import-activities.service'; + +jest.mock('uuid', () => { + return { v4: () => 'uuid' }; +}); + +describe('ImportActivitiesService', () => { + let post: jest.Mock; + let service: ImportActivitiesService; + + beforeEach(() => { + post = jest.fn().mockReturnValue(of({ activities: [] })); + + TestBed.configureTestingModule({ + providers: [{ provide: HttpClient, useValue: { post } }] + }); + + service = TestBed.inject(ImportActivitiesService); + }); + + it('should keep text columns and ISO dates as they are in the CSV', async () => { + await service.importCsv({ + fileContent: [ + 'Date,Symbol,Currency,Price,Quantity,Type,Fee,Comment,Account', + '2021-09-01T00:00:00.000Z,AAPL,USD,150.5,2,buy,1,007,01' + ].join('\n'), + isDryRun: true, + userAccounts: [{ id: 'account-01', name: '01' } as any] + }); + + const [activity] = post.mock.calls[0][1].activities; + + expect(activity).toMatchObject({ + accountId: 'account-01', + comment: '007', + date: '2021-09-01T00:00:00.000Z', + fee: 1, + quantity: 2, + unitPrice: 150.5 + }); + }); + + it('should skip empty cells and use the next column', async () => { + await service.importCsv({ + fileContent: [ + 'Date,Code,Symbol,Ccy,Currency,Price,Quantity,Type,Fee,Comment,Note,Account,AccountId', + '2021-09-01,,AAPL,,USD,150.5,2,buy,1,,hello,,account-01' + ].join('\n'), + isDryRun: true, + userAccounts: [{ id: 'account-01', name: '01' } as any] + }); + + const [activity] = post.mock.calls[0][1].activities; + + expect(activity).toMatchObject({ + accountId: 'account-01', + comment: 'hello', + currency: 'USD', + symbol: 'AAPL' + }); + }); + + it('should reject a fee without a symbol', async () => { + await expect( + service.importCsv({ + fileContent: [ + 'Date,Code,Currency,Price,Quantity,Type,Fee', + '2021-09-01,,USD,0,0,fee,49' + ].join('\n'), + isDryRun: true, + userAccounts: [] + }) + ).rejects.toMatchObject({ + message: 'activities.0.symbol is not valid' + }); + + expect(post).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/client/src/app/services/import-activities.service.ts b/apps/client/src/app/services/import-activities.service.ts index 06ca922c1f..62c5c8b3c5 100644 --- a/apps/client/src/app/services/import-activities.service.ts +++ b/apps/client/src/app/services/import-activities.service.ts @@ -66,9 +66,18 @@ export class ImportActivitiesService { assetProfiles: CreateAssetProfileWithMarketDataDto[]; }> { const content = csvToJson>(fileContent, { - dynamicTyping: true, + dynamicTyping: (field) => { + return [ + ...ImportActivitiesService.FEE_KEYS, + ...ImportActivitiesService.QUANTITY_KEYS, + ...ImportActivitiesService.UNIT_PRICE_KEYS + ].includes(String(field).toLowerCase()); + }, header: true, - skipEmptyLines: true + skipEmptyLines: true, + transform: (value) => { + return value === '' ? null : value; + } }).data; const activities: CreateOrderDto[] = []; @@ -260,11 +269,9 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.ACCOUNT_KEYS) { const value = item[key]; - if (isNumber(value) || isString(value)) { + if (isString(value)) { return userAccounts.find(({ id, name }) => { - return ( - id === value || name?.toLowerCase() === String(value).toLowerCase() - ); + return id === value || name?.toLowerCase() === value.toLowerCase(); })?.id; } } @@ -278,8 +285,8 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.COMMENT_KEYS) { const value = item[key]; - if (isNumber(value) || isString(value)) { - return String(value); + if (isString(value)) { + return value; } } @@ -339,9 +346,9 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.DATE_KEYS) { const value = item[key]; - if (isNumber(value) || isString(value)) { + if (isString(value)) { try { - const parsedDate = parseDateHelper(String(value)); + const parsedDate = parseDateHelper(value); if (parsedDate) { return parsedDate.toISOString(); @@ -420,8 +427,8 @@ export class ImportActivitiesService { for (const key of ImportActivitiesService.SYMBOL_KEYS) { const value = item[key]; - if (isNumber(value) || isString(value)) { - return String(value); + if (isString(value)) { + return value; } } From 6755f49e61141354d3fb5bc6f60c49d466c887cc Mon Sep 17 00:00:00 2001 From: Divit Periwal Date: Mon, 9 Nov 2026 19:49:06 +0530 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7bb0de143..cf03825a18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed the parsing of text columns and dates with a time in the import of activities from CSV files + ## 3.99.0 - 2026-11-07 ### Added From 2ef59ee3aa3460bcebff645f29d2774ba481c6fc Mon Sep 17 00:00:00 2001 From: Divit Periwal Date: Mon, 9 Nov 2026 22:25:22 +0530 Subject: [PATCH 3/5] Trim cells in CSV import of activities --- apps/client/src/app/services/import-activities.service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/client/src/app/services/import-activities.service.ts b/apps/client/src/app/services/import-activities.service.ts index 62c5c8b3c5..ccb4f80843 100644 --- a/apps/client/src/app/services/import-activities.service.ts +++ b/apps/client/src/app/services/import-activities.service.ts @@ -76,7 +76,9 @@ export class ImportActivitiesService { header: true, skipEmptyLines: true, transform: (value) => { - return value === '' ? null : value; + const trimmedValue = value.trim(); + + return trimmedValue === '' ? null : trimmedValue; } }).data; From 4dd1e2b1dd593ae412559b16c12b23747e32d09a Mon Sep 17 00:00:00 2001 From: Divit Periwal Date: Mon, 9 Nov 2026 22:25:23 +0530 Subject: [PATCH 4/5] Extend tests --- .../import-activities.service.spec.ts | 69 +++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/apps/client/src/app/services/import-activities.service.spec.ts b/apps/client/src/app/services/import-activities.service.spec.ts index 32fc365432..92c76701e2 100644 --- a/apps/client/src/app/services/import-activities.service.spec.ts +++ b/apps/client/src/app/services/import-activities.service.spec.ts @@ -1,6 +1,9 @@ +import { CreateOrderDto } from '@ghostfolio/common/dtos'; +import type { Account } from '@ghostfolio/prisma/browser'; + import { HttpClient } from '@angular/common/http'; import { TestBed } from '@angular/core/testing'; -import { of } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { ImportActivitiesService } from './import-activities.service'; @@ -8,12 +11,16 @@ jest.mock('uuid', () => { return { v4: () => 'uuid' }; }); +type PostArguments = [string, { activities: CreateOrderDto[] }]; + describe('ImportActivitiesService', () => { - let post: jest.Mock; + let post: jest.Mock, PostArguments>; let service: ImportActivitiesService; beforeEach(() => { - post = jest.fn().mockReturnValue(of({ activities: [] })); + post = jest + .fn, PostArguments>() + .mockReturnValue(of({ activities: [] })); TestBed.configureTestingModule({ providers: [{ provide: HttpClient, useValue: { post } }] @@ -29,7 +36,7 @@ describe('ImportActivitiesService', () => { '2021-09-01T00:00:00.000Z,AAPL,USD,150.5,2,buy,1,007,01' ].join('\n'), isDryRun: true, - userAccounts: [{ id: 'account-01', name: '01' } as any] + userAccounts: [{ id: 'account-01', name: '01' } as Account] }); const [activity] = post.mock.calls[0][1].activities; @@ -44,6 +51,40 @@ describe('ImportActivitiesService', () => { }); }); + it('should keep symbols which look like a number or a boolean', async () => { + await service.importCsv({ + fileContent: [ + 'Date,Symbol,Currency,Price,Quantity,Type,Fee', + '2021-09-01,0050,USD,150.5,2,buy,1', + '2021-09-01,TRUE,USD,150.5,2,buy,1', + '2021-09-01,7203,USD,150.5,2,buy,1' + ].join('\n'), + isDryRun: true, + userAccounts: [] + }); + + const symbols = post.mock.calls[0][1].activities.map(({ symbol }) => { + return symbol; + }); + + expect(symbols).toEqual(['0050', 'TRUE', '7203']); + }); + + it('should parse a date with an offset', async () => { + await service.importCsv({ + fileContent: [ + 'Date,Symbol,Currency,Price,Quantity,Type,Fee', + '2021-09-01T10:00:00+02:00,AAPL,USD,150.5,2,buy,1' + ].join('\n'), + isDryRun: true, + userAccounts: [] + }); + + const [activity] = post.mock.calls[0][1].activities; + + expect(activity.date).toBe('2021-09-01T08:00:00.000Z'); + }); + it('should skip empty cells and use the next column', async () => { await service.importCsv({ fileContent: [ @@ -51,7 +92,7 @@ describe('ImportActivitiesService', () => { '2021-09-01,,AAPL,,USD,150.5,2,buy,1,,hello,,account-01' ].join('\n'), isDryRun: true, - userAccounts: [{ id: 'account-01', name: '01' } as any] + userAccounts: [{ id: 'account-01', name: '01' } as Account] }); const [activity] = post.mock.calls[0][1].activities; @@ -64,6 +105,24 @@ describe('ImportActivitiesService', () => { }); }); + it('should trim cells and skip cells with only spaces', async () => { + await service.importCsv({ + fileContent: [ + 'Date,Code,Symbol,Currency,Price,Quantity,Type,Fee', + ' 20230403 , , 7203 ,USD,150.5,2,buy,1' + ].join('\n'), + isDryRun: true, + userAccounts: [] + }); + + const [activity] = post.mock.calls[0][1].activities; + + expect(activity).toMatchObject({ + date: new Date(2023, 3, 3).toISOString(), + symbol: '7203' + }); + }); + it('should reject a fee without a symbol', async () => { await expect( service.importCsv({ From a0b15146ba2dcb421b7b5723afd6f3cdd294fafd Mon Sep 17 00:00:00 2001 From: Divit Periwal Date: Mon, 9 Nov 2026 22:25:23 +0530 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf03825a18..50851c7d73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed the parsing of text columns and dates with a time in the import of activities from CSV files +- Fixed the parsing of text columns and dates with a time zone in the import of activities from CSV files ## 3.99.0 - 2026-11-07