created API For Aplication Absensi
This commit is contained in:
22
app/helpers/updateBalance.js
Normal file
22
app/helpers/updateBalance.js
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user