mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat: Cachable and date session model.
This commit is contained in:
@@ -4,6 +4,7 @@ import JournalEntry from '@/services/Accounting/JournalEntry';
|
||||
import AccountTransaction from '@/models/AccountTransaction';
|
||||
import AccountBalance from '@/models/AccountBalance';
|
||||
import {promiseSerial} from '@/utils';
|
||||
import Account from '../../models/Account';
|
||||
|
||||
export default class JournalPoster {
|
||||
/**
|
||||
@@ -85,6 +86,8 @@ export default class JournalPoster {
|
||||
const balanceFindOneOpers = [];
|
||||
let balanceAccounts = [];
|
||||
|
||||
const effectAccountsOpers = [];
|
||||
|
||||
balancesList.forEach((balance) => {
|
||||
const oper = AccountBalance.tenant()
|
||||
.query().findOne('account_id', balance.account_id);
|
||||
@@ -113,12 +116,67 @@ export default class JournalPoster {
|
||||
});
|
||||
balanceInsertOpers.push(query);
|
||||
}
|
||||
|
||||
const effectedAccountsOper = this.effectAssociatedAccountsBalance(
|
||||
balance.accountId, amount, 'USD', method,
|
||||
);
|
||||
effectAccountsOpers.push(effectedAccountsOper);
|
||||
});
|
||||
await Promise.all([
|
||||
...balanceUpdateOpers, ...balanceInsertOpers,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Effect associated descendants and parent accounts
|
||||
* of the given account id.
|
||||
* @param {Number} accountId
|
||||
* @param {Number} amount
|
||||
* @param {String} currencyCode
|
||||
* @param {*} method
|
||||
*/
|
||||
async effectAssociatedAccountsBalance(accountId, amount, currencyCode = 'USD', method) {
|
||||
const accounts = await Account.query().withGraphFetched('balance');
|
||||
|
||||
const accountsDecendences = accounts.getDescendants();
|
||||
|
||||
const asyncOpers = [];
|
||||
const accountsInsertBalance = [];
|
||||
const accountsUpdateBalance = [];
|
||||
|
||||
accounts.forEach((account) => {
|
||||
const accountBalances = account.balance;
|
||||
const currencyBalance = accountBalances
|
||||
.find(balance => balance.currencyCode === currencyCode);
|
||||
|
||||
if (currencyBalance) {
|
||||
accountsInsertBalance.push(account.id);
|
||||
} else {
|
||||
accountsUpdateBalance.push(account.id);
|
||||
}
|
||||
});
|
||||
|
||||
accountsInsertBalance.forEach((accountId) => {
|
||||
const oper = AccountBalance.tenant().query().insert({
|
||||
account_id: accountId,
|
||||
amount: method === 'decrement' ? amount * -1 : amount,
|
||||
currency_code: currencyCode,
|
||||
});
|
||||
asyncOpers.push(oper);
|
||||
});
|
||||
|
||||
if (accountsUpdateBalance.length > 0) {
|
||||
const oper = AccountBalance.tenant().query()
|
||||
.whereIn('account_id', accountsUpdateBalance);
|
||||
[method]('amount', Math.abs(amount))
|
||||
.where('currency_code', currencyCode);
|
||||
|
||||
asyncOpers.push(oper);
|
||||
}
|
||||
await Promise.all(asyncOpers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the stacked journal entries to the storage.
|
||||
*/
|
||||
@@ -233,9 +291,9 @@ export default class JournalPoster {
|
||||
result.debit += entry.debit;
|
||||
|
||||
if (entry.accountNormal === 'credit') {
|
||||
result.balance += (entry.credit) ? entry.credit : -1 * entry.debit;
|
||||
result.balance += entry.credit - entry.debit;
|
||||
} else if (entry.accountNormal === 'debit') {
|
||||
result.balance += (entry.debit) ? entry.debit : -1 * entry.credit;
|
||||
result.balance += entry.debit - entry.credit;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
|
||||
52
server/src/services/Cache/index.js
Normal file
52
server/src/services/Cache/index.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import NodeCache from 'node-cache';
|
||||
|
||||
class Cache {
|
||||
|
||||
constructor() {
|
||||
this.cache = new NodeCache({
|
||||
// stdTTL: 9999999,
|
||||
// checkperiod: 9999999 * 0.2,
|
||||
useClones: false,
|
||||
});
|
||||
}
|
||||
|
||||
get(key, storeFunction) {
|
||||
const value = this.cache.get(key);
|
||||
|
||||
if (value) {
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
return storeFunction().then((result) => {
|
||||
this.cache.set(key, result);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
set(key, results) {
|
||||
this.cache.set(key, results);
|
||||
}
|
||||
|
||||
del(keys) {
|
||||
this.cache.del(keys);
|
||||
}
|
||||
|
||||
delStartWith(startStr = '') {
|
||||
if (!startStr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keys = this.cache.keys();
|
||||
for (const key of keys) {
|
||||
if (key.indexOf(startStr) === 0) {
|
||||
this.del(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flush() {
|
||||
this.cache.flushAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default new Cache();
|
||||
Reference in New Issue
Block a user