feat: Cachable and date session model.

This commit is contained in:
Ahmed Bouhuolia
2020-05-20 06:51:34 +02:00
parent 10f636d2bc
commit 90dc83c70a
18 changed files with 638 additions and 200 deletions

View File

@@ -192,7 +192,7 @@ export default {
async handler(req, res) {
const { id } = req.params;
const { Account } = req.models;
const account = await Account.query().where('id', id).first();
const account = await Account.query().remember().where('id', id).first();
if (!account) {
return res.boom.notFound();
@@ -273,6 +273,7 @@ export default {
const errorReasons = [];
const accountsResource = await Resource.query()
.remember()
.where('name', 'accounts')
.withGraphFetched('fields')
.first();
@@ -294,6 +295,8 @@ export default {
builder.withGraphFetched('roles.field');
builder.withGraphFetched('columns');
builder.first();
builder.remember();
});
const dynamicFilter = new DynamicFilter(Account.tableName);
@@ -335,13 +338,15 @@ export default {
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
const accounts = await Account.query().onBuild((builder) => {
builder.modify('filterAccountTypes', filter.account_types);
builder.withGraphFetched('type');
builder.withGraphFetched('balance');
const accounts = await Account.query()
.remember()
.onBuild((builder) => {
builder.modify('filterAccountTypes', filter.account_types);
builder.withGraphFetched('type');
builder.withGraphFetched('balance');
dynamicFilter.buildQuery()(builder);
});
dynamicFilter.buildQuery()(builder);
});
const nestedAccounts = new NestedSet(accounts, { parentId: 'parentAccountId' });
const nestedSetAccounts = nestedAccounts.toTree();

View File

@@ -107,6 +107,7 @@ export default {
filter.account_ids = filter.account_ids.map((id) => parseInt(id, 10));
const accountsJournalEntries = await AccountTransaction.query()
.remember()
.modify('filterDateRange', filter.from_date, filter.to_date)
.modify('filterAccounts', filter.account_ids)
.modify('filterTransactionTypes', filter.transaction_types)
@@ -197,6 +198,7 @@ export default {
return res.status(400).send({ error: errorReasons });
}
const accounts = await Account.query()
.remember('general_ledger_accounts')
.orderBy('index', 'DESC')
.modify('filterAccounts', filter.accounts_ids)
.withGraphFetched('type')
@@ -206,11 +208,13 @@ export default {
});
const openingBalanceTransactions = await AccountTransaction.query()
.remember()
.modify('filterDateRange', null, filter.from_date)
.modify('sumationCreditDebit')
.withGraphFetched('account.type');
const closingBalanceTransactions = await AccountTransaction.query()
.remember()
.modify('filterDateRange', null, filter.to_date)
.modify('sumationCreditDebit')
.withGraphFetched('account.type');
@@ -226,7 +230,7 @@ export default {
const items = accounts
.filter((account) => (
account.transactions.length > 0 || filter.none_zero
account.transactions.length > 0 || !filter.none_zero
))
.map((account) => ({
...pick(account, ['id', 'name', 'code', 'index']),
@@ -248,11 +252,11 @@ export default {
],
opening: {
date: filter.from_date,
amount: opeingBalanceCollection.getClosingBalance(account.id),
amount: formatNumber(opeingBalanceCollection.getClosingBalance(account.id)),
},
closing: {
date: filter.to_date,
amount: closingBalanceCollection.getClosingBalance(account.id),
amount: formatNumber(closingBalanceCollection.getClosingBalance(account.id)),
},
}));
@@ -276,6 +280,8 @@ export default {
.isIn(['year', 'month', 'week', 'day', 'quarter']),
query('number_format.no_cents').optional().isBoolean().toBoolean(),
query('number_format.divide_1000').optional().isBoolean().toBoolean(),
query('account_ids').isArray().optional(),
query('account_ids.*').isNumeric().toInt(),
query('none_zero').optional().isBoolean().toBoolean(),
],
async handler(req, res) {
@@ -298,13 +304,20 @@ export default {
},
none_zero: false,
basis: 'cash',
account_ids: [],
...req.query,
};
if (!Array.isArray(filter.account_ids)) {
filter.account_ids = [filter.account_ids];
}
const balanceSheetTypes = await AccountType.query().where('balance_sheet', true);
// Fetch all balance sheet accounts.
const accounts = await Account.query()
.remember('balance_sheet_accounts')
.whereIn('account_type_id', balanceSheetTypes.map((a) => a.id))
.modify('filterAccounts', filter.account_ids)
.withGraphFetched('type')
.withGraphFetched('transactions')
.modifyGraph('transactions', (builder) => {
@@ -313,6 +326,7 @@ export default {
const journalEntriesCollected = Account.collectJournalEntries(accounts);
const journalEntries = new JournalPoster();
journalEntries.loadEntries(journalEntriesCollected);
// Account balance formmatter based on the given query.
@@ -378,10 +392,12 @@ export default {
accounts: [
{
name: 'Assets',
type: 'assets',
children: [...assets],
},
{
name: 'Liabilities & Equity',
type: 'liabilities_equity',
children: [...liabilitiesEquity],
},
],
@@ -426,6 +442,7 @@ export default {
};
const accounts = await Account.query()
.remember('trial_balance_accounts')
.withGraphFetched('type')
.withGraphFetched('transactions')
.modifyGraph('transactions', (builder) => {
@@ -474,7 +491,7 @@ export default {
query('number_format.no_cents').optional().isBoolean(),
query('number_format.divide_1000').optional().isBoolean(),
query('basis').optional(),
query('none_zero').optional(),
query('none_zero').optional().isBoolean().toBoolean(),
query('display_columns_type').optional().isIn([
'total', 'date_periods',
]),
@@ -507,6 +524,7 @@ export default {
// Fetch all income accounts from storage.
const accounts = await Account.query()
.remember('profit_loss_accounts')
.whereIn('account_type_id', incomeStatementTypes.map((t) => t.id))
.withGraphFetched('type')
.withGraphFetched('transactions');

View File

@@ -45,10 +45,10 @@ export default async (req, res, next) => {
req.knex = knex;
req.organizationId = organizationId;
req.models = {
...Object.values(models).reduce((acc, model) => {
if (model.resource
&& model.resource.default
&& Object.getPrototypeOf(model.resource.default) === TenantModel) {
...Object.values(models).reduce((acc, model) => {
if (typeof model.resource.default.requestModel === 'function' &&
model.resource.default.requestModel() &&
model.name !== 'TenantModel') {
acc[model.name] = model.resource.default.bindKnex(knex);
}
return acc;