Skip to content

Commit

Permalink
feat: return deleted resources object from submitChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
kanikasharma97 committed Apr 24, 2024
1 parent b6fc4e2 commit d4f361d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/sap/fhir/model/r4/FHIRModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ sap.ui.define([
*/
FHIRModel.prototype.submitChanges =
function(sGroupId, fnSuccessCallback, fnErrorCallback) {
var removedResources = this.getRemovedResourcesObject();
if (typeof sGroupId === "function") {
fnErrorCallback = fnSuccessCallback;
fnSuccessCallback = FHIRUtils.deepClone(sGroupId);
Expand Down Expand Up @@ -693,9 +694,17 @@ sap.ui.define([
);
aPromises.push(oPromise);
oPromise.then(function (aFHIRResource) {
if (aFHIRResource.length == 0) {
aFHIRResource = removedResources;
}
fnSuccessCallback(aFHIRResource);
}).catch(function (oError) {
if (fnErrorCallback && oError.requestHandle) {
var sIds = FHIRUtils.getsIdFromOperationOutcome(oError.operationOutcomes);
removedResources = removedResources.filter(obj => !sIds.includes(obj.id));
if (oError.resources.length == 0) {
oError.resources = removedResources;
}
var mParameters = {
message: oError.requestHandle.getRequest().statusText,
description: oError.requestHandle.getRequest().responseText,
Expand Down Expand Up @@ -806,6 +815,28 @@ sap.ui.define([
return mRequestHandles;
};

/**
* Retrieves an array of resources that have been removed from the FHIR model.
* Iterates through the removed resources,
* retrieves corresponding resources from the model, and returns them.
* @returns {Array} An array containing the removed resources.
*/
FHIRModel.prototype.getRemovedResourcesObject = function () {
var resources = [];
for (var type in this.mRemovedResources) {
if (this.mRemovedResources.hasOwnProperty(type)) {
var removedResources = this.mRemovedResources[type];
for (var key in removedResources) {
var resource = this.getProperty("/" + removedResources[key]);
if (resource) {
resources.push(resource);
}
}
}
}
return resources;
};

/**
* Checks if an update for the existing bindings is necessary due to the <code>mChangedResources</code>
*
Expand Down
14 changes: 14 additions & 0 deletions src/sap/fhir/model/r4/FHIRUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,20 @@ sap.ui.define([
return sFullUrl;
};

FHIRUtils.getsIdFromOperationOutcome = function(operationOutcomes){
var sIds = [];
for (var key in operationOutcomes) {
if (operationOutcomes.hasOwnProperty(key)) {
var operationOutcome = operationOutcomes[key];
var text = operationOutcome._aIssue[0].details.text;
var sId = text.match(/[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}/);
sIds.push(sId[0]);
}
}
return sIds;

};

return FHIRUtils;

});
43 changes: 43 additions & 0 deletions test/localService/BundleWithDeleteSuccessAndFailureEntries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"resourceType": "Bundle",
"id": "934953d3-35a3-477d-8c9e-0948a7e553ec",
"links": [],
"type": "batch-response",
"entry": [
{
"response": {
"status": "409 CONFLICT",
"outcome": {
"issue": [
{
"severity": "error",
"code": "conflict",
"details": {
"text": "Referenced resource exist 'e1692049-4bdc-4e40-abb5-4eae27ce0b8b' for resource 'RolePermission}'"
},
"diagnostics": "Referenced resource exist 'e1692049-4bdc-4e40-abb5-4eae27ce0b8b' for resource 'RolePermission}'"
}
],
"resourceType": "OperationOutcome",
"meta": {
"profile": [
"http://hl7.org/fhir/StructureDefinition/OperationOutcome"
]
}
}
}
},
{
"response": {
"status": "204 NO_CONTENT"
},
"fullUrl": "urn:uuid:cb62c497-ee6c-4c26-9721-2668f797e6c8"
}
],
"total": 2,
"meta": {
"profile": [
"http://hl7.org/fhir/StructureDefinition/Bundle"
]
}
}
18 changes: 18 additions & 0 deletions test/qunit/model/FHIRModel.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,22 @@ sap.ui.define([
oListBinding.attachDataReceived(fnDataReceivedCheck);
});

QUnit.test("Test delete containing successful entry and operation outcome", function (assert) {
var oJSONData = TestUtils.loadJSONFile("BundleWithDeleteSuccessAndFailureEntries");
var done = assert.async();
var sResId = this.oFhirModel.create("RolePermission", {
resourceType: "RolePermission",
version: "1.0.0",
name: "PersonaRead"
}, "bundle");
this.oFhirModel.mChangedResources = {};
this.oFhirModel.remove(["/RolePermission/" + sResId], undefined, "bundle");
var fnErrorCallback = function (oMessage, aFHIRResource, aOperationOutcome) {
assert.strictEqual(aOperationOutcome.length, 1, "Bundle error callback contains the opertion outcome of the failed entry ");
done();
};
TestUtilsIntegration.manipulateResponse("http://localhost:8080/fhir/R4", this.oFhirModel, TestUtilsIntegration.setResponseJSON, undefined, oJSONData);
this.oFhirModel.submitChanges("bundle", undefined, fnErrorCallback);
});

});
7 changes: 7 additions & 0 deletions test/qunit/model/FHIRModel.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1503,4 +1503,11 @@ sap.ui.define([

});

QUnit.test("Test getRemovedResourcesObject returning the correct value", function (assert) {
this.oFhirModel1.mRemovedResources = { RolePermission: ["RolePermission/123"] };
this.oFhirModel1.setProperty("/RolePermission/123", { id: 123, description: "this is a test object", resourceType: "RolePermission" });
var resources = this.oFhirModel1.getRemovedResourcesObject();
assert.deepEqual(resources, [{ id: 123, description: "this is a test object", resourceType: "RolePermission" }], "Correct resources returned");
});

});
24 changes: 24 additions & 0 deletions test/qunit/model/FHIRUtils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,28 @@ sap.ui.define(["../utils/TestUtils", "sap/fhir/model/r4/FHIRUtils"], function(Te
sFullUrl = FHIRUtils.generateFullUrl(TestUtils.createUri("url"), "/Patient/123", "123", "http://example.com");
assert.strictEqual(sFullUrl, "http://example.com/Patient/123");
});

Check failure on line 519 in test/qunit/model/FHIRUtils.unit.js

View workflow job for this annotation

GitHub Actions / Build, Lint Checks and Tests

Trailing spaces not allowed
QUnit.test("Test getIDsFromOperationOutcomes", function(assert) {
var operationOutcomes = {
"0": {
"_sResourceType": "OperationOutcome",
"_aIssue": [
{
"severity": "error",
"code": "conflict",
"details": {
"text": "Referenced resource exist '7b4abf15-8a93-4e11-8d85-96c945530d05' for resource 'RolePermission}'"
},
"diagnostics": "Referenced resource exist '7b4abf15-8a93-4e11-8d85-96c945530d05' for resource 'RolePermission}'"
}
]
}
};
var expectedIds = ["7b4abf15-8a93-4e11-8d85-96c945530d05"];

var actualIds = FHIRUtils.getsIdFromOperationOutcome(operationOutcomes);

assert.deepEqual(actualIds, expectedIds, "IDs extracted correctly from operationOutcomes");
});

});

0 comments on commit d4f361d

Please sign in to comment.