Skip to content

Commit

Permalink
CIRC-2100 Avoiding extra user API call
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh-kalyanasundaram committed Jul 25, 2024
1 parent 791bcf4 commit 574a926
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public CompletableFuture<Result<MultipleRecords<Request>>> findPrintEventDetails

private CompletableFuture<Result<MultipleRecords<Request>>> fetchAndMapPrintEventDetails(
MultipleRecords<Request> multipleRequests) {
log.debug("fetchAndMapPrintEventDetails:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
var requestIds = multipleRequests.toKeys(Request::getId);
if (requestIds.isEmpty()) {
return completedFuture(succeeded(multipleRequests));
Expand Down Expand Up @@ -88,10 +90,12 @@ private CompletableFuture<Boolean> validatePrintEventFeatureFlag() {

private Result<MultipleRecords<Request>> mapPrintEventDetailsToRequest(
MultipleRecords<PrintEventDetail> printEventDetails, MultipleRecords<Request> requests) {
log.info("mapPrintEventDetailsToRequest:: Mapping print event details {} with requests {}",
log.debug("mapPrintEventDetailsToRequest:: Mapping print event details {} with requests {}",
() -> multipleRecordsAsString(printEventDetails), () -> multipleRecordsAsString(requests));
Map<String, PrintEventDetail> printEventDetailMap = printEventDetails.toMap(PrintEventDetail::getRequestId);
return of(() -> requests.mapRecords(request -> request.withPrintEventDetail(printEventDetailMap.getOrDefault(request.getId(), null))));
return of(() ->
requests.mapRecords(request -> request
.withPrintEventDetail(printEventDetailMap.getOrDefault(request.getId(), null))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public static RequestRepository using(
public CompletableFuture<Result<MultipleRecords<Request>>> findBy(String query) {
return requestsStorageClient.getManyWithRawQueryStringParameters(query)
.thenApply(flatMapResult(this::mapResponseToRequests))
.thenCompose(r -> r.after(this::fetchAdditionalFields))
.thenCompose(r -> r.after(this::fetchPrintEventDetails));
.thenCompose(r -> r.after(this::fetchPrintEventDetails))
.thenCompose(r -> r.after(this::fetchAdditionalFields));
}

CompletableFuture<Result<MultipleRecords<Request>>> findBy(CqlQuery query, PageLimit pageLimit) {
Expand All @@ -151,9 +151,10 @@ private CompletableFuture<Result<MultipleRecords<Request>>> fetchAdditionalField

private CompletableFuture<Result<MultipleRecords<Request>>> fetchPrintEventDetails(
MultipleRecords<Request> requestRecords) {
log.debug("fetchPrintEventDetails:: Fetching print event details for requestRecords: {}",
()-> multipleRecordsAsString(requestRecords));
return ofAsync(() -> requestRecords)
.thenComposeAsync(result -> result.after(printEventsRepository::findPrintEventDetails))
.thenComposeAsync(result -> result.after(userRepository::findUsersForPrintEvents));
.thenComposeAsync(result -> result.after(printEventsRepository::findPrintEventDetails));
}

CompletableFuture<Result<MultipleRecords<Request>>> findByWithoutItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,6 @@ public CompletableFuture<Result<MultipleRecords<Request>>> findUsersForRequests(
matchUsersToRequests(request, multipleUsers)))));
}

public CompletableFuture<Result<MultipleRecords<Request>>> findUsersForPrintEvents(
MultipleRecords<Request> multipleRequests) {

log.debug("findUsersForRequests:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
return findUsersForPrintEvents(multipleRequests.getRecords())
.thenApply(multipleUsersResult -> multipleUsersResult.next(
multipleUsers -> of(() ->
multipleRequests.mapRecords(request ->
matchUsersToPrintEvents(request, multipleUsers)))));
}

public CompletableFuture<Result<MultipleRecords<User>>> findUsersByRequests(
Collection<Request> requests) {

Expand All @@ -261,22 +249,6 @@ public CompletableFuture<Result<MultipleRecords<User>>> findUsersByRequests(
.distinct()
.collect(Collectors.toList());

return fetchUsers(usersToFetch);
}

public CompletableFuture<Result<MultipleRecords<User>>> findUsersForPrintEvents(
Collection<Request> requests) {
log.debug("findUsersForPrintEvents:: parameters requests: {}", () -> collectionAsString(requests));
final List<String> usersToFetch = requests.stream()
.filter(request -> request.getPrintEventDetail() != null && request.getPrintEventDetail().getUserId() != null)
.map(request -> request.getPrintEventDetail().getUserId())
.distinct()
.collect(Collectors.toList());

return fetchUsers(usersToFetch);
}

private CompletableFuture<Result<MultipleRecords<User>>> fetchUsers(List<String> usersToFetch) {
if (usersToFetch.isEmpty()) {
log.info("findUsersByRequests:: no users to fetch");
return completedFuture(succeeded(MultipleRecords.empty()));
Expand All @@ -300,6 +272,10 @@ private ArrayList<String> getUsersFromRequest(Request request) {
usersToFetch.add(request.getProxyUserId());
}

if (request.getPrintEventDetail() != null && request.getPrintEventDetail().getUserId() != null) {
usersToFetch.add(request.getPrintEventDetail().getUserId());
}

return usersToFetch;
}

Expand All @@ -311,16 +287,7 @@ private Request matchUsersToRequests(

return request
.withRequester(userMap.getOrDefault(request.getUserId(), null))
.withProxy(userMap.getOrDefault(request.getProxyUserId(), null));
}

private Request matchUsersToPrintEvents(
Request request,
MultipleRecords<User> users) {

final Map<String, User> userMap = users.toMap(User::getId);

return request
.withProxy(userMap.getOrDefault(request.getProxyUserId(), null))
.withPrintEventDetail(mapUserToPrintEventDetails(request, userMap));
}

Expand Down

0 comments on commit 574a926

Please sign in to comment.