Skip to content

Commit

Permalink
Merge pull request #81 from doubleface/fix/debit_no_found
Browse files Browse the repository at this point in the history
fix: linkBankOperation : match credit operation even if no debit operation found
  • Loading branch information
kosssi authored Jan 23, 2018
2 parents e53850c + 70b15b2 commit aa566fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
11 changes: 8 additions & 3 deletions libs/linkBankOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,18 @@ class Linker {
const linkBillToCreditOperation = debitOperation => {
return findCreditOperation(this.cozyClient, bill, options)
.then(creditOperation => {
if (creditOperation && debitOperation) {
const creditPromise = Promise.resolve()
if (creditOperation) {
res.creditOperation = creditOperation
creditPromise.then(() => this.addBillToOperation(bill, creditOperation))
}
const debitPromise = Promise.resolve()
if (creditOperation && debitOperation) {
log('debug', bill, 'Matching bill')
log('debug', creditOperation, 'Matching credit creditOperation')
return this.addReimbursementToOperation(bill, debitOperation, creditOperation)
.then(() => this.addBillToOperation(bill, creditOperation))
debitPromise.then(() => this.addReimbursementToOperation(bill, debitOperation, creditOperation))
}
return Promise.all([creditOperation, debitOperation])
})
}

Expand Down
26 changes: 24 additions & 2 deletions libs/linkBankOperations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe('linker', () => {
// reset operations to operationsInit values
operations = operationsInit.map(op => ({ ...op }))
cozyClient.data.query.mockReturnValue(Promise.resolve(operations))
cozyClient.data.updateAttributes.mockImplementation(updateOperation)
})

const defaultOptions = {
Expand All @@ -141,7 +142,6 @@ describe('linker', () => {
}

test('health bills', () => {
cozyClient.data.updateAttributes.mockImplementation(updateOperation)
const healthBills = [
{
_id: 'b1',
Expand Down Expand Up @@ -169,8 +169,30 @@ describe('linker', () => {
})
})

test('health bills with not debit operation found should associate credit operation', () => {
const healthBills = [
{
_id: 'b1',
amount: 5,
originalAmount: 999,
type: 'health_costs',
originalDate: new Date(2017, 11, 13),
date: new Date(2017, 11, 15),
isRefund: true,
vendor: 'Ameli'
}
]
const options = { ...defaultOptions, identifiers: ['CPAM'] }
return linker.linkBillsToOperations(healthBills, options)
.then(result => {
expect(result).toEqual({
b1: { creditOperation: operations[1], debitOperation: undefined }
})
expect(operations[1]).toMatchObject({bills: ['io.cozy.bills:b1']})
})
})

test('not health bills', () => {
cozyClient.data.updateAttributes.mockReturnValue(Promise.resolve())
const noHealthBills = [
{
_id: 'b2',
Expand Down

0 comments on commit aa566fa

Please sign in to comment.