Browse Source

feat(client): replace any types

pull/7077/head
KenTandrian 4 weeks ago
parent
commit
7f1b551372
  1. 94
      apps/client/src/app/services/import-activities.service.ts

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

@ -10,7 +10,7 @@ import { Activity } from '@ghostfolio/common/interfaces';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { inject, 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 { EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators'; import { catchError } from 'rxjs/operators';
@ -49,7 +49,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
@ -208,8 +208,8 @@ export class ImportActivitiesService {
}; };
} }
private lowercaseKeys(aObject: any) { private lowercaseKeys(aObject: Record<string, unknown>) {
return Object.keys(aObject).reduce((acc, key) => { return Object.keys(aObject).reduce<Record<string, unknown>>((acc, key) => {
acc[key.toLowerCase()] = aObject[key]; acc[key.toLowerCase()] = aObject[key];
return acc; return acc;
}, {}); }, {});
@ -219,17 +219,18 @@ export class ImportActivitiesService {
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 val = item[key];
if (isString(val) || isNumber(val)) {
return userAccounts.find((account) => { return userAccounts.find((account) => {
return ( return (
account.id === item[key] || account.id === val ||
account.name?.toLowerCase() === item[key]?.toString().toLowerCase() account.name?.toLowerCase() === String(val).toLowerCase()
); );
})?.id; })?.id;
} }
@ -238,12 +239,13 @@ 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 val = item[key];
return item[key]; if (isString(val) || isNumber(val)) {
return String(val);
} }
} }
@ -255,15 +257,16 @@ 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 val = item[key];
return item[key]; if (isString(val)) {
return val;
} }
} }
@ -273,12 +276,13 @@ 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 val = item[key];
return DataSource[item[key].toUpperCase()]; if (isString(val)) {
return DataSource[val.toUpperCase() as keyof typeof DataSource];
} }
} }
@ -290,16 +294,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.DATE_KEYS) { for (const key of ImportActivitiesService.DATE_KEYS) {
if (item[key]) { const val = item[key];
if (isString(val) || isNumber(val)) {
try { try {
const parsedDate = parseDateHelper(item[key].toString()); const parsedDate = parseDateHelper(String(val));
if (parsedDate) { if (parsedDate) {
return parsedDate.toISOString(); return parsedDate.toISOString();
} }
@ -318,15 +323,16 @@ 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 val = item[key];
return Math.abs(item[key]); if (isNumber(val) && isFinite(val)) {
return Math.abs(val);
} }
} }
@ -341,15 +347,16 @@ 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 val = item[key];
return Math.abs(item[key]); if (isNumber(val) && isFinite(val)) {
return Math.abs(val);
} }
} }
@ -364,15 +371,16 @@ 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 val = item[key];
return item[key]; if (isString(val) || isNumber(val)) {
return String(val);
} }
} }
@ -387,15 +395,16 @@ 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 val = item[key];
switch (item[key].toLowerCase()) { if (isString(val)) {
switch (val.toLowerCase()) {
case 'buy': case 'buy':
return 'BUY'; return 'BUY';
case 'dividend': case 'dividend':
@ -425,15 +434,16 @@ 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 val = item[key];
return Math.abs(item[key]); if (isNumber(val) && isFinite(val)) {
return Math.abs(val);
} }
} }

Loading…
Cancel
Save