Skip to content

Commit

Permalink
bug(Asset Register)
Browse files Browse the repository at this point in the history
- Hide the asset where quantity in stock is 0
- Add HAVING filters in SQL Clause format

closes #6842
  • Loading branch information
lomamech committed Oct 15, 2022
1 parent 5f9e4ce commit 84b958e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion server/controllers/stock/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ async function getAssets(params) {
) AS last_scan ON last_scan.asset_uuid = l.uuid
`;

const groupByClause = ` GROUP BY l.uuid, m.depot_uuid ORDER BY i.code, l.label `;
const groupByClause = ` GROUP BY l.uuid, m.depot_uuid `;
const havingClause = ` HAVING quantity > 0 `;

const filters = getLotFilters(params);
if (['scanned', 'unscanned'].includes(params.scan_status)) {
Expand All @@ -318,6 +319,8 @@ async function getAssets(params) {
}
filters.setGroup(groupByClause);

filters.setHaving(havingClause);
filters.setOrder('ORDER BY i.code, l.label');
const query = filters.applyQuery(sql);
const queryParameters = filters.parameters();

Expand Down
16 changes: 15 additions & 1 deletion server/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class FilterParser {
this._parseUuids = _.isUndefined(options.parseUuids) ? true : options.parseUuids;
this._autoParseStatements = _.isUndefined(options.autoParseStatements) ? false : options.autoParseStatements;
this._group = '';
this._having = '';
}

/**
Expand Down Expand Up @@ -203,6 +204,18 @@ class FilterParser {
this._group = groupString;
}

/**
* @method setHaving
*
* @description
* Allows setting the SQL Having in the HAVING statement. A developer is expected to
* provide a valid SQL string. This will be appended to the SQL statement after the
* WHERE clause and/or GROUP BY
*/
setHaving(havingClause) {
this._having = havingClause;
}

applyQuery(sql, ignoreLimit = false) {
// optionally call utility method to parse all remaining options as simple
// equality filters into `_statements`
Expand All @@ -215,8 +228,9 @@ class FilterParser {
const conditionStatements = this._parseStatements();
const order = this._order;
const group = this._group;
const having = this._having;

return `${sql} WHERE ${conditionStatements} ${group} ${order} ${limitCondition}`;
return `${sql} WHERE ${conditionStatements} ${group} ${having} ${order} ${limitCondition}`;
}

parameters() {
Expand Down

0 comments on commit 84b958e

Please sign in to comment.