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. 68
      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({ public async getCashDetails({
currency, currency,
filters = [], filters = [],

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

@ -115,68 +115,48 @@ export class ImportService {
userCurrency: string; userCurrency: string;
userId: string; userId: string;
}): Promise<Activity[]> { }): Promise<Activity[]> {
const accountMappings = {}; const accountIdMapping = {};
//Validate accounts
if (accountsDto?.length) { if (!isDryRun && accountsDto?.length) {
for (let account of accountsDto) { for (let account of accountsDto) {
const existingAccounts = await this.accountService.accounts({ //Check if there is any existing account with the same id
where: { id: account.id } const accountWithSameId = await this.accountService.getAccountById(
}); account.id
);
const oldAccountId = account.id; //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; const platformId = account.platformId;
delete account.id;
delete account.platformId; delete account.platformId;
delete account.isDefault; delete account.isDefault;
//If account id does not exist, then create a new one if (accountWithSameId) {
if (existingAccounts.length === 0) { oldAccountId = account.id;
const newAccountConfig = { delete account.id;
...account,
User: { connect: { id: userId } }
};
if (platformId) {
Object.assign(newAccountConfig, {
Platform: { connect: { id: platformId } }
});
}
const newAccount = await this.accountService.createAccount(
newAccountConfig,
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 newAccountObj = {
const newAccountConfig = {
...account, ...account,
User: { connect: { id: userId } } User: { connect: { id: userId } }
}; };
if (platformId) { if (platformId) {
Object.assign(newAccountConfig, { Object.assign(newAccountObj, {
Platform: { connect: { id: platformId } } Platform: { connect: { id: platformId } }
}); });
} }
const newAccount = await this.accountService.createAccount( const newAccount = await this.accountService.createAccount(
newAccountConfig, newAccountObj,
userId userId
); );
accountMappings[oldAccountId] = newAccount.id; if (accountWithSameId && oldAccountId) {
accountIdMapping[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 (!isDryRun) {
if (Object.keys(accountMappings).includes(activity.accountId)) { //If a new account is created, then update the accountId in all activities
activity.accountId = accountMappings[activity.accountId]; 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 { try {
this.accounts = content.accounts;
const data = await this.importActivitiesService.importJson({ const data = await this.importActivitiesService.importJson({
activities: content.activities, activities: content.activities,
accounts: content.accounts,
isDryRun: true isDryRun: true
}); });
this.activities = data.activities; this.activities = data.activities;
this.accounts = data.accounts;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
this.handleImportError({ error, activities: content.activities }); this.handleImportError({ error, activities: content.activities });

Loading…
Cancel
Save