Skip to content

Commit

Permalink
perf(depots): reduce expired/stock out calls
Browse files Browse the repository at this point in the history
This commit reduces the number of stock out and expired stock calls made
by the components checking for stock out by caching the results on the
client.
  • Loading branch information
jniles committed Mar 4, 2022
1 parent 698c24f commit 84171f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
45 changes: 26 additions & 19 deletions client/src/js/components/bhStockExpired/bhStockExpired.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,40 @@ angular.module('bhima.components')
});

bhStockExpiredController.$inject = [
'StockService', 'moment', 'NotifyService', 'DepotService', '$filter',
'StockService', 'moment', 'NotifyService', 'DepotService', '$filter', '$q',
];

/**
* Stock Expired component
*/
function bhStockExpiredController(Stock, moment, Notify, Depot, $filter) {
function bhStockExpiredController(Stock, moment, Notify, Depots, $filter, $q) {
const $ctrl = this;

$ctrl.loading = false;
$ctrl.expiredInventories = [];

const $date = $filter('date');

$ctrl.$onInit = () => {
fetchExpiredStock();
getDepot();
$ctrl.loading = true;
$ctrl.expiredInventories = [];

$q.all([
fetchExpiredStock(),
getDepot(),
])
.finally(() => { $ctrl.loading = false; });
};

$ctrl.$onChanges = () => {
fetchExpiredStock();
getDepot();
$ctrl.loading = true;
$q.all([
fetchExpiredStock(),
getDepot(),
])
.finally(() => { $ctrl.loading = false; });
};

function getDepot() {
if (!$ctrl.depotUuid) return;
Depot.read($ctrl.depotUuid)
if (!$ctrl.depotUuid) return 0;

return Depots.read($ctrl.depotUuid)
.then(depot => {
$ctrl.depot = depot;
});
Expand All @@ -48,11 +55,14 @@ function bhStockExpiredController(Stock, moment, Notify, Depot, $filter) {
* Gets expired inventories for a depot
*/
function fetchExpiredStock() {
if (!$ctrl.depotUuid) return;
if (!$ctrl.depotUuid) return 0;

const dateTo = $ctrl.date || new Date();
$ctrl.loading = true;

Depot.getExpiredStockForDate($ctrl.depotUuid, dateTo)
// format date into a cacheable format
const dateToFormatted = $date(dateTo, 'yyyy-MM-dd');

return Depots.getExpiredStockForDate($ctrl.depotUuid, dateToFormatted)
.then((inventories) => {
inventories.forEach(inventory => {
inventory.expiration_date_raw = $date(inventory.expiration_date);
Expand All @@ -61,10 +71,7 @@ function bhStockExpiredController(Stock, moment, Notify, Depot, $filter) {

$ctrl.expiredInventories = inventories;
})
.catch(Notify.handleError)
.finally(() => {
$ctrl.loading = false;
});
.catch(Notify.handleError);
}

}
22 changes: 17 additions & 5 deletions client/src/modules/depots/depots.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ function DepotService(Api, Modal, HttpCache) {
.then(service.util.unwrapHttpResponse);
};

function stockOutFetcherCallback(depotUuid, date) {
const target = `/depots/${depotUuid}/flags/stock_out`;
return service.$http.get(target, { params : { date } })
.then(service.util.unwrapHttpResponse);
}

const getStockOutFetcher = HttpCache(stockOutFetcherCallback, 3000);

/**
* @function getStockOutsForDate
*
Expand All @@ -85,10 +93,16 @@ function DepotService(Api, Modal, HttpCache) {
* it uses the stock_movement_status table.
*/
service.getStockOutsForDate = function getStockOutsForDate(depotUuid, date = new Date()) {
const target = `/depots/${depotUuid}/flags/stock_out`;
return getStockOutFetcher(depotUuid, date);
};

function expiredStockFetcherCallback(depotUuid, date) {
const target = `/depots/${depotUuid}/flags/expired`;
return service.$http.get(target, { params : { date } })
.then(service.util.unwrapHttpResponse);
};
}

const getExpiredStockFetcher = HttpCache(expiredStockFetcherCallback, 3000);

/**
* @function getExpiredStockForDate
Expand All @@ -97,9 +111,7 @@ function DepotService(Api, Modal, HttpCache) {
* Returns lots that are expired in the depot at a given date.
*/
service.getExpiredStockForDate = function getExpiredStock(depotUuid, date) {
const target = `/depots/${depotUuid}/flags/expired`;
return service.$http.get(target, { params : { date } })
.then(service.util.unwrapHttpResponse);
return getExpiredStockFetcher(depotUuid, date);
};

service.clean = depot => {
Expand Down

0 comments on commit 84171f8

Please sign in to comment.