Browse Source

Merge 79900ea3f3 into 618e6c9d27

pull/8433/merge
Divit Periwal 17 hours ago
committed by GitHub
parent
commit
a8925eb6a2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      CHANGELOG.md
  2. 142
      apps/client/src/app/services/import-activities.service.spec.ts
  3. 33
      apps/client/src/app/services/import-activities.service.ts

4
CHANGELOG.md

@ -31,6 +31,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Upgraded `stripe` from version `23.0.1` to `23.1.0` - Upgraded `stripe` from version `23.0.1` to `23.1.0`
- Upgraded `undici` from version `8.12.3` to `8.12.4` - Upgraded `undici` from version `8.12.3` to `8.12.4`
### Fixed
- 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 ## 3.99.0 - 2026-11-07
### Added ### Added

142
apps/client/src/app/services/import-activities.service.spec.ts

@ -0,0 +1,142 @@
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 { Observable, of } from 'rxjs';
import { ImportActivitiesService } from './import-activities.service';
jest.mock('uuid', () => {
return { v4: () => 'uuid' };
});
type PostArguments = [string, { activities: CreateOrderDto[] }];
describe('ImportActivitiesService', () => {
let post: jest.Mock<Observable<unknown>, PostArguments>;
let service: ImportActivitiesService;
beforeEach(() => {
post = jest
.fn<Observable<unknown>, PostArguments>()
.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 Account]
});
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 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: [
'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 Account]
});
const [activity] = post.mock.calls[0][1].activities;
expect(activity).toMatchObject({
accountId: 'account-01',
comment: 'hello',
currency: 'USD',
symbol: 'AAPL'
});
});
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({
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();
});
});

33
apps/client/src/app/services/import-activities.service.ts

@ -66,9 +66,20 @@ export class ImportActivitiesService {
assetProfiles: CreateAssetProfileWithMarketDataDto[]; assetProfiles: CreateAssetProfileWithMarketDataDto[];
}> { }> {
const content = csvToJson<Record<string, unknown>>(fileContent, { const content = csvToJson<Record<string, unknown>>(fileContent, {
dynamicTyping: true, dynamicTyping: (field) => {
return [
...ImportActivitiesService.FEE_KEYS,
...ImportActivitiesService.QUANTITY_KEYS,
...ImportActivitiesService.UNIT_PRICE_KEYS
].includes(String(field).toLowerCase());
},
header: true, header: true,
skipEmptyLines: true skipEmptyLines: true,
transform: (value) => {
const trimmedValue = value.trim();
return trimmedValue === '' ? null : trimmedValue;
}
}).data; }).data;
const activities: CreateOrderDto[] = []; const activities: CreateOrderDto[] = [];
@ -260,11 +271,9 @@ export class ImportActivitiesService {
for (const key of ImportActivitiesService.ACCOUNT_KEYS) { for (const key of ImportActivitiesService.ACCOUNT_KEYS) {
const value = item[key]; const value = item[key];
if (isNumber(value) || isString(value)) { if (isString(value)) {
return userAccounts.find(({ id, name }) => { return userAccounts.find(({ id, name }) => {
return ( return id === value || name?.toLowerCase() === value.toLowerCase();
id === value || name?.toLowerCase() === String(value).toLowerCase()
);
})?.id; })?.id;
} }
} }
@ -278,8 +287,8 @@ export class ImportActivitiesService {
for (const key of ImportActivitiesService.COMMENT_KEYS) { for (const key of ImportActivitiesService.COMMENT_KEYS) {
const value = item[key]; const value = item[key];
if (isNumber(value) || isString(value)) { if (isString(value)) {
return String(value); return value;
} }
} }
@ -339,9 +348,9 @@ export class ImportActivitiesService {
for (const key of ImportActivitiesService.DATE_KEYS) { for (const key of ImportActivitiesService.DATE_KEYS) {
const value = item[key]; const value = item[key];
if (isNumber(value) || isString(value)) { if (isString(value)) {
try { try {
const parsedDate = parseDateHelper(String(value)); const parsedDate = parseDateHelper(value);
if (parsedDate) { if (parsedDate) {
return parsedDate.toISOString(); return parsedDate.toISOString();
@ -420,8 +429,8 @@ export class ImportActivitiesService {
for (const key of ImportActivitiesService.SYMBOL_KEYS) { for (const key of ImportActivitiesService.SYMBOL_KEYS) {
const value = item[key]; const value = item[key];
if (isNumber(value) || isString(value)) { if (isString(value)) {
return String(value); return value;
} }
} }

Loading…
Cancel
Save