Browse Source

Refactoring and bug fixes

pull/1635/head
yksolanki9 3 years ago
committed by Thomas
parent
commit
e104ae17f8
  1. 7
      apps/api/src/app/account/account.service.ts
  2. 76
      apps/api/src/app/import/import.service.ts
  3. 3
      apps/client/src/app/pages/portfolio/activities/import-activities-dialog/import-activities-dialog.component.ts

7
apps/api/src/app/account/account.service.ts

@ -104,6 +104,13 @@ export class AccountService {
});
}
public async getAccountById(accountId: string) {
const accounts = await this.accounts({
where: { id: accountId }
});
return accounts.length ? accounts[0] : null;
}
public async getCashDetails({
currency,
filters = [],

76
apps/api/src/app/import/import.service.ts

@ -115,68 +115,48 @@ export class ImportService {
userCurrency: string;
userId: string;
}): Promise<Activity[]> {
const accountMappings = {};
//Validate accounts
if (accountsDto?.length) {
const accountIdMapping = {};
if (!isDryRun && accountsDto?.length) {
for (let account of accountsDto) {
const existingAccounts = await this.accountService.accounts({
where: { id: account.id }
});
//Check if there is any existing account with the same id
const accountWithSameId = await this.accountService.getAccountById(
account.id
);
const oldAccountId = account.id;
const platformId = account.platformId;
//If there is no account or if the account belongs to a different user then create a new account
if (!accountWithSameId || accountWithSameId.userId !== userId) {
let oldAccountId: string;
const platformId = account.platformId;
delete account.id;
delete account.platformId;
delete account.isDefault;
delete account.platformId;
delete account.isDefault;
if (accountWithSameId) {
oldAccountId = account.id;
delete account.id;
}
//If account id does not exist, then create a new one
if (existingAccounts.length === 0) {
const newAccountConfig = {
const newAccountObj = {
...account,
User: { connect: { id: userId } }
};
if (platformId) {
Object.assign(newAccountConfig, {
Object.assign(newAccountObj, {
Platform: { connect: { id: platformId } }
});
}
const newAccount = await this.accountService.createAccount(
newAccountConfig,
newAccountObj,
userId
);
accountMappings[oldAccountId] = newAccount.id;
continue;
}
//If account id is used, then check if it belongs to the same user
//Yes -> Merge the accounts and don't create a new one
if (existingAccounts[0].userId === userId) {
continue;
}
//No -> Replace the account id with a new account id as well as update all the activities when looping
const newAccountConfig = {
...account,
User: { connect: { id: userId } }
};
if (platformId) {
Object.assign(newAccountConfig, {
Platform: { connect: { id: platformId } }
});
if (accountWithSameId && oldAccountId) {
accountIdMapping[oldAccountId] = newAccount.id;
}
}
const newAccount = await this.accountService.createAccount(
newAccountConfig,
userId
);
accountMappings[oldAccountId] = newAccount.id;
}
}
@ -189,9 +169,11 @@ export class ImportService {
}
}
//If we updated the account id, then update the account id in the activity as well
if (Object.keys(accountMappings).includes(activity.accountId)) {
activity.accountId = accountMappings[activity.accountId];
if (!isDryRun) {
//If a new account is created, then update the accountId in all activities
if (Object.keys(accountIdMapping).includes(activity.accountId)) {
activity.accountId = accountIdMapping[activity.accountId];
}
}
}

3
apps/client/src/app/pages/portfolio/activities/import-activities-dialog/import-activities-dialog.component.ts

@ -183,13 +183,12 @@ export class ImportActivitiesDialog implements OnDestroy {
}
try {
this.accounts = content.accounts;
const data = await this.importActivitiesService.importJson({
activities: content.activities,
accounts: content.accounts,
isDryRun: true
});
this.activities = data.activities;
this.accounts = data.accounts;
} catch (error) {
console.error(error);
this.handleImportError({ error, activities: content.activities });

Loading…
Cancel
Save