/
BalanceHistory.js
44 lines (38 loc) · 1.06 KB
/
BalanceHistory.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
const BankAccount = require('./BankAccount')
const Document = require('../Document')
class BalanceHistory extends Document {
static async getByYearAndAccount(year, accountId) {
const index = await Document.getIndex(this.doctype, this.idAttributes)
const options = {
selector: { year, 'relationships.account.data._id': accountId },
limit: 1
}
const [balance] = await Document.query(index, options)
if (balance) {
return balance
}
return this.getEmptyDocument(year, accountId)
}
static getEmptyDocument(year, accountId) {
return {
year,
balances: {},
metadata: {
version: this.version
},
relationships: {
account: {
data: {
_id: accountId,
_type: BankAccount.doctype
}
}
}
}
}
}
BalanceHistory.doctype = 'io.cozy.bank.balancehistories'
BalanceHistory.idAttributes = ['year', 'relationships.account.data._id']
BalanceHistory.version = 1
BalanceHistory.checkAttributes = ['balances']
module.exports = BalanceHistory