Skip to content

Commit

Permalink
OLMIS-7993: added stock card cache
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikNoga committed Oct 8, 2024
1 parent bdf748e commit 2e1dda3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/stock-card/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"stockCard.lot": "Lot number",
"stockCard.expiryDate": "Expiry date",
"stockCard.physicalInventory": "Physical Inventory",
"stockCard.print": "Print"
"stockCard.print": "Print",
"stockCard.notCached.error": "This stock card isn't cached. You'll need to go online to view and save it for offline use."
}
40 changes: 40 additions & 0 deletions src/stock-card/stock-card-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

(function() {

'use strict';

angular
.module('stock-card')
.factory('StockCardResource', StockCardResource);

StockCardResource.inject = ['OpenlmisCachedResource', 'classExtender'];

function StockCardResource(OpenlmisCachedResource, classExtender) {

classExtender.extend(StockCardResource, OpenlmisCachedResource);

return StockCardResource;

function StockCardResource() {
this.super('/api/stockCards', 'stockCards', {
versioned: false
});
}

}

})();
3 changes: 2 additions & 1 deletion src/stock-card/stock-card.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
angular.module('stock-card', [
'stockmanagement',
'openlmis-auth',
'stock-reasons-modal'
'stock-reasons-modal',
'openlmis-cached-repository'
]);

})();
Expand Down
7 changes: 6 additions & 1 deletion src/stock-card/stock-card.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
$stateProvider.state('openlmis.stockmanagement.stockCardSummaries.singleCard', {
url: '/:stockCardId?stockCardPage&stockCardSize',
showInNavigation: false,
isOffline: true,
views: {
'@openlmis': {
controller: 'StockCardController',
Expand All @@ -35,7 +36,7 @@
},
accessRights: [STOCKMANAGEMENT_RIGHTS.STOCK_CARDS_VIEW],
resolve: {
stockCard: function($stateParams, stockCardService, paginationService, StockCard) {
stockCard: function($stateParams, stockCardService, paginationService, StockCard, alertService, $q) {
return stockCardService
.getStockCard($stateParams.stockCardId)
.then(function(json) {
Expand All @@ -49,6 +50,10 @@
paginationId: 'stockCard'
});
return stockCard;
})
.catch(function(error) {
alertService.error(error.message);
return $q.reject();
});
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/stock-card/stock-card.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
.module('stock-card')
.service('stockCardService', service);

service.$inject = ['$resource', '$window', 'stockmanagementUrlFactory', 'accessTokenFactory', 'dateUtils'];
service.$inject = ['$resource', '$window', 'stockmanagementUrlFactory', 'accessTokenFactory', 'dateUtils',
'StockCardResource', '$q', 'offlineService'];

function service($resource, $window, stockmanagementUrlFactory, accessTokenFactory, dateUtils) {
function service($resource, $window, stockmanagementUrlFactory, accessTokenFactory, dateUtils,
StockCardResource, $q, offlineService) {
var stockCardResource = new StockCardResource();
var resource = $resource(stockmanagementUrlFactory('/api/stockCards/:stockCardId'), {}, {
get: {
method: 'GET',
Expand Down Expand Up @@ -58,9 +61,13 @@
* @return {Promise} stock card promise.
*/
function getStockCard(stockCardId) {
return resource.get({
stockCardId: stockCardId
}).$promise;
return stockCardResource.get(stockCardId)
.then(function(stockCard) {
if (!stockCard && offlineService.isOffline()) {
throw new Error('stockCard.notCached.error');
}
return $q.resolve(stockCard);
});
}

/**
Expand Down
21 changes: 1 addition & 20 deletions src/stock-card/stock-card.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('stockCardService', function() {
this.$rootScope = $injector.get('$rootScope');
this.$httpBackend = $injector.get('$httpBackend');
this.stockCardService = $injector.get('stockCardService');
this.StockCardResource = $injector.get('StockCardResource');
this.stockmanagementUrlFactory = $injector.get('stockmanagementUrlFactory');
this.accessTokenFactory = $injector.get('accessTokenFactory');
this.dateUtils = $injector.get('dateUtils');
Expand All @@ -36,31 +37,11 @@ describe('stockCardService', function() {
});

describe('getStockCard', function() {

beforeEach(function() {
this.$httpBackend.when('GET', this.stockmanagementUrlFactory('/api/stockCards/' + this.stockCard.id))
.respond(200, this.stockCard);
});

it('should return promise', function() {
var result = this.stockCardService.getStockCard(this.stockCard.id);
this.$httpBackend.flush();

expect(result.then).not.toBeUndefined();
});

it('should resolve to stock card', function() {
var result;

this.stockCardService.getStockCard(this.stockCard.id).then(function(data) {
result = data;
});
this.$httpBackend.flush();
this.$rootScope.$apply();

expect(angular.toJson(result)).toEqual(angular.toJson(this.stockCard));
expect(result.lot.expirationDate).toEqual(this.dateUtils.toDate(this.stockCard.lot.expirationDate));
});
});

describe('updateStockCardStatus', function() {
Expand Down

0 comments on commit 2e1dda3

Please sign in to comment.