Browse Source

Support CSV file format of Yahoo Finance in activities import #2288

Added default warnings for no type or fee provided
Future: localize messages
pull/4056/head
Brandon Wortman 9 months ago
parent
commit
0e07415bf8
  1. 24
      apps/client/src/app/services/import-activities.service.ts

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

@ -10,6 +10,7 @@ import { isFinite } from 'lodash';
import { parse as csvToJson } from 'papaparse';
import { EMPTY, map, firstValueFrom } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { NotificationService } from '@ghostfolio/client/core/notification/notification.service';
@Injectable({
providedIn: 'root'
@ -32,7 +33,12 @@ export class ImportActivitiesService {
'value'
];
public constructor(private http: HttpClient) {}
private warnings: string[] = [];
public constructor(
private http: HttpClient,
private notificationService: NotificationService
) {}
public async importCsv({
fileContent,
@ -45,6 +51,8 @@ export class ImportActivitiesService {
}): Promise<{
activities: Activity[];
}> {
this.warnings = [];
const content = csvToJson(fileContent, {
dynamicTyping: true,
header: true,
@ -68,6 +76,14 @@ export class ImportActivitiesService {
});
}
// Process any possible warnings during import
if (this.warnings.length > 0) {
this.notificationService.alert({
title: 'Import Warnings',
message: this.warnings.join('<br/>')
});
}
return await this.importJson({ activities, isDryRun });
}
@ -276,7 +292,6 @@ export class ImportActivitiesService {
}
private parseFee({
// content,
index,
item
}: {
@ -292,7 +307,7 @@ export class ImportActivitiesService {
}
}
console.warn(`activities.${index}.fee was not provided, defaulting to 0`);
this.warnings.push(`Activity ${index + 1}: Fee not provided, defaulting to 0`);
return 0;
}
@ -343,7 +358,6 @@ export class ImportActivitiesService {
}
private parseType({
//content,
index,
item
}: {
@ -376,7 +390,7 @@ export class ImportActivitiesService {
}
}
console.warn(`activities.${index}.type was not provided, defaulting to BUY`);
this.warnings.push(`Activity ${index + 1}: Type not provided, defaulting to BUY`);
return 'BUY';
}

Loading…
Cancel
Save