Browse Source

Refactoring

pull/7077/head
Thomas Kaul 4 weeks ago
parent
commit
2cfa2fec95
  1. 78
      apps/client/src/app/services/import-activities.service.ts

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

@ -198,7 +198,9 @@ export class ImportActivitiesService {
aObject: Record<string, unknown> aObject: Record<string, unknown>
): Record<string, unknown> { ): Record<string, unknown> {
return Object.fromEntries( return Object.fromEntries(
Object.entries(aObject).map(([key, val]) => [key.toLowerCase(), val]) Object.entries(aObject).map(([key, val]) => {
return [key.toLowerCase(), val];
})
); );
} }
@ -212,12 +214,12 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.ACCOUNT_KEYS) { for (const key of ImportActivitiesService.ACCOUNT_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val) || isNumber(val)) {
return userAccounts.find((account) => { if (isNumber(value) || isString(value)) {
return userAccounts.find(({ id, name }) => {
return ( return (
account.id === val || id === value || name?.toLowerCase() === String(value).toLowerCase()
account.name?.toLowerCase() === String(val).toLowerCase()
); );
})?.id; })?.id;
} }
@ -230,9 +232,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.COMMENT_KEYS) { for (const key of ImportActivitiesService.COMMENT_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val) || isNumber(val)) {
return String(val); if (isNumber(value) || isString(value)) {
return String(value);
} }
} }
@ -251,9 +254,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.CURRENCY_KEYS) { for (const key of ImportActivitiesService.CURRENCY_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val)) {
return val; if (isString(value)) {
return value;
} }
} }
@ -267,9 +271,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.DATA_SOURCE_KEYS) { for (const key of ImportActivitiesService.DATA_SOURCE_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val)) {
return DataSource[val.toUpperCase() as keyof typeof DataSource]; if (isString(value)) {
return DataSource[value.toUpperCase() as keyof typeof DataSource];
} }
} }
@ -288,10 +293,12 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.DATE_KEYS) { for (const key of ImportActivitiesService.DATE_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val) || isNumber(val)) {
if (isNumber(value) || isString(value)) {
try { try {
const parsedDate = parseDateHelper(String(val)); const parsedDate = parseDateHelper(String(value));
if (parsedDate) { if (parsedDate) {
return parsedDate.toISOString(); return parsedDate.toISOString();
} }
@ -317,9 +324,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.FEE_KEYS) { for (const key of ImportActivitiesService.FEE_KEYS) {
const val = item[key]; const value = item[key];
if (isNumber(val) && isFinite(val)) {
return Math.abs(val); if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }
@ -341,9 +349,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.QUANTITY_KEYS) { for (const key of ImportActivitiesService.QUANTITY_KEYS) {
const val = item[key]; const value = item[key];
if (isNumber(val) && isFinite(val)) {
return Math.abs(val); if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }
@ -365,9 +374,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.SYMBOL_KEYS) { for (const key of ImportActivitiesService.SYMBOL_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val) || isNumber(val)) {
return String(val); if (isNumber(value) || isString(value)) {
return String(value);
} }
} }
@ -389,9 +399,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.TYPE_KEYS) { for (const key of ImportActivitiesService.TYPE_KEYS) {
const val = item[key]; const value = item[key];
if (isString(val)) {
switch (val.toLowerCase()) { if (isString(value)) {
switch (value.toLowerCase()) {
case 'buy': case 'buy':
return 'BUY'; return 'BUY';
case 'dividend': case 'dividend':
@ -428,9 +439,10 @@ export class ImportActivitiesService {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
for (const key of ImportActivitiesService.UNIT_PRICE_KEYS) { for (const key of ImportActivitiesService.UNIT_PRICE_KEYS) {
const val = item[key]; const value = item[key];
if (isNumber(val) && isFinite(val)) {
return Math.abs(val); if (isNumber(value) && isFinite(value)) {
return Math.abs(value);
} }
} }

Loading…
Cancel
Save