Skip to content

Commit

Permalink
OLMIS-8022: improved cached resource
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikNoga committed Oct 8, 2024
1 parent 414e281 commit f937b26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
7.2.12-SNAPSHOT / WIP
7.2.12-SNAPSHOT / WIP
==================
Bug fixes:
* [OLMIS-7931](https://openlmis.atlassian.net/browse/OLMIS-7931): Fix imports for layout-ui jenkins job
* [OLMIS-8010](https://openlmis.atlassian.net/browse/OLMIS-8010): Added a condition for putAll function for local storage

New Functionalities:
* [OLMIS-7976](https://openlmis.atlassian.net/browse/OLMIS-7976): Improved visual apperance of the homepage alerts component
* [OLMIS-7976](https://openlmis.atlassian.net/browse/OLMIS-7976): Improved visual apperance of the homepage alerts component

New functionalities that are backwards-compatible:
* [OLMIS-7987](https://openlmis.atlassian.net/browse/OLMIS-7987): Move Submit Requisitionless Orders functionalities from Angola to Core instance

Improvements:
* [OIS-23](https://openlmis.atlassian.net/browse/OIS-23): Add basic mixin utils for RTL support
* [OLMIS-8022](https://openlmis.atlassian.net/browse/OLMIS-8022): Improvements in app offline capabilities.

7.2.11 / 2024-04-19
==================
Bug fixes:
* [OLMIS-7922](https://openlmis.atlassian.net/browse/OLMIS-7922): Fix missing loading spinner in the Data Export and Data Import pages
* [OLMIS-7903](https://openlmis.atlassian.net/browse/OLMIS-7903): Adjust BUQ colors with core UI
* [OLMIS-7903](https://openlmis.atlassian.net/browse/OLMIS-7903): Adjust BUQ colors with core UI
* [OLMIS-7906](https://openlmis.atlassian.net/browse/OLMIS-7906): Adjust BUQ to core geographic levels

New Functionalities:
Expand Down Expand Up @@ -113,7 +114,7 @@ Bug fixes:
Improvements:
* [OLMIS-6684](https://openlmis.atlassian.net/browse/OLMIS-6684): Updated pagination component to allow providing a function to call before changing page.
* Got rid of angular.copy to improve performance in some places.

Bug fixes:
* [OLMIS-6612](https://openlmis.atlassian.net/browse/OLMIS-6612): Added check whether the property exists before sorting array by given property.
* [OLMIS-6740](https://openlmis.atlassian.net/browse/OLMIS-6740): Fixed wrapping text in modal body.
Expand Down
37 changes: 23 additions & 14 deletions src/openlmis-cached-repository/openlmis-cached-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,22 @@
database = this.database,
isVersioned = this.isVersioned;

return database.allDocsByIndex(id, versionId).then(function(result) {
if (!result[0] || !versionId) {
openlmisResource.lastModified = result[0] ? result[0].lastModified : undefined;
return openlmisResource.get(id, versionId)
.then(function(response) {
isVersioned ? database.putVersioned(response.content) : database.put(response.content);
return response.content;
}, function(response) {
if (response.status === 304) {
return result[0];
}
if (offlineService.isOffline()) {
return database.allDocsByIndex(id, versionId).then(function(result) {
return result[0];
});
}
return openlmisResource.get(id, versionId)
.then(function(response) {
isVersioned ? database.putVersioned(response.content) : database.put(response.content);
return response.content;
}, function(response) {
if (response.status === 304) {
return database.allDocsByIndex(id, versionId).then(function(result) {
return result[0];
});
}
return result[0];
});
}
});
}
return $q.reject();
}
Expand Down Expand Up @@ -259,7 +260,15 @@
* rejected if request fails
*/
function getAll(params) {
if (offlineService.isOffline()) {
var deferred = $q.defer();
this.database.getAll().then(function(response) {
deferred.resolve(response);
}, deferred.reject);
return deferred.promise;
}
var config = this.config;

return this.query(params)
.then(function(response) {
return isPaginated(config) ? response.content : response;
Expand Down
28 changes: 1 addition & 27 deletions src/openlmis-cached-repository/openlmis-cached-resource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ describe('OpenlmisCachedResource', function() {
this.getDeferred.resolve(this.response);
this.$rootScope.$apply();

expect(this.LocalDatabase.prototype.allDocsByIndex).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.putVersioned).not.toHaveBeenCalled();
expect(this.LocalDatabase.prototype.put).toHaveBeenCalled();
});
Expand All @@ -95,7 +94,6 @@ describe('OpenlmisCachedResource', function() {
this.getDeferred.resolve(this.response);
this.$rootScope.$apply();

expect(this.LocalDatabase.prototype.allDocsByIndex).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.putVersioned).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled();
});
Expand All @@ -122,30 +120,6 @@ describe('OpenlmisCachedResource', function() {
this.$rootScope.$apply();

expect(result.$$state.value).toEqual(this.page[0]);
expect(this.LocalDatabase.prototype.allDocsByIndex).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.putVersioned).not.toHaveBeenCalled();
expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled();
});

it('should no request if record with the specified version is in the local database', function() {
this.page = [{
id: 'some-id',
_id: 'some-id/2',
latest: true
}, {
id: 'some-id',
_id: 'some-id/1',
latest: false
}];

this.openlmisCachedResource.isVersioned = true;

this.allDocsByIndexDeferred.resolve(this.page);
var result = this.openlmisCachedResource.get(this.id, 1);
this.$rootScope.$apply();

expect(result.$$state.value).toEqual(this.page[0]);
expect(this.LocalDatabase.prototype.allDocsByIndex).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.putVersioned).not.toHaveBeenCalled();
expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled();
});
Expand All @@ -156,8 +130,8 @@ describe('OpenlmisCachedResource', function() {
this.getDeferred.reject();
this.$rootScope.$apply();

expect(this.LocalDatabase.prototype.allDocsByIndex).toHaveBeenCalled();
expect(this.LocalDatabase.prototype.putVersioned).not.toHaveBeenCalled();
expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled();
});

it('should reject if null was given', function() {
Expand Down

0 comments on commit f937b26

Please sign in to comment.