Skip to content

Commit

Permalink
fix: Set connection id of current bank's accounts
Browse files Browse the repository at this point in the history
  When (re-)connecting a bank, we try to match existing accounts with
  newly fetched ones.
  When we find matches, we update the existing accounts with the fresh
  data and set the connection id with the new one.
  We also update the connection id of other bank accounts that were
  sharing the same old connection id with the matching bank account.

  e.g. :

    Say we have
    - bank account #1 with connection id A
    - bank account #2 with connection id A
    - bank account #3 with connection id B

    And we fetch
    - bank account #1 connection id C

    Then we'll make these updates
    - bank account #1 gets connection id C
    - bank account #2 gets connection id C

  Now, if an existing bank account matches with a fetched one but did
  not have a connection id anymore, we'll use the new connection id as
  well.

  However, the way we were doing so resulted in the update of all bank
  accounts without connection ids whenever one of them would match a
  newly fetched one.
  This means that bank accounts from different banks could get
  associated with the same connection.

  To avoid this, we now prevent connection id updates for bank accounts
  missing one unless they have a match in the newly fetched bank
  accounts.
  • Loading branch information
taratatach committed Mar 31, 2023
1 parent d2faf21 commit 1964421
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
15 changes: 14 additions & 1 deletion packages/cozy-doctypes/src/banking/BankAccount.js
Expand Up @@ -64,13 +64,26 @@ class BankAccount extends Document {
const matchings = [...previousMatchings]
const matchedAccountIds = keyBy(matchings, 'match._id')
for (const localAccount of localAccounts) {
if (connectionId(localAccount) == null) {
// XXX: don't try to update the connectionId of local accounts which
// don't have any as they're probably not from the same bank.
// If they are, their connectionId should be updated via a match with a
// fetched account.
continue
}

const foundInMatchedAccounts = Boolean(
matchedAccountIds[localAccount._id]
)
if (foundInMatchedAccounts) {
continue
}

const newAccountId = replacedCozyAccountIds[connectionId(localAccount)]
if (foundInMatchedAccounts || !newAccountId) {
if (newAccountId == null) {
continue
}

matchings.push({
forcedReplace: true,
account: {
Expand Down
49 changes: 48 additions & 1 deletion packages/cozy-doctypes/src/banking/BankAccount.spec.js
@@ -1,7 +1,7 @@
const BankAccount = require('./BankAccount')

describe('account reconciliation', () => {
it('should update relationship of disabled accounts associated to the same relationship as updated anabled accounts even with accounts without connection relationship', () => {
it('should update connection of disabled accounts without connection with the one of matching fetched accounts', () => {
const newAccounts = [
{
number: '1',
Expand All @@ -17,6 +17,21 @@ describe('account reconciliation', () => {
metadata: {
updatedAt: '2020-11-30'
}
},
{
number: '10',
balance: 33,
relationships: {
connection: {
data: {
_id: 'cozyaccountnew',
_type: 'io.cozy.accounts'
}
}
},
metadata: {
updatedAt: '2020-11-30'
}
}
]
const currentAccounts = [
Expand Down Expand Up @@ -66,6 +81,15 @@ describe('account reconciliation', () => {
metadata: {
updatedAt: '2012-11-30'
}
},
{
_id: 'otheraccountnorelationship',
number: '20',
balance: 130,
relationships: {},
metadata: {
updatedAt: '2012-11-30'
}
}
]
const matchedAccounts = BankAccount.reconciliate(
Expand All @@ -91,6 +115,28 @@ describe('account reconciliation', () => {
updatedAt: '2020-11-30'
}
},
{
_id: 'oldaccountnorelationship',
number: '10',
rawNumber: '10',
balance: 33,
relationships: {
connection: {
data: {
_id: 'cozyaccountnew',
_type: 'io.cozy.accounts'
}
},
other: {
data: {
some: 'data'
}
}
},
metadata: {
updatedAt: '2020-11-30'
}
},
{
_id: 'a2',
number: '2',
Expand All @@ -110,6 +156,7 @@ describe('account reconciliation', () => {
}
])
})

it('should correctly match linxo accounts to cozy accounts through number', () => {
const newAccounts = [
{
Expand Down

0 comments on commit 1964421

Please sign in to comment.