From fe3543cdcf8ce97ccdd9fdd64fdc6b0956501cbd Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Tue, 1 Feb 2022 18:29:36 +0100 Subject: [PATCH] Import activities by account name Co-Authored-By: Ronald Konjer --- .../transactions-page.component.ts | 3 +- .../services/import-transactions.service.ts | 33 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/apps/client/src/app/pages/portfolio/transactions/transactions-page.component.ts b/apps/client/src/app/pages/portfolio/transactions/transactions-page.component.ts index eae444e94..8172ed080 100644 --- a/apps/client/src/app/pages/portfolio/transactions/transactions-page.component.ts +++ b/apps/client/src/app/pages/portfolio/transactions/transactions-page.component.ts @@ -204,7 +204,8 @@ export class TransactionsPageComponent implements OnDestroy, OnInit { } else if (file.name.endsWith('.csv')) { try { await this.importTransactionsService.importCsv({ - fileContent + fileContent, + userAccounts: this.user.accounts }); this.handleImportSuccess(); diff --git a/apps/client/src/app/services/import-transactions.service.ts b/apps/client/src/app/services/import-transactions.service.ts index c974e0a60..b39d4ac65 100644 --- a/apps/client/src/app/services/import-transactions.service.ts +++ b/apps/client/src/app/services/import-transactions.service.ts @@ -1,7 +1,7 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto'; -import { DataSource, Type } from '@prisma/client'; +import { Account, DataSource, Type } from '@prisma/client'; import { parse } from 'date-fns'; import { isNumber } from 'lodash'; import { parse as csvToJson } from 'papaparse'; @@ -12,7 +12,7 @@ import { catchError } from 'rxjs/operators'; providedIn: 'root' }) export class ImportTransactionsService { - private static ACCOUNT_ID_KEYS = ['account', 'accountid']; + private static ACCOUNT_KEYS = ['account', 'accountid']; private static CURRENCY_KEYS = ['ccy', 'currency']; private static DATA_SOURCE_KEYS = ['datasource']; private static DATE_KEYS = ['date']; @@ -24,7 +24,13 @@ export class ImportTransactionsService { public constructor(private http: HttpClient) {} - public async importCsv({ fileContent }: { fileContent: string }) { + public async importCsv({ + fileContent, + userAccounts + }: { + fileContent: string; + userAccounts: Account[]; + }) { const content = csvToJson(fileContent, { dynamicTyping: true, header: true, @@ -34,7 +40,7 @@ export class ImportTransactionsService { const orders: CreateOrderDto[] = []; for (const [index, item] of content.entries()) { orders.push({ - accountId: this.parseAccountId({ item }), + accountId: this.parseAccount({ item, userAccounts }), currency: this.parseCurrency({ content, index, item }), dataSource: this.parseDataSource({ item }), date: this.parseDate({ content, index, item }), @@ -75,12 +81,23 @@ export class ImportTransactionsService { }, {}); } - private parseAccountId({ item }: { item: any }) { + private parseAccount({ + item, + userAccounts + }: { + item: any; + userAccounts: Account[]; + }) { item = this.lowercaseKeys(item); - for (const key of ImportTransactionsService.ACCOUNT_ID_KEYS) { + for (const key of ImportTransactionsService.ACCOUNT_KEYS) { if (item[key]) { - return item[key]; + return userAccounts.find((account) => { + return ( + account.id === item[key] || + account.name.toLowerCase() === item[key].toLowerCase() + ); + })?.id; } } @@ -112,7 +129,7 @@ export class ImportTransactionsService { for (const key of ImportTransactionsService.DATA_SOURCE_KEYS) { if (item[key]) { - return DataSource[item[key]]; + return DataSource[item[key].toUpperCase()]; } }