Skip to content

Commit

Permalink
chore: align /tracker/relationships?trackedEntity= attributes
Browse files Browse the repository at this point in the history
with #17572
  • Loading branch information
teleivo committed Jan 15, 2025
1 parent 8cc485b commit fdfff2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ public TrackedEntity getTrackedEntity(
}
}

UserDetails user = getCurrentUserDetails();
TrackedEntity trackedEntity = getTrackedEntity(trackedEntityUid, program, params, user, false);
return trackedEntity;
return getTrackedEntity(trackedEntityUid, program, params, getCurrentUserDetails());
}

/**
Expand All @@ -249,11 +247,7 @@ public TrackedEntity getTrackedEntity(
* @throws ForbiddenException if TE owner is not in user's scope or not enough sharing access
*/
private TrackedEntity getTrackedEntity(
UID uid,
Program program,
TrackedEntityParams params,
UserDetails user,
boolean includeDeleted)
UID uid, Program program, TrackedEntityParams params, UserDetails user)
throws NotFoundException, ForbiddenException {
TrackedEntity trackedEntity = trackedEntityStore.getByUid(uid.getValue());
if (trackedEntity == null) {
Expand All @@ -280,32 +274,16 @@ private TrackedEntity getTrackedEntity(
}
}

TrackedEntity result = new TrackedEntity();
result.setId(trackedEntity.getId());
result.setUid(trackedEntity.getUid());
result.setOrganisationUnit(trackedEntity.getOrganisationUnit());
result.setTrackedEntityType(trackedEntity.getTrackedEntityType());
result.setCreated(trackedEntity.getCreated());
result.setCreatedAtClient(trackedEntity.getCreatedAtClient());
result.setLastUpdated(trackedEntity.getLastUpdated());
result.setLastUpdatedAtClient(trackedEntity.getLastUpdatedAtClient());
result.setInactive(trackedEntity.isInactive());
result.setGeometry(trackedEntity.getGeometry());
result.setDeleted(trackedEntity.isDeleted());
result.setPotentialDuplicate(trackedEntity.isPotentialDuplicate());
result.setStoredBy(trackedEntity.getStoredBy());
result.setCreatedByUserInfo(trackedEntity.getCreatedByUserInfo());
result.setLastUpdatedByUserInfo(trackedEntity.getLastUpdatedByUserInfo());
result.setGeometry(trackedEntity.getGeometry());
if (params.isIncludeEnrollments()) {
result.setEnrollments(getEnrollments(trackedEntity, user, includeDeleted, program));
trackedEntity.setEnrollments(getEnrollments(trackedEntity, user, false, program));
}
setRelationshipItems(result, trackedEntity, params, includeDeleted);
setRelationshipItems(trackedEntity, trackedEntity, params, false);
if (params.isIncludeProgramOwners()) {
result.setProgramOwners(getTrackedEntityProgramOwners(trackedEntity, program));
trackedEntity.setProgramOwners(getTrackedEntityProgramOwners(trackedEntity, program));
}
result.setTrackedEntityAttributeValues(getTrackedEntityAttributeValues(trackedEntity, program));
return result;
trackedEntity.setTrackedEntityAttributeValues(
getTrackedEntityAttributeValues(trackedEntity, program));
return trackedEntity;
}

private Set<Enrollment> getEnrollments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.webapi.controller.tracker.export.relationship;

import static org.hisp.dhis.test.utils.Assertions.assertIsEmpty;
import static org.hisp.dhis.test.utils.Assertions.assertStartsWith;
import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertContainsAll;
import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertEnrollmentWithinRelationship;
Expand Down Expand Up @@ -652,7 +653,11 @@ void getRelationshipsByTrackedEntityWithEnrollments() {
}

@Test
void getRelationshipsByTrackedEntityAndEnrollmentWithAttributes() {
void getRelationshipsByTrackedEntityAndEnrollmentWithAttributesIsEmpty() {
// Tracked entity attribute values are owned by the tracked entity and only mapped onto the
// enrollment on export. Program tracked entity attributes are only returned by the underlying
// TE service if a program is
// provided which is not possible on the relationship endpoint.
TrackedEntity to = trackedEntity(orgUnit);
to.setTrackedEntityAttributeValues(
Set.of(attributeValue(tea, to, "12"), attributeValue(tea2, to, "24")));
Expand Down Expand Up @@ -689,13 +694,13 @@ void getRelationshipsByTrackedEntityAndEnrollmentWithAttributes() {

JsonList<JsonAttribute> enrollmentAttr =
relationships.get(0).getFrom().getEnrollment().getAttributes();
assertContainsAll(List.of(tea2.getUid()), enrollmentAttr, JsonAttribute::getAttribute);
assertContainsAll(List.of("24"), enrollmentAttr, JsonAttribute::getValue);
assertIsEmpty(
enrollmentAttr.toList(JsonAttribute::getAttribute),
"program attributes should not be returned as no program can be provided");
JsonList<JsonAttribute> teAttributes =
relationships.get(0).getTo().getTrackedEntity().getAttributes();
assertContainsAll(
List.of(tea.getUid(), tea2.getUid()), teAttributes, JsonAttribute::getAttribute);
assertContainsAll(List.of("12", "24"), teAttributes, JsonAttribute::getValue);
assertContainsAll(List.of(tea.getUid()), teAttributes, JsonAttribute::getAttribute);
assertContainsAll(List.of("12"), teAttributes, JsonAttribute::getValue);
}

@Test
Expand Down

0 comments on commit fdfff2b

Please sign in to comment.