-
Notifications
You must be signed in to change notification settings - Fork 12
/
BankAccount.js
169 lines (153 loc) · 5.38 KB
/
BankAccount.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const groupBy = require('lodash/groupBy')
const get = require('lodash/get')
const keyBy = require('lodash/keyBy')
const merge = require('lodash/merge')
const Document = require('../Document')
const matching = require('./matching-accounts')
const { getSlugFromInstitutionLabel } = require('./slug-account')
const log = require('cozy-logger').namespace('BankAccount')
class BankAccount extends Document {
/**
* Adds _id of existing accounts to fetched accounts
*/
static reconciliate(fetchedAccounts, localAccounts) {
let matchings = matching.matchAccounts(fetchedAccounts, localAccounts)
matchings = BankAccount.addForcedReplaceMatchings(matchings, localAccounts)
return matchings.map(matching => {
log(
'info',
matching.match
? `${matching.account.label} matched with ${matching.match.label} via ${matching.method}`
: `${matching.account.label} did not match with an existing account`
)
if (matching.forcedReplace) {
log('info', `${matching.account.label} forced disabled state`)
}
const matchingMatchId = matching.match ? matching.match._id : undefined
const forcedReplaceId = matching.forcedReplace
? matching.account._id
: undefined
return {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
...matching.account,
relationships: merge(
{},
matching.match ? matching.match.relationships : null,
matching.account.relationships
),
_id: forcedReplaceId || matchingMatchId
}
})
}
/**
* Finds existing local bank accounts which are disabled and force their association to the new cozy account id
* which can be found in the other updated bank accounts
*
* @param {Array} previousMatchings
* @param {Array} localAccounts
* @returns {Array} matchings array with added disabled bank accounts to update with new cozy account id
*/
static addForcedReplaceMatchings(previousMatchings, localAccounts) {
const replacedCozyAccountIds =
BankAccount.findReplacedCozyAccountIdsMatchings(previousMatchings)
if (!Object.keys(replacedCozyAccountIds).length > 0) {
return previousMatchings
}
const matchings = [...previousMatchings]
const matchedAccountIds = keyBy(matchings, 'match._id')
for (const localAccount of localAccounts) {
const foundInMatchedAccounts = Boolean(
matchedAccountIds[localAccount._id]
)
const newAccountId =
replacedCozyAccountIds[
localAccount?.relationships?.connection?.data?._id
]
if (foundInMatchedAccounts || !newAccountId) {
continue
}
matchings.push({
forcedReplace: true,
account: {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
...localAccount,
metadata: merge({}, localAccount.metadata, {
disabledAt: localAccount.metadata?.updatedAt
}),
relationships: merge({}, localAccount.relationships, {
connection: {
data: {
_id: newAccountId
}
}
})
}
})
}
return matchings
}
/**
* Find any bank account which cozy account id has been changed
*
* @param {Array} matchings
* @returns {Object} mapping from old cozy account id to new cozy account id
*/
static findReplacedCozyAccountIdsMatchings(matchings) {
const result = {}
for (const matching of matchings) {
if (
matching.match &&
matching?.account?.relationships?.connection?.data?._id !==
matching?.match?.relationships?.connection?.data?._id
) {
result[matching?.match?.relationships?.connection?.data?._id] =
matching?.account?.relationships?.connection?.data?._id
}
}
return result
}
static findDuplicateAccountsWithNoOperations(accounts, operations) {
const opsByAccountId = groupBy(operations, op => op.account)
const duplicateAccountGroups = Object.entries(
groupBy(accounts, x => x.institutionLabel + ' > ' + x.label)
)
.map(([, duplicateGroup]) => duplicateGroup)
.filter(duplicateGroup => duplicateGroup.length > 1)
const res = []
for (const duplicateAccounts of duplicateAccountGroups) {
for (const account of duplicateAccounts) {
const accountOperations = opsByAccountId[account._id] || []
if (accountOperations.length === 0) {
res.push(account)
}
}
}
return res
}
static hasIncoherentCreatedByApp(account) {
const predictedSlug = getSlugFromInstitutionLabel(account.institutionLabel)
const createdByApp =
account.cozyMetadata && account.cozyMetadata.createdByApp
return Boolean(
predictedSlug && createdByApp && predictedSlug !== createdByApp
)
}
static getUpdatedAt(account) {
const vendorUpdatedAt = get(account, 'metadata.updatedAt')
if (vendorUpdatedAt) {
return vendorUpdatedAt
}
const cozyUpdatedAt = get(account, 'cozyMetadata.updatedAt')
if (cozyUpdatedAt) {
return cozyUpdatedAt
}
return null
}
}
BankAccount.normalizeAccountNumber = matching.normalizeAccountNumber
BankAccount.doctype = 'io.cozy.bank.accounts'
BankAccount.idAttributes = ['_id']
BankAccount.version = 1
BankAccount.checkedAttributes = null
BankAccount.vendorIdAttr = 'vendorId'
module.exports = BankAccount