Browse Source

Refactoring

pull/1635/head
Thomas 3 years ago
parent
commit
f3157414a9
  1. 3
      apps/api/src/app/export/export.service.ts
  2. 4
      apps/api/src/app/import/import.controller.ts
  3. 22
      apps/api/src/app/import/import.service.ts

3
apps/api/src/app/export/export.service.ts

@ -15,6 +15,9 @@ export class ExportService {
userId: string; userId: string;
}): Promise<Export> { }): Promise<Export> {
const accounts = await this.prismaService.account.findMany({ const accounts = await this.prismaService.account.findMany({
orderBy: {
name: 'asc'
},
select: { select: {
accountType: true, accountType: true,
balance: true, balance: true,

4
apps/api/src/app/import/import.controller.ts

@ -68,11 +68,11 @@ export class ImportController {
try { try {
const activities = await this.importService.import({ const activities = await this.importService.import({
accountsDto: importData.accounts || [],
activitiesDto: importData.activities,
isDryRun, isDryRun,
maxActivitiesToImport, maxActivitiesToImport,
userCurrency, userCurrency,
accountsDto: importData.accounts ?? [],
activitiesDto: importData.activities,
userId: this.request.user.id userId: this.request.user.id
}); });

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

@ -120,20 +120,22 @@ export class ImportService {
const existingAccounts = await this.accountService.accounts({ const existingAccounts = await this.accountService.accounts({
where: { where: {
id: { 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) { if (isDryRun && accountsDto?.length) {
for (let account of accountsDto) { for (const account of accountsDto) {
//Check if there is any existing account with the same ID // Check if there is any existing account with the same ID
const accountWithSameId = existingAccounts.find( const accountWithSameId = existingAccounts.find(
(existingAccount) => existingAccount.id === account.id (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) { if (!accountWithSameId || accountWithSameId.userId !== userId) {
let oldAccountId: string; let oldAccountId: string;
const platformId = account.platformId; const platformId = account.platformId;
@ -145,23 +147,23 @@ export class ImportService {
delete account.id; delete account.id;
} }
const newAccountObj = { const newAccountObject = {
...account, ...account,
User: { connect: { id: userId } } User: { connect: { id: userId } }
}; };
if (platformId) { if (platformId) {
Object.assign(newAccountObj, { Object.assign(newAccountObject, {
Platform: { connect: { id: platformId } } Platform: { connect: { id: platformId } }
}); });
} }
const newAccount = await this.accountService.createAccount( const newAccount = await this.accountService.createAccount(
newAccountObj, newAccountObject,
userId 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) { if (accountWithSameId && oldAccountId) {
accountIdMapping[oldAccountId] = newAccount.id; 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 (isDryRun) {
if (Object.keys(accountIdMapping).includes(activity.accountId)) { if (Object.keys(accountIdMapping).includes(activity.accountId)) {
activity.accountId = accountIdMapping[activity.accountId]; activity.accountId = accountIdMapping[activity.accountId];

Loading…
Cancel
Save