Skip to content

Commit

Permalink
feat(Transactions): Guess budget for new, synced transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Apr 17, 2021
1 parent 2a5f8b6 commit 1bb5ee2
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion logic/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,8 @@ class TransactionLogic extends BaseLogic {
await Promise.all(jobs);

// check units and add unit with difference when necessary
if (newTransaction.id) {
const isNew = !newTransaction.id;
if (!isNew) {
const units = await newTransaction.getUnits();
if (units.length === 1 && units[0].amount !== newTransaction.amount) {
units[0].amount = newTransaction.amount;
Expand Down Expand Up @@ -1483,6 +1484,23 @@ class TransactionLogic extends BaseLogic {
throw err;
}

if(!isNew) {
return newTransaction;
}

const guesses = await this.guessBudget(newTransaction);
if(!guesses.length || guesses[0].probability < 1) {
return newTransaction;
}

const unit = await DatabaseHelper.get('unit').create({
transactionId: newTransaction.id,
budgetId: guesses[0].budgetId,
amount: newTransaction.amount,
type: 'BUDGET'
});

await newTransaction.addUnit(unit);
return newTransaction;
}

Expand Down

0 comments on commit 1bb5ee2

Please sign in to comment.