Browse Source

Task/improve type safety in import activities service (#7077)

* Improve type safety
pull/6999/head
Kenrick Tandrian 2 days ago
committed by GitHub
parent
commit
c2a07e8185
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 172
      apps/client/src/app/services/import-activities.service.ts

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

@ -8,12 +8,11 @@ import { parseDate as parseDateHelper } from '@ghostfolio/common/helper';
import { Activity } from '@ghostfolio/common/interfaces'; import { Activity } from '@ghostfolio/common/interfaces';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { inject, Injectable } from '@angular/core';
import { Account, DataSource, Type as ActivityType } from '@prisma/client'; import { Account, DataSource, Type as ActivityType } from '@prisma/client';
import { isFinite } from 'lodash'; import { isFinite, isNumber, isString } from 'lodash';
import { parse as csvToJson } from 'papaparse'; import { parse as csvToJson } from 'papaparse';
import { EMPTY } from 'rxjs'; import { firstValueFrom } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -35,7 +34,7 @@ export class ImportActivitiesService {
'value' 'value'
]; ];
public constructor(private http: HttpClient) {} private readonly http = inject(HttpClient);
public async importCsv({ public async importCsv({
fileContent, fileContent,
@ -49,7 +48,7 @@ export class ImportActivitiesService {
activities: Activity[]; activities: Activity[];
assetProfiles: CreateAssetProfileWithMarketDataDto[]; assetProfiles: CreateAssetProfileWithMarketDataDto[];
}> { }> {
const content = csvToJson(fileContent, { const content = csvToJson<Record<string, unknown>>(fileContent, {
dynamicTyping: true, dynamicTyping: true,
header: true, header: true,
skipEmptyLines: true skipEmptyLines: true
@ -83,22 +82,22 @@ export class ImportActivitiesService {
assetProfiles.push({ assetProfiles.push({
currency, currency,
symbol, symbol,
assetClass: null, assetClass: undefined,
assetSubClass: null, assetSubClass: undefined,
comment: null, comment: undefined,
countries: [], countries: [],
cusip: null, cusip: undefined,
dataSource: DataSource.MANUAL, dataSource: DataSource.MANUAL,
figi: null, figi: undefined,
figiComposite: null, figiComposite: undefined,
figiShareClass: null, figiShareClass: undefined,
holdings: [], holdings: [],
isActive: true, isActive: true,
isin: null, isin: undefined,
marketData: [], marketData: [],
name: symbol, name: symbol,
sectors: [], sectors: [],
url: null url: undefined
}); });
} }
} }
@ -126,7 +125,7 @@ export class ImportActivitiesService {
}): Promise<{ }): Promise<{
activities: Activity[]; activities: Activity[];
}> { }> {
return new Promise((resolve, reject) => { return firstValueFrom(
this.postImport( this.postImport(
{ {
accounts, accounts,
@ -136,18 +135,7 @@ export class ImportActivitiesService {
}, },
isDryRun isDryRun
) )
.pipe( );
catchError((error) => {
reject(error);
return EMPTY;
})
)
.subscribe({
next: (data) => {
resolve(data);
}
});
});
} }
public importSelectedActivities({ public importSelectedActivities({
@ -163,11 +151,9 @@ export class ImportActivitiesService {
}): Promise<{ }): Promise<{
activities: Activity[]; activities: Activity[];
}> { }> {
const importData: CreateOrderDto[] = []; const importData = activities.map((activity) =>
this.convertToCreateOrderDto(activity)
for (const activity of activities) { );
importData.push(this.convertToCreateOrderDto(activity));
}
return this.importJson({ return this.importJson({
accounts, accounts,
@ -191,14 +177,14 @@ export class ImportActivitiesService {
updateAccountBalance updateAccountBalance
}: Activity): CreateOrderDto { }: Activity): CreateOrderDto {
return { return {
accountId,
comment,
fee, fee,
quantity, quantity,
type, type,
unitPrice, unitPrice,
updateAccountBalance, updateAccountBalance,
currency: currency ?? SymbolProfile.currency, accountId: accountId ?? undefined,
comment: comment ?? undefined,
currency: currency ?? SymbolProfile.currency ?? '',
dataSource: SymbolProfile.dataSource, dataSource: SymbolProfile.dataSource,
date: date.toString(), date: date.toString(),
symbol: SymbolProfile.symbol, symbol: SymbolProfile.symbol,
@ -208,28 +194,32 @@ export class ImportActivitiesService {
}; };
} }
private lowercaseKeys(aObject: any) { private lowercaseKeys(
return Object.keys(aObject).reduce((acc, key) => { aObject: Record<string, unknown>
acc[key.toLowerCase()] = aObject[key]; ): Record<string, unknown> {
return acc; return Object.fromEntries(
}, {}); Object.entries(aObject).map(([key, val]) => {
return [key.toLowerCase(), val];
})
);
} }
private parseAccount({ private parseAccount({
item, item,
userAccounts userAccounts
}: { }: {
item: any; item: Record<string, unknown>;
userAccounts: Account[]; userAccounts: Account[];
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.ACCOUNT_KEYS) { for (const key of ImportActivitiesService.ACCOUNT_KEYS) {
if (item[key]) { const value = item[key];
return userAccounts.find((account) => {
if (isNumber(value) || isString(value)) {
return userAccounts.find(({ id, name }) => {
return ( return (
account.id === item[key] || id === value || name?.toLowerCase() === String(value).toLowerCase()
account.name.toLowerCase() === item[key].toLowerCase()
); );
})?.id; })?.id;
} }
@ -238,12 +228,14 @@ export class ImportActivitiesService {
return undefined; return undefined;
} }
private parseComment({ item }: { item: any }) { private parseComment({ item }: { item: Record<string, unknown> }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.COMMENT_KEYS) { for (const key of ImportActivitiesService.COMMENT_KEYS) {
if (item[key]) { const value = item[key];
return item[key];
if (isNumber(value) || isString(value)) {
return String(value);
} }
} }
@ -255,15 +247,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.CURRENCY_KEYS) { for (const key of ImportActivitiesService.CURRENCY_KEYS) {
if (item[key]) { const value = item[key];
return item[key];
if (isString(value)) {
return value;
} }
} }
@ -273,12 +267,14 @@ export class ImportActivitiesService {
}; };
} }
private parseDataSource({ item }: { item: any }) { private parseDataSource({ item }: { item: Record<string, unknown> }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.DATA_SOURCE_KEYS) { for (const key of ImportActivitiesService.DATA_SOURCE_KEYS) {
if (item[key]) { const value = item[key];
return DataSource[item[key].toUpperCase()];
if (isString(value)) {
return DataSource[value.toUpperCase() as keyof typeof DataSource];
} }
} }
@ -290,16 +286,22 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.DATE_KEYS) { for (const key of ImportActivitiesService.DATE_KEYS) {
if (item[key]) { const value = item[key];
if (isNumber(value) || isString(value)) {
try { try {
return parseDateHelper(item[key].toString()).toISOString(); const parsedDate = parseDateHelper(String(value));
if (parsedDate) {
return parsedDate.toISOString();
}
} catch {} } catch {}
} }
} }
@ -315,15 +317,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.FEE_KEYS) { for (const key of ImportActivitiesService.FEE_KEYS) {
if (isFinite(item[key])) { const value = item[key];
return Math.abs(item[key]);
if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }
@ -338,15 +342,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.QUANTITY_KEYS) { for (const key of ImportActivitiesService.QUANTITY_KEYS) {
if (isFinite(item[key])) { const value = item[key];
return Math.abs(item[key]);
if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }
@ -361,15 +367,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.SYMBOL_KEYS) { for (const key of ImportActivitiesService.SYMBOL_KEYS) {
if (item[key]) { const value = item[key];
return item[key];
if (isNumber(value) || isString(value)) {
return String(value);
} }
} }
@ -384,15 +392,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}): ActivityType { }): ActivityType {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.TYPE_KEYS) { for (const key of ImportActivitiesService.TYPE_KEYS) {
if (item[key]) { const value = item[key];
switch (item[key].toLowerCase()) {
if (isString(value)) {
switch (value.toLowerCase()) {
case 'buy': case 'buy':
return 'BUY'; return 'BUY';
case 'dividend': case 'dividend':
@ -422,15 +432,17 @@ export class ImportActivitiesService {
index, index,
item item
}: { }: {
content: any[]; content: Record<string, unknown>[];
index: number; index: number;
item: any; item: Record<string, unknown>;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.UNIT_PRICE_KEYS) { for (const key of ImportActivitiesService.UNIT_PRICE_KEYS) {
if (isFinite(item[key])) { const value = item[key];
return Math.abs(item[key]);
if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }

Loading…
Cancel
Save