From 2e1dda3e2c4712932cd851eac8863dc3d41a4d10 Mon Sep 17 00:00:00 2001 From: DominikNoga Date: Mon, 7 Oct 2024 15:15:03 +0200 Subject: [PATCH] OLMIS-7993: added stock card cache --- src/stock-card/messages_en.json | 3 +- src/stock-card/stock-card-resource.js | 40 +++++++++++++++++++++++ src/stock-card/stock-card.module.js | 3 +- src/stock-card/stock-card.routes.js | 7 +++- src/stock-card/stock-card.service.js | 17 +++++++--- src/stock-card/stock-card.service.spec.js | 21 +----------- 6 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 src/stock-card/stock-card-resource.js diff --git a/src/stock-card/messages_en.json b/src/stock-card/messages_en.json index 3f060ff5..4bbda343 100644 --- a/src/stock-card/messages_en.json +++ b/src/stock-card/messages_en.json @@ -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." } diff --git a/src/stock-card/stock-card-resource.js b/src/stock-card/stock-card-resource.js new file mode 100644 index 00000000..0db80ad4 --- /dev/null +++ b/src/stock-card/stock-card-resource.js @@ -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 + }); + } + + } + +})(); \ No newline at end of file diff --git a/src/stock-card/stock-card.module.js b/src/stock-card/stock-card.module.js index 9f523e8d..a9aa9d35 100644 --- a/src/stock-card/stock-card.module.js +++ b/src/stock-card/stock-card.module.js @@ -26,7 +26,8 @@ angular.module('stock-card', [ 'stockmanagement', 'openlmis-auth', - 'stock-reasons-modal' + 'stock-reasons-modal', + 'openlmis-cached-repository' ]); })(); diff --git a/src/stock-card/stock-card.routes.js b/src/stock-card/stock-card.routes.js index 37cca53f..436ecd11 100644 --- a/src/stock-card/stock-card.routes.js +++ b/src/stock-card/stock-card.routes.js @@ -26,6 +26,7 @@ $stateProvider.state('openlmis.stockmanagement.stockCardSummaries.singleCard', { url: '/:stockCardId?stockCardPage&stockCardSize', showInNavigation: false, + isOffline: true, views: { '@openlmis': { controller: 'StockCardController', @@ -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) { @@ -49,6 +50,10 @@ paginationId: 'stockCard' }); return stockCard; + }) + .catch(function(error) { + alertService.error(error.message); + return $q.reject(); }); } } diff --git a/src/stock-card/stock-card.service.js b/src/stock-card/stock-card.service.js index a2991c82..3ecf3aef 100644 --- a/src/stock-card/stock-card.service.js +++ b/src/stock-card/stock-card.service.js @@ -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', @@ -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); + }); } /** diff --git a/src/stock-card/stock-card.service.spec.js b/src/stock-card/stock-card.service.spec.js index 6fa6ca22..8232f19d 100644 --- a/src/stock-card/stock-card.service.spec.js +++ b/src/stock-card/stock-card.service.spec.js @@ -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'); @@ -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() {