Skip to content

Commit

Permalink
Merge pull request #2880 from LiteFarmOrg/LF-3649-knex-timeout-acquir…
Browse files Browse the repository at this point in the history
…ing-a-connection-the-pool-is-probably-full-are-you-missing-a-transacting-trx-call

LF-3649 Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
  • Loading branch information
Duncan-Brain authored Sep 19, 2023
2 parents b37c668 + 1d48b58 commit d75c7c3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/api/src/controllers/farmExpenseTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const farmExpenseTypeController = {
const data = req.body;
data.expense_translation_key = baseController.formatTranslationKey(data.expense_name);

const record = await this.existsInFarm(farm_id, data.expense_name);
const record = await this.existsInFarm(trx, farm_id, data.expense_name);
// if record exists in db
if (record) {
// if not deleted, means it is a active expense type
// throw conflict error
if (record.deleted === false) {
await trx.rollback();
return res.status(409).send();
} else {
// if its deleted, them make it active
Expand Down Expand Up @@ -96,11 +97,13 @@ const farmExpenseTypeController = {
return async (req, res) => {
const trx = await transaction.start(Model.knex());
if (req.headers.farm_id == null) {
await trx.rollback();
res.sendStatus(403);
}
try {
// do not allow operations to deleted records
if (await this.isDeleted(req.params.expense_type_id)) {
if (await this.isDeleted(req.params.expense_type_id, trx)) {
await trx.rollback();
return res.status(404).send();
}

Expand Down Expand Up @@ -136,16 +139,19 @@ const farmExpenseTypeController = {
try {
// do not allow updating of farm_id
if (data.farm_id && data.farm_id !== farm_id) {
await trx.rollback();
return res.status(400).send();
}

// do not allow update to deleted records
if (await this.isDeleted(expense_type_id)) {
if (await this.isDeleted(expense_type_id, trx)) {
await trx.rollback();
return res.status(404).send();
}

// if record exists then throw Conflict error
if (await this.existsInFarm(farm_id, data.expense_name, expense_type_id)) {
if (await this.existsInFarm(trx, farm_id, data.expense_name, expense_type_id)) {
await trx.rollback();
return res.status(409).send();
}

Expand All @@ -165,14 +171,15 @@ const farmExpenseTypeController = {

/**
* Check if records exists in DB
* @param {Object} trx - transaction object
* @param {number} farm_id
* @param {String} expense_name
* @param {number} expense_type_id - Expesnse type id to be excluded while checking records
* @async
* @returns {Promise} - Object DB record promise
*/
existsInFarm(farm_id, expense_name, expense_type_id = '') {
let query = ExpenseTypeModel.query().context({ showHidden: true }).where({
existsInFarm(trx, farm_id, expense_name, expense_type_id = '') {
let query = ExpenseTypeModel.query(trx).context({ showHidden: true }).where({
expense_name,
farm_id,
});
Expand All @@ -187,11 +194,12 @@ const farmExpenseTypeController = {
/**
* To check if record is deleted or not
* @param {number} expense_type_id - Expesnse type id
* @param {Object} trx - transaction object
* @async
* @returns {Boolean} - true or false
*/
async isDeleted(expense_type_id) {
const expense = await ExpenseTypeModel.query()
async isDeleted(expense_type_id, trx) {
const expense = await ExpenseTypeModel.query(trx)
.context({ showHidden: true })
.where({
expense_type_id,
Expand Down

0 comments on commit d75c7c3

Please sign in to comment.