Browse Source

Improve check for duplicates

pull/7541/head
Thomas Kaul 4 weeks ago
parent
commit
1515b84530
  1. 33
      apps/api/src/app/import/import.service.ts

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

@ -326,7 +326,8 @@ export class ImportService {
}
if (!isDryRun && accountsWithBalancesDto?.length) {
const [existingAccounts, existingPlatforms] = await Promise.all([
const [existingAccounts, existingAccountsOfUser, existingPlatforms] =
await Promise.all([
this.accountService.accounts({
where: {
id: {
@ -336,6 +337,9 @@ export class ImportService {
}
}
}),
this.accountService.accounts({
where: { userId: user.id }
}),
this.platformService.getPlatforms()
]);
@ -351,8 +355,28 @@ export class ImportService {
return existingAccount.id === accountWithBalances.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 reuse an existing account of the user with the same name
// or create a new account
if (!accountWithSameId || accountWithSameId.userId !== user.id) {
// Check if the user has an account with the same name
const accountWithSameNameOfUser = existingAccountsOfUser.find(
({ name }) => {
return name === accountWithBalances.name;
}
);
if (accountWithSameNameOfUser) {
// Reuse the account of the user instead of creating a duplicate
if (
accountWithBalances.id &&
accountWithBalances.id !== accountWithSameNameOfUser.id
) {
// Store the new to old account ID mappings for updating activities
accountIdMapping[accountWithBalances.id] =
accountWithSameNameOfUser.id;
}
} else {
const account = omit(accountWithBalances, [
'balances',
'isExcluded',
@ -420,6 +444,7 @@ export class ImportService {
}
}
}
}
if (assetProfilesWithMarketDataDto?.length) {
const customAssetProfileNames = assetProfilesWithMarketDataDto
@ -823,10 +848,10 @@ export class ImportService {
unitPrice
}) => {
const date = parseISO(dateString);
const isDuplicate = existingActivities.some((activity) => {
return (
activity.accountId === accountId &&
activity.comment === comment &&
(activity.comment ?? null) === (comment ?? null) &&
(activity.currency === currency ||
activity.assetProfile.currency === currency) &&
activity.assetProfile.dataSource === dataSource &&

Loading…
Cancel
Save