Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: align fetching a single vs multiple enrollments [DHIS2-18791] #19705

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
import org.hisp.dhis.tracker.acl.TrackerOwnershipManager;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.tracker.export.event.EventOperationParams;
import org.hisp.dhis.tracker.export.event.EventParams;
import org.hisp.dhis.tracker.export.event.EventService;
import org.hisp.dhis.user.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -63,6 +66,8 @@
class DefaultEnrollmentService implements EnrollmentService {
private final EnrollmentStore enrollmentStore;

private final EventService eventService;

private final TrackerOwnershipManager trackerOwnershipAccessManager;

private final TrackedEntityAttributeService trackedEntityAttributeService;
Expand All @@ -71,29 +76,68 @@ class DefaultEnrollmentService implements EnrollmentService {

private final EnrollmentOperationParamsMapper paramsMapper;

@Nonnull
@Override
public Enrollment getEnrollment(@Nonnull UID uid) throws ForbiddenException, NotFoundException {
return getEnrollment(uid, EnrollmentParams.FALSE, false);
}

@Nonnull
@Override
public Enrollment getEnrollment(
@Nonnull UID uid, @Nonnull EnrollmentParams params, boolean includeDeleted)
throws NotFoundException, ForbiddenException {
UserDetails currentUser = getCurrentUserDetails();
Enrollment enrollment = enrollmentStore.getByUid(uid.getValue());
Page<Enrollment> enrollments;
try {
EnrollmentOperationParams operationParams =
EnrollmentOperationParams.builder()
.enrollments(Set.of(uid))
.enrollmentParams(params)
.includeDeleted(includeDeleted)
.build();
enrollments = getEnrollments(operationParams, new PageParams(1, 1, false));
} catch (BadRequestException e) {
throw new IllegalArgumentException(
"this must be a bug in how the EnrollmentOperationParams are built");
}

if (enrollment == null) {
if (enrollments.getItems().isEmpty()) {
throw new NotFoundException(Enrollment.class, uid);
}

List<String> errors = trackerAccessManager.canRead(currentUser, enrollment, false);
return enrollments.getItems().get(0);
}

if (!errors.isEmpty()) {
throw new ForbiddenException(errors.toString());
@Override
public RelationshipItem getEnrollmentInRelationshipItem(
@Nonnull UID uid, boolean includeDeleted) {
Enrollment enrollment;
try {
enrollment = getEnrollment(uid);
} catch (NotFoundException | ForbiddenException e) {
// enrollments are not shown in relationships if the user has no access to them
return null;
}

return getEnrollment(enrollment, params, includeDeleted, currentUser);
RelationshipItem relationshipItem = new RelationshipItem();
relationshipItem.setEnrollment(enrollment);
return relationshipItem;
}

private Set<Event> getEvents(
Enrollment enrollment, EventParams eventParams, boolean includeDeleted) {
EventOperationParams eventOperationParams =
EventOperationParams.builder()
.enrollments(Set.of(UID.of(enrollment)))
.eventParams(eventParams)
.includeDeleted(includeDeleted)
.build();
try {
return Set.copyOf(eventService.getEvents(eventOperationParams));
} catch (BadRequestException | ForbiddenException e) {
throw new IllegalArgumentException(
"this must be a bug in how the EventOperationParams are built");
}
}

private Enrollment getEnrollment(
Expand Down Expand Up @@ -130,7 +174,9 @@ private Enrollment getEnrollment(
result.setDeleted(enrollment.isDeleted());
result.setNotes(enrollment.getNotes());
if (params.isIncludeEvents()) {
result.setEvents(getEvents(user, enrollment, includeDeleted));
result.setEvents(
getEvents(
enrollment, params.getEnrollmentEventsParams().getEventParams(), includeDeleted));
}
if (params.isIncludeRelationships()) {
result.setRelationshipItems(getRelationshipItems(user, enrollment, includeDeleted));
Expand All @@ -144,40 +190,6 @@ private Enrollment getEnrollment(
return result;
}

@Override
public RelationshipItem getEnrollmentInRelationshipItem(
@Nonnull UID uid, @Nonnull EnrollmentParams params, boolean includeDeleted)
throws NotFoundException {

RelationshipItem relationshipItem = new RelationshipItem();
Enrollment enrollment = enrollmentStore.getByUid(uid.getValue());

if (enrollment == null) {
throw new NotFoundException(Enrollment.class, uid);
}

UserDetails currentUser = getCurrentUserDetails();
List<String> errors = trackerAccessManager.canRead(currentUser, enrollment, false);
if (!errors.isEmpty()) {
return null;
}

relationshipItem.setEnrollment(getEnrollment(enrollment, params, includeDeleted, currentUser));
return relationshipItem;
}

private Set<Event> getEvents(UserDetails user, Enrollment enrollment, boolean includeDeleted) {
Set<Event> events = new HashSet<>();

for (Event event : enrollment.getEvents()) {
if ((includeDeleted || !event.isDeleted())
&& trackerAccessManager.canRead(user, event, true).isEmpty()) {
events.add(event);
}
}
return events;
}

private Set<RelationshipItem> getRelationshipItems(
UserDetails user, Enrollment enrollment, boolean includeDeleted) {
Set<RelationshipItem> relationshipItems = new HashSet<>();
Expand Down Expand Up @@ -210,24 +222,28 @@ private Set<TrackedEntityAttributeValue> getTrackedEntityAttributeValues(
return attributeValues;
}

@Nonnull
@Override
public List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenException {
List<Enrollment> enrollments = enrollmentStore.getByUid(UID.toValueList(uids));
UserDetails user = getCurrentUserDetails();
List<String> errors =
enrollments.stream()
.flatMap(e -> trackerAccessManager.canRead(user, e, false).stream())
.toList();

if (!errors.isEmpty()) {
throw new ForbiddenException(errors.toString());
EnrollmentQueryParams queryParams;
try {
queryParams =
paramsMapper.map(
EnrollmentOperationParams.builder().enrollments(uids).build(),
getCurrentUserDetails());
} catch (BadRequestException e) {
throw new IllegalArgumentException(
"this must be a bug in how the EventOperationParams are built");
}

return enrollments.stream()
.map(e -> getEnrollment(e, EnrollmentParams.FALSE, false, user))
.toList();
return getEnrollments(
new ArrayList<>(enrollmentStore.getEnrollments(queryParams)),
EnrollmentParams.FALSE,
false,
queryParams.getOrganisationUnitMode());
}

@Nonnull
@Override
public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params)
throws ForbiddenException, BadRequestException {
Expand All @@ -240,6 +256,7 @@ public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params
queryParams.getOrganisationUnitMode());
}

@Nonnull
@Override
public Page<Enrollment> getEnrollments(
@Nonnull EnrollmentOperationParams params, PageParams pageParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public class EnrollmentOperationParams {
@Builder.Default private final Set<UID> orgUnits = new HashSet<>();

/** Selection mode for the specified organisation units. */
private final OrganisationUnitSelectionMode orgUnitMode;
@Builder.Default
private final OrganisationUnitSelectionMode orgUnitMode =
OrganisationUnitSelectionMode.ACCESSIBLE;

/** Enrollments must be enrolled into this program. */
private final UID program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
import org.hisp.dhis.tracker.export.PageParams;

public interface EnrollmentService {
@Nonnull
Enrollment getEnrollment(UID uid) throws ForbiddenException, NotFoundException;

@Nonnull
Enrollment getEnrollment(UID uid, EnrollmentParams params, boolean includeDeleted)
throws NotFoundException, ForbiddenException;

RelationshipItem getEnrollmentInRelationshipItem(
UID uid, EnrollmentParams params, boolean includeDeleted) throws NotFoundException;
RelationshipItem getEnrollmentInRelationshipItem(UID uid, boolean includeDeleted)
Dismissed Show dismissed Hide dismissed
throws NotFoundException;

/** Get all enrollments matching given params. */
@Nonnull
Expand All @@ -62,6 +64,7 @@
* Get event matching given {@code UID} under the privileges the user in the context. This method
* does not get the events relationships.
*/
@Nonnull
List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,15 @@ private Event getEvent(
return event;
}

@Nonnull
@Override
public List<Event> getEvents(@Nonnull EventOperationParams operationParams)
throws BadRequestException, ForbiddenException {
EventQueryParams queryParams = paramsMapper.map(operationParams, getCurrentUserDetails());
return eventStore.getEvents(queryParams);
}

@Nonnull
@Override
public Page<Event> getEvents(
@Nonnull EventOperationParams operationParams, @Nonnull PageParams pageParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class EventOperationParams {

private UID orgUnit;

private OrganisationUnitSelectionMode orgUnitMode;
@Builder.Default
private OrganisationUnitSelectionMode orgUnitMode = OrganisationUnitSelectionMode.ACCESSIBLE;

private AssignedUserSelectionMode assignedUserMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import org.hisp.dhis.tracker.export.FileResourceStream;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.tracker.export.enrollment.EnrollmentParams;
import org.hisp.dhis.tracker.export.enrollment.EnrollmentService;
import org.hisp.dhis.tracker.export.event.EventParams;
import org.hisp.dhis.tracker.export.event.EventService;
Expand Down Expand Up @@ -543,9 +542,7 @@ private RelationshipItem getRelationshipItem(
} else if (item.getEnrollment() != null) {
result =
enrollmentService.getEnrollmentInRelationshipItem(
UID.of(item.getEnrollment()),
EnrollmentParams.TRUE.withIncludeRelationships(false),
includeDeleted);
UID.of(item.getEnrollment()), includeDeleted);
} else if (item.getEvent() != null) {
result =
eventService.getEventInRelationshipItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ void getRelationshipsByEnrollmentWithNotes() {

@Test
void getRelationshipsByEnrollmentNotFound() {
switchContextToUser(user);
assertStartsWith(
"Enrollment with id Hq3Kc6HK4OZ",
GET("/tracker/relationships?enrollment=Hq3Kc6HK4OZ")
Expand Down
Loading