23 lines
579 B
JavaScript
23 lines
579 B
JavaScript
|
|
const db = require('../../../../models/migration')
|
|
const Balance = db.Balance
|
|
|
|
async function updateBalance(user_id, type, amount, t) {
|
|
if (!['income', 'expanse', 'debt', 'receivable'].includes(type)) return;
|
|
|
|
const [balance, created] = await Balance.findOrCreate({
|
|
where: { user_id, type },
|
|
defaults: { amount: 0 },
|
|
transaction: t,
|
|
});
|
|
|
|
const operation = type === 'expanse' ? -1 : 1;
|
|
|
|
await balance.increment('amount', {
|
|
by: amount * operation,
|
|
transaction: t
|
|
});
|
|
|
|
await balance.reload({ transaction: t });
|
|
}
|