Skip to content

Commit

Permalink
Add option to only show most recent asset scan
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcameron committed May 9, 2022
1 parent 08070ac commit 1ed0210
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/en/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"SHOW_NOT_ASSIGNED" : "Unassigned assets",
"SHOW_ONLY" : "Show only",
"SHOW_ONLY_ASSIGNED" : "Assigned assets",
"SHOW_ONLY_LAST_SCAN" : "Show only last scan",
"SHOW_SCANNED_ASSETS" : "Show Scanned Assets",
"SHOW_UNSCANNED_ASSETS" : "Show Unscanned Assets",
"SUCCESSFULLY_EDITED":"Asset successfully updated",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function AssetsScansRegistryService(Session, Filters, AppCache, bhConstants, Per
{ key : 'inventory_uuid', label : 'FORM.LABELS.INVENTORY' },
{ key : 'group_uuid', label : 'STOCK.INVENTORY_GROUP' },
{ key : 'reference_number', label : 'FORM.LABELS.REFERENCE_NUMBER' },
{ key : 'show_only_last_scans', label : 'ASSET.SHOW_ONLY_LAST_SCAN' },
]);

if (filterCache.filters) {
Expand Down
14 changes: 13 additions & 1 deletion client/src/modules/asset_scans/modals/search.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
<bh-clear on-clear="$ctrl.clear('reference_number')"></bh-clear>
</div>
</div>

<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox"
ng-model="$ctrl.showOnlyLatestScan"
ng-change="$ctrl.onShowOnlyLatestScan()"
ng-true-value="1" ng-false-value="0">
<span translate>ASSET.SHOW_ONLY_LAST_SCAN</span>
</label>
</div>
</div>

</div>
</uib-tab>
Expand Down
16 changes: 16 additions & 0 deletions client/src/modules/asset_scans/modals/search.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function AssetScansSearchModalController(data, util, Store, Instance, Stock, Sea

const searchQueryOptions = [
'depot_uuid', 'inventory_uuid', 'group_uuid', 'asset_label', 'reference_number',
'show_only_last_scans',
];

// displayValues will be an id:displayValue pair
Expand All @@ -25,6 +26,12 @@ function AssetScansSearchModalController(data, util, Store, Instance, Stock, Sea
// assign already defined custom filters to searchQueries object
vm.searchQueries = util.maskObjectFromKeys(data, searchQueryOptions);

// Set the excludeAssets flag based on the existing custom search filter
vm.showOnlyLatestScan = 0;
if ('show_only_last_scans' in vm.searchQueries && !vm.searchQueries.show_only_last_scans) {
vm.showOnlyLatestScan = 1;
}

// custom filter depot_uuid - assign the value to the params object
vm.onSelectDepot = function onSelectDepot(depot) {
vm.searchQueries.depot_uuid = depot.uuid;
Expand All @@ -43,6 +50,15 @@ function AssetScansSearchModalController(data, util, Store, Instance, Stock, Sea
displayValues.group_uuid = group.name;
};

// custom filter - flag to only show the latests scan
vm.onShowOnlyLatestScan = function onShowOnlyLatestScan() {
if (vm.showOnlyLatestScan) {
vm.searchQueries.show_only_last_scans = 1;
} else {
vm.clear('show_only_last_scans');
}
};

// default filter limit - directly write to changes list
vm.onSelectLimit = function onSelectLimit(value) {
// input is type value, this will only be defined for a valid number
Expand Down
14 changes: 13 additions & 1 deletion server/controllers/stock/asset_scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function getAssetScanFilters(parameters) {
filters.dateFrom('custom_period_start', 'created_at');
filters.dateTo('custom_period_end', 'created_at');

filters.custom('show_only_last_scans', '(s2.uuid IS NULL)');

return filters;
}

Expand Down Expand Up @@ -98,6 +100,16 @@ exports.getAssetScans = async function getAssetScans(req, res, next) {
// Get the asset scans
function listAssetScans(params) {
const filters = getAssetScanFilters(params);
let lastScanJoin = '';
if ('show_only_last_scans' in params) {
// See 'Outer Join Method' in https://thoughtbot.com/blog/ordering-within-a-sql-group-by-clause
// This join with the 'show_only_last_scans' filter (defined above) ensures that we
// are not selecting only the latest⁄last asset scan for each asset.
lastScanJoin = `
LEFT OUTER JOIN asset_scan AS s2
ON s2.asset_uuid = s.asset_uuid AND
s.created_at < s2.created_at`;
}

const sql = `
SELECT
Expand All @@ -115,7 +127,7 @@ function listAssetScans(params) {
JOIN inventory i ON i.uuid = l.inventory_uuid AND i.is_asset = 1
JOIN inventory_group ig ON ig.uuid = i.group_uuid
JOIN depot d ON d.uuid = s.depot_uuid
JOIN user AS u ON u.id = s.scanned_by
JOIN user AS u ON u.id = s.scanned_by ${lastScanJoin}
LEFT JOIN stock_assign sa ON sa.lot_uuid = l.uuid AND sa.is_active = 1
LEFT JOIN entity e ON e.uuid = sa.entity_uuid
`;
Expand Down

0 comments on commit 1ed0210

Please sign in to comment.