Skip to content

Commit

Permalink
fix: do not crash on no account references
Browse files Browse the repository at this point in the history
This commit prevents an invalid SQL request from being executed when no
account references exist.
  • Loading branch information
jniles committed Aug 16, 2021
1 parent f38db85 commit eb8020c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const fiscal = require('../fiscal');
async function configuration(req, res, next) {
try {
const { query } = req;

const params = {
typeFeeCenter : query.typeFeeCenter,
fee_center_id : query.fee_center_id,
Expand All @@ -21,6 +22,11 @@ async function configuration(req, res, next) {
const refAccounts = accounts;
const accountsId = accounts.map(account => account.account_id);

if (accountsId.length === 0) {
res.status(200).json([]);
return;
}

const options = {
accounts_id : accountsId,
excludes_distributed : true,
Expand All @@ -40,6 +46,7 @@ async function configuration(req, res, next) {
if (query.fee_center_id || query.trans_id) {
delete options.limit;
}

const rows = await generalLedger.findTransactions(options);

rows.forEach(item => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* of a profit or a cost, of an auxiliary center towards the main centers
*/
const db = require('../../../lib/db');
const BadRequest = require('../../../lib/errors/BadRequest');
const { BadRequest } = require('../../../lib/errors');

function proceed(req, res, next) {
const { data } = req.body;
Expand Down
26 changes: 15 additions & 11 deletions server/controllers/finance/distributionFeeCenter/setting.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
/**
* Distribution Fee Center Controller
*
* This function is used to set the various distribution keys of the ancillary cost centers to the Main center.
* This function is used to set the various distribution keys of the auxiliary cost centers to the main cost center.
*/

const db = require('../../../lib/db');

function setting(req, res, next) {
const { data } = req.body;

const dataValues = data.values;

data.user_id = req.session.user.id;

const distributionKey = [];

Object.entries(dataValues).forEach(([principalCenterId, rateDistribution]) => {
if (rateDistribution) {
distributionKey.push([
data.auxiliary_fee_center_id,
principalCenterId,
rateDistribution,
data.user_id,
]);
}
});
Object.entries(dataValues)
.forEach(([principalCenterId, rateDistribution]) => {
if (rateDistribution) {
distributionKey.push([
data.auxiliary_fee_center_id,
principalCenterId,
rateDistribution,
data.user_id,
]);
}
});

const delDistribution = `DELETE FROM distribution_key WHERE auxiliary_fee_center_id = ?`;

Expand Down

0 comments on commit eb8020c

Please sign in to comment.