Skip to content

Commit

Permalink
Merge pull request #2886 from LiteFarmOrg/LF-3591-create-the-revenue-…
Browse files Browse the repository at this point in the history
…type-backend-api-route-and-controller-and-the-get-all-types-function

LF-3591 Create the revenueType backend api (route and controller) and the get all types function
  • Loading branch information
Duncan-Brain authored Sep 21, 2023
2 parents 489876c + f9770ce commit 2e68c32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
10 changes: 6 additions & 4 deletions packages/api/src/controllers/baseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,15 @@ export default {

/**
* To check if record is deleted or not
* @param {Object} trx - Transaction object
* @param {Object} model - Database model instance
* @param {object} where - 'Where' condition to fetch record
* @async
* @returns {Boolean} - true or false
*/
async isDeleted(model, where) {
async isDeleted(trx, model, where) {
const record = await model
.query()
.query(trx)
.context({ showHidden: true })
.where(where)
.select('deleted')
Expand All @@ -211,13 +212,14 @@ export default {

/**
* Check if records exists in table
* @param {object} trx - Transaction object
* @param {object} model - Database model instance
* @param {object} where - 'Where' condition to fetch record
* @param {object} whereNot - 'WhereNot' condition to fetch record
* @returns {Promise} - Object DB record promise
*/
existsInTable(model, where, whereNot = {}) {
let query = model.query().context({ showHidden: true }).where(where);
existsInTable(trx, model, where, whereNot = {}) {
let query = model.query(trx).context({ showHidden: true }).where(where);

if (Object.keys(whereNot).length > 0) {
query = query.whereNot(whereNot);
Expand Down
9 changes: 5 additions & 4 deletions packages/api/src/controllers/revenueTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const revenueTypeController = {
const data = req.body;
data.revenue_translation_key = baseController.formatTranslationKey(data.revenue_name);

const record = await baseController.existsInTable(RevenueTypeModel, {
const record = await baseController.existsInTable(trx, RevenueTypeModel, {
revenue_name: data.revenue_name,
farm_id,
});
Expand Down Expand Up @@ -68,7 +68,7 @@ const revenueTypeController = {
};
},

getAllTypes() {
getFarmRevenueType() {
return async (req, res) => {
try {
const farm_id = req.headers.farm_id;
Expand Down Expand Up @@ -113,7 +113,7 @@ const revenueTypeController = {
try {
// do not allow operations to deleted records
if (
await baseController.isDeleted(RevenueTypeModel, {
await baseController.isDeleted(trx, RevenueTypeModel, {
revenue_type_id: req.params.revenue_type_id,
})
) {
Expand Down Expand Up @@ -152,14 +152,15 @@ const revenueTypeController = {

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

// if record exists then throw Conflict error
if (
await baseController.existsInTable(
trx,
RevenueTypeModel,
{
revenue_name: data.revenue_name,
Expand Down
7 changes: 6 additions & 1 deletion packages/api/src/routes/revenueTypeRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ router.post(
checkScope(['add:revenue_types']),
RevenueTypeController.addType(),
);
router.get('/', checkScope(['get:revenue_types']), RevenueTypeController.getAllTypes());
router.get(
'/farm/:farm_id',
hasFarmAccess({ params: 'farm_id' }),
checkScope(['get:revenue_types']),
RevenueTypeController.getFarmRevenueType(),
);
router.get(
'/:revenue_type_id',
hasFarmAccess({ params: 'revenue_type_id' }),
Expand Down
2 changes: 1 addition & 1 deletion packages/api/tests/revenue_type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Revenue Type Tests', () => {
function getRequest({ user_id = newOwner.user_id, farm_id = farm.farm_id }, callback) {
chai
.request(server)
.get(`/revenue_type`)
.get(`/revenue_type/farm/${farm_id}`)
.set('user_id', user_id)
.set('farm_id', farm_id)
.end(callback);
Expand Down

0 comments on commit 2e68c32

Please sign in to comment.