From f3157414a9151543bc7eb9b7f55923a89e4c71f6 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 5 Feb 2023 10:07:43 +0100 Subject: [PATCH] Refactoring --- apps/api/src/app/export/export.service.ts | 3 +++ apps/api/src/app/import/import.controller.ts | 4 ++-- apps/api/src/app/import/import.service.ts | 22 +++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/apps/api/src/app/export/export.service.ts b/apps/api/src/app/export/export.service.ts index 219b022cb..c57f1df4e 100644 --- a/apps/api/src/app/export/export.service.ts +++ b/apps/api/src/app/export/export.service.ts @@ -15,6 +15,9 @@ export class ExportService { userId: string; }): Promise { const accounts = await this.prismaService.account.findMany({ + orderBy: { + name: 'asc' + }, select: { accountType: true, balance: true, diff --git a/apps/api/src/app/import/import.controller.ts b/apps/api/src/app/import/import.controller.ts index 0de1c0b54..06d9ad93d 100644 --- a/apps/api/src/app/import/import.controller.ts +++ b/apps/api/src/app/import/import.controller.ts @@ -68,11 +68,11 @@ export class ImportController { try { const activities = await this.importService.import({ - accountsDto: importData.accounts || [], - activitiesDto: importData.activities, isDryRun, maxActivitiesToImport, userCurrency, + accountsDto: importData.accounts ?? [], + activitiesDto: importData.activities, userId: this.request.user.id }); diff --git a/apps/api/src/app/import/import.service.ts b/apps/api/src/app/import/import.service.ts index 5c3aa9e59..b9ca5eccd 100644 --- a/apps/api/src/app/import/import.service.ts +++ b/apps/api/src/app/import/import.service.ts @@ -120,20 +120,22 @@ export class ImportService { const existingAccounts = await this.accountService.accounts({ where: { id: { - in: accountsDto.map((account) => account.id) + in: accountsDto.map(({ id }) => { + return id; + }) } } }); - //Create new accounts during dryRun so that new account IDs don't get invalidated + // Create new accounts during dryRun so that new account IDs don't get invalidated if (isDryRun && accountsDto?.length) { - for (let account of accountsDto) { - //Check if there is any existing account with the same ID + for (const account of accountsDto) { + // Check if there is any existing account with the same ID const accountWithSameId = existingAccounts.find( (existingAccount) => existingAccount.id === account.id ); - //If there is no account or if the account belongs to a different user then create a new account + // 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; @@ -145,23 +147,23 @@ export class ImportService { delete account.id; } - const newAccountObj = { + const newAccountObject = { ...account, User: { connect: { id: userId } } }; if (platformId) { - Object.assign(newAccountObj, { + Object.assign(newAccountObject, { Platform: { connect: { id: platformId } } }); } const newAccount = await this.accountService.createAccount( - newAccountObj, + newAccountObject, userId ); - //Store the new to old account ID mappings for updating activities + // Store the new to old account ID mappings for updating activities if (accountWithSameId && oldAccountId) { accountIdMapping[oldAccountId] = newAccount.id; } @@ -178,7 +180,7 @@ export class ImportService { } } - //If a new account is created, then update the accountId in all activities + // If a new account is created, then update the accountId in all activities if (isDryRun) { if (Object.keys(accountIdMapping).includes(activity.accountId)) { activity.accountId = accountIdMapping[activity.accountId];