-
Notifications
You must be signed in to change notification settings - Fork 28
CIRC-2364 Anonymize Single Request Post API #1632
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
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1e02717
Purpose/Overview:
yuntianhu 424fb3e
clean the request-anonymization.raml
yuntianhu 3424f23
add description to anonymize-single-request-response.json
yuntianhu 820bc28
Merge branch 'master' into CIRC-2364
yuntianhu b1eea2e
solve issues and increase test coverage
yuntianhu 9041de9
solve issues and increase test coverage
yuntianhu 8a45dfe
update RequestAnonymizationResource, RequestAnonymizationService, upd…
yuntianhu c7d91f9
tidy up the code and remove duplicates and unused codes
yuntianhu 6d69952
remove the unused method parameter. edit permission.
yuntianhu 77b8b8e
code corrections according to code review
yuntianhu 8da0432
delete extra space
yuntianhu 435fffd
cla: retry signature
yuntianhu 257cced
remove unused local variable.
yuntianhu 44af376
Refine request anonymization endpoint, permissions, and tests
yuntianhu 7fe7644
CIRC-2364-Anonymize Single Request Post API
yuntianhu 77c306e
CIRC-2364-Anonymize Single Request Post API
yuntianhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #%RAML 1.0 | ||
| title: Circulation | ||
| version: v0.1 | ||
| protocols: [ HTTP, HTTPS ] | ||
| baseUri: http://localhost:9130 | ||
|
|
||
| documentation: | ||
| - title: Request Anonymization API | ||
| content: <b>Request Anonymization API</b> | ||
|
|
||
| types: | ||
| anonymize-request-response: !include schema/anonymize-single-request-response.json | ||
| errors: !include raml-util/schemas/errors.schema | ||
|
|
||
| traits: | ||
| validate: !include raml-util/traits/validation.raml | ||
|
|
||
| /request-anonymization: | ||
| /{requestId}: | ||
| uriParameters: | ||
| requestId: | ||
| type: string | ||
| description: Request UUID to anonymize | ||
| example: 9fd9b9d8-1b1a-4a7a-9f54-5e7c5f5e0b2e | ||
| pattern: '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' | ||
| post: | ||
| is: [ validate ] | ||
| description: Anonymize a single closed request by its requestId. | ||
| responses: | ||
| 200: | ||
| description: The request has been successfully anonymized. | ||
| body: | ||
| application/json: | ||
| type: anonymize-request-response | ||
| example: | ||
| requestId: 9fd9b9d8-1b1a-4a7a-9f54-5e7c5f5e0b2e | ||
| anonymized: true | ||
|
|
||
| 404: | ||
| description: Request with the given requestId was not found. | ||
| body: | ||
| application/json: | ||
| type: errors | ||
| example: | ||
| errors: | ||
| - message: Request not found | ||
|
|
||
| 422: | ||
| description: Validation error — request is not closed or another rule prevents anonymization. | ||
| body: | ||
| application/json: | ||
| type: errors | ||
| example: | ||
| errors: | ||
| - message: requestNotClosed | ||
|
|
||
| 500: | ||
| description: Internal server error. | ||
| body: | ||
| application/json: | ||
| type: errors | ||
| example: | ||
| errors: | ||
| - message: Internal server error | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "description": "Single Request Anonymization Response", | ||
| "type": "object", | ||
| "properties": { | ||
| "requestId": { | ||
yuntianhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "type": "string", | ||
| "format": "UUID", | ||
| "description": "UUID of the request that was processed." | ||
| }, | ||
| "anonymized": { | ||
| "type": "boolean", | ||
| "description": "True if the request is anonymized after this call (PII removed or already absent)." | ||
| } | ||
| }, | ||
| "required": ["requestId", "anonymized"], | ||
| "additionalProperties": false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/folio/circulation/resources/RequestAnonymizationResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.folio.circulation.resources; | ||
|
|
||
| import io.vertx.core.json.JsonObject; | ||
| import org.folio.circulation.services.EventPublisher; | ||
| import org.folio.circulation.services.RequestAnonymizationService; | ||
| import org.folio.circulation.support.Clients; | ||
| import org.folio.circulation.support.RouteRegistration; | ||
| import org.folio.circulation.support.http.server.JsonHttpResponse; | ||
| import org.folio.circulation.support.http.server.WebContext; | ||
|
|
||
| import io.vertx.core.http.HttpClient; | ||
| import io.vertx.ext.web.Router; | ||
| import io.vertx.ext.web.RoutingContext; | ||
|
|
||
| public class RequestAnonymizationResource extends Resource { | ||
| public RequestAnonymizationResource(HttpClient client) { | ||
| super(client); | ||
| } | ||
|
|
||
| @Override | ||
| public void register(Router router) { | ||
| new RouteRegistration("/request-anonymization/:requestId", router) | ||
| .create(this::anonymizeRequest); | ||
| } | ||
|
|
||
| public void anonymizeRequest(RoutingContext routingContext) { | ||
yuntianhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final WebContext context = new WebContext(routingContext); | ||
| final Clients clients = Clients.create(context, client); | ||
|
|
||
| final String requestId = routingContext.request().getParam("requestId"); | ||
|
|
||
| final var eventPublisher = new EventPublisher(clients); | ||
| final var requestAnonymizationService = new RequestAnonymizationService(clients, eventPublisher); | ||
|
|
||
| requestAnonymizationService.anonymizeSingle(requestId) | ||
| .thenApply(r -> r.map(id -> | ||
| JsonHttpResponse.ok(new JsonObject() | ||
| .put("requestId", id) | ||
| .put("anonymized", true)) | ||
| )) | ||
| .thenAccept(context::writeResultToHttpResponse); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/org/folio/circulation/services/RequestAnonymizationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package org.folio.circulation.services; | ||
|
|
||
| import static org.folio.circulation.support.results.Result.failed; | ||
| import static org.folio.circulation.support.results.Result.succeeded; | ||
| import static org.folio.circulation.support.results.ResultBinding.flatMapResult; | ||
| import static org.folio.circulation.support.results.ResultBinding.mapResult; | ||
|
|
||
| import java.util.EnumSet; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.folio.circulation.domain.Request; | ||
| import org.folio.circulation.domain.RequestFulfillmentPreference; | ||
| import org.folio.circulation.domain.RequestStatus; | ||
| import org.folio.circulation.support.Clients; | ||
| import org.folio.circulation.support.ValidationErrorFailure; | ||
| import org.folio.circulation.support.http.server.ValidationError; | ||
| import org.folio.circulation.support.results.Result; | ||
| import org.folio.circulation.infrastructure.storage.inventory.ItemRepository; | ||
| import org.folio.circulation.infrastructure.storage.loans.LoanRepository; | ||
| import org.folio.circulation.infrastructure.storage.users.UserRepository; | ||
| import org.folio.circulation.infrastructure.storage.requests.RequestRepository; | ||
| import org.folio.util.UuidUtil; | ||
| import io.vertx.core.json.JsonObject; | ||
|
|
||
| public class RequestAnonymizationService { | ||
| private static final Set<RequestStatus> ALLOWED_STATUSES = EnumSet.of( | ||
| RequestStatus.CLOSED_FILLED, | ||
| RequestStatus.CLOSED_CANCELLED, | ||
| RequestStatus.CLOSED_PICKUP_EXPIRED, | ||
| RequestStatus.CLOSED_UNFILLED | ||
| ); | ||
|
|
||
| private static final Set<RequestStatus> OPEN_STATUSES = EnumSet.of( | ||
| RequestStatus.OPEN_AWAITING_PICKUP, | ||
| RequestStatus.OPEN_IN_TRANSIT, | ||
| RequestStatus.OPEN_NOT_YET_FILLED | ||
| ); | ||
|
|
||
| private final RequestRepository requestRepository; | ||
| private final EventPublisher eventPublisher; | ||
|
|
||
| public RequestAnonymizationService(Clients clients, EventPublisher eventPublisher) { | ||
| ItemRepository itemRepository = new ItemRepository(clients); | ||
| UserRepository userRepository = new UserRepository(clients); | ||
| LoanRepository loanRepository = new LoanRepository(clients, itemRepository, userRepository); | ||
|
|
||
| this.requestRepository = RequestRepository.using(clients, itemRepository, userRepository, loanRepository); | ||
|
|
||
| this.eventPublisher = eventPublisher; | ||
| } | ||
|
|
||
| public RequestAnonymizationService(RequestRepository requestRepository, | ||
| EventPublisher eventPublisher) { | ||
|
|
||
| this.requestRepository = requestRepository; | ||
| this.eventPublisher = eventPublisher; | ||
| } | ||
|
|
||
| public CompletableFuture<Result<String>> anonymizeSingle(String requestId) { | ||
| if (!UuidUtil.isUuid(requestId)) { | ||
| return CompletableFuture.completedFuture( | ||
| ValidationErrorFailure.failedValidation("invalidRequestId", "requestId", requestId) | ||
| ); | ||
| } | ||
|
|
||
| return fetchRequest(requestId) | ||
| .thenApply(flatMapResult(req -> validateStatus(req, requestId))) | ||
| .thenApply(mapResult(this::scrubPii)) | ||
| .thenCompose(r -> r.after(requestRepository::update)) | ||
| .thenCompose(r -> r.after(this::publishLog)) | ||
| .thenApply(mapResult(updated -> requestId)); | ||
| } | ||
|
|
||
| private CompletableFuture<Result<Request>> fetchRequest(String requestId) { | ||
| return requestRepository.getById(requestId); | ||
| } | ||
|
|
||
| private Result<Request> validateStatus(Request request, String id) { | ||
| final RequestStatus status = request.getStatus(); | ||
|
|
||
| if (ALLOWED_STATUSES.contains(status)) { | ||
| return succeeded(request); | ||
| } | ||
| if (OPEN_STATUSES.contains(status)) { | ||
| return failed(new ValidationErrorFailure((new ValidationError("requestNotClosed", "requestId", id)))); | ||
| } | ||
| return failed(new ValidationErrorFailure((new ValidationError("requestNotEligibleForAnonymization", "requestId", id)))); | ||
| } | ||
|
|
||
| private Request scrubPii(Request req) { | ||
| final JsonObject rep = req.asJson(); | ||
|
|
||
| final boolean hadRequester = rep.containsKey("requester") || rep.containsKey("requesterId"); | ||
| final boolean hadProxy = rep.containsKey("proxy") || rep.containsKey("proxyUserId"); | ||
| final boolean isDelivery = req.getfulfillmentPreference() == RequestFulfillmentPreference.DELIVERY; | ||
| final boolean hadDelivery = isDelivery && (rep.containsKey("deliveryAddress") || rep.containsKey("deliveryAddressTypeId")); | ||
|
|
||
| if (!hadRequester && !hadProxy && (!isDelivery || !hadDelivery)) { | ||
| return req; | ||
| } | ||
|
|
||
| rep.putNull("requesterId"); | ||
| rep.putNull("proxyUserId"); | ||
| rep.remove("requester"); | ||
| rep.remove("proxy"); | ||
|
|
||
| if (isDelivery) { | ||
| rep.remove("deliveryAddress"); | ||
| rep.remove("deliveryAddressTypeId"); | ||
| } | ||
|
|
||
| return Request.from(rep); | ||
| } | ||
|
|
||
| private CompletableFuture<Result<Void>> publishLog(Request req) { | ||
| return eventPublisher.publishRequestAnonymizedLog(req); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.