diff --git a/ramls/edge-patron.raml b/ramls/edge-patron.raml index 4dcc99d..f08bbc5 100644 --- a/ramls/edge-patron.raml +++ b/ramls/edge-patron.raml @@ -1036,7 +1036,7 @@ types: example: internal server error, contact administrator /registration-status: get: - description: Get the patron details by email ID + description: Get the patron details by email ID or externalSystemId queryParameters: apikey: description: "API Key" @@ -1044,7 +1044,10 @@ types: emailId: description: The email ID of the patron. type: string - required: true + externalSystemId: + description: The UUID of a staging user + type: string + pattern: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$ responses: 200: description: patron information retrieved successfully diff --git a/src/main/java/org/folio/edge/patron/PatronHandler.java b/src/main/java/org/folio/edge/patron/PatronHandler.java index 26631ea..d2494c6 100644 --- a/src/main/java/org/folio/edge/patron/PatronHandler.java +++ b/src/main/java/org/folio/edge/patron/PatronHandler.java @@ -308,24 +308,32 @@ public void handleSecureGetAllowedServicePointsForItem(RoutingContext ctx) { public void handleGetPatronRegistrationStatus(RoutingContext ctx) { logger.debug("handleGetPatronRegistrationStatus:: Fetching patron registration"); + String emailId = ctx.request().getParam(PARAM_EMAIL_ID); - if(StringUtils.isNullOrEmpty(emailId)) { - logger.warn("handleGetPatronRegistrationStatus:: Missing or empty emailId"); + String externalSystemId = ctx.request().getParam(PARAM_EXTERNAL_SYSTEM_ID); + + if (StringUtils.isNullOrEmpty(emailId) && StringUtils.isNullOrEmpty(externalSystemId)) { + logger.warn("handleGetPatronRegistrationStatus:: Missing or empty emailId and externalSystemId"); ctx.response() .setStatusCode(400) .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) - .end(getErrorMsg("EMAIL_NOT_PROVIDED", "emailId is missing in the request")); + .end(getErrorMsg("IDENTIFIERS_NOT_PROVIDED", "Either emailId or externalSystemId must be provided in the request")); return; } + super.handleCommon(ctx, new String[]{}, new String[]{}, (client, params) -> { String alternateTenantId = ctx.request().getParam("alternateTenantId", client.tenant); final PatronOkapiClient patronClient = new PatronOkapiClient(client, alternateTenantId); - patronClient.getPatronRegistrationStatus(emailId, + + patronClient.getPatronRegistrationStatus( + emailId != null ? emailId : externalSystemId, resp -> handleRegistrationStatusResponse(ctx, resp), - t -> handleProxyException(ctx, t)); + t -> handleProxyException(ctx, t) + ); }); } + @Override protected void invalidApiKey(RoutingContext ctx, String msg) { accessDenied(ctx, msg); diff --git a/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java b/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java index fef94e3..028cf8d 100644 --- a/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java +++ b/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java @@ -251,11 +251,11 @@ public void placeInstanceHold(String patronId, String instanceId, String request exceptionHandler); } - public void getPatronRegistrationStatus(String emailId, - Handler> responseHandler, Handler exceptionHandler) { + public void getPatronRegistrationStatus(String identifier, + Handler> responseHandler, Handler exceptionHandler) { get(format("%s/patron/registration-status/%s", okapiURL, - emailId), tenant, null, responseHandler, exceptionHandler); + identifier), tenant, null, responseHandler, exceptionHandler); } private Hold createCancellationHoldRequest(JsonObject cancellationRequest, JsonObject baseRequest, String patronId) { diff --git a/src/test/java/org/folio/edge/patron/MainVerticleTest.java b/src/test/java/org/folio/edge/patron/MainVerticleTest.java index 1346c32..c2d8334 100644 --- a/src/test/java/org/folio/edge/patron/MainVerticleTest.java +++ b/src/test/java/org/folio/edge/patron/MainVerticleTest.java @@ -393,8 +393,8 @@ public void testGetPatronRegistrationStatusWithoutEmail(TestContext context) { .response(); var jsonResponse = new JsonObject(response.body().asString()); - assertEquals("EMAIL_NOT_PROVIDED", jsonResponse.getString("code")); - assertEquals("emailId is missing in the request", jsonResponse.getString("errorMessage")); + assertEquals("IDENTIFIERS_NOT_PROVIDED", jsonResponse.getString("code")); + assertEquals("Either emailId or externalSystemId must be provided in the request", jsonResponse.getString("errorMessage")); response = RestAssured .get(String.format("/patron/registration-status?emailId=%s&apikey=%s", "", apiKey)) @@ -405,8 +405,8 @@ public void testGetPatronRegistrationStatusWithoutEmail(TestContext context) { .response(); jsonResponse = new JsonObject(response.body().asString()); - assertEquals("EMAIL_NOT_PROVIDED", jsonResponse.getString("code")); - assertEquals("emailId is missing in the request", jsonResponse.getString("errorMessage")); + assertEquals("IDENTIFIERS_NOT_PROVIDED", jsonResponse.getString("code")); + assertEquals("Either emailId or externalSystemId must be provided in the request", jsonResponse.getString("errorMessage")); } @Test @@ -426,6 +426,23 @@ public void testGetPatronRegistrationStatusWithActiveEmail(TestContext context) assertEquals(expected, actual); } + @Test + public void testGetPatronRegistrationStatusWithExternalSystemId(TestContext context) { + + final var response = RestAssured + .get(String.format("/patron/registration-status?externalSystemId=%s&apikey=%s", "9eb67301-6f6e-468f-9b1a-6134dc39a699", apiKey)) + .then() + .statusCode(200) + .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .extract() + .response(); + + var expected = new JsonObject(readMockFile( + "/user_active.json")); + var actual = new JsonObject(response.body().asString()); + assertEquals(expected, actual); + } + @Test public void testGetPatronRegistrationStatusWithInvalidEmail() { @@ -442,6 +459,23 @@ public void testGetPatronRegistrationStatusWithInvalidEmail() { assertEquals("User does not exist", jsonResponse.getString("errorMessage")); } + @Test + public void testGetPatronRegistrationStatusWithInvalidExternalSystemId() { + + final var response = RestAssured + .get(String.format("/patron/registration-status?externalSystemId=%s&apikey=%s", "9eb67301-6f6e-468f-9b1a-6134dc39a700", apiKey)) + .then() + .statusCode(404) + .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .extract() + .response(); + + var jsonResponse = new JsonObject(response.body().asString()); + assertEquals("USER_NOT_FOUND", jsonResponse.getString("code")); + assertEquals("User does not exist", jsonResponse.getString("errorMessage")); + } + + @Test public void testGetPatronRegistrationStatusWithMultipleUserEmail() { diff --git a/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java b/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java index b4004f8..d3943ff 100644 --- a/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java +++ b/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java @@ -5,18 +5,7 @@ import static org.folio.edge.core.Constants.DAY_IN_MILLIS; import static org.folio.edge.core.Constants.TEXT_PLAIN; import static org.folio.edge.core.Constants.X_OKAPI_TOKEN; -import static org.folio.edge.patron.Constants.PARAM_EMAIL_ID; -import static org.folio.edge.patron.Constants.PARAM_HOLD_ID; -import static org.folio.edge.patron.Constants.PARAM_INCLUDE_CHARGES; -import static org.folio.edge.patron.Constants.PARAM_INCLUDE_HOLDS; -import static org.folio.edge.patron.Constants.PARAM_INCLUDE_LOANS; -import static org.folio.edge.patron.Constants.PARAM_INSTANCE_ID; -import static org.folio.edge.patron.Constants.PARAM_ITEM_ID; -import static org.folio.edge.patron.Constants.PARAM_LIMIT; -import static org.folio.edge.patron.Constants.PARAM_OFFSET; -import static org.folio.edge.patron.Constants.PARAM_PATRON_ID; -import static org.folio.edge.patron.Constants.PARAM_REQUEST_ID; -import static org.folio.edge.patron.Constants.PARAM_SORT_BY; +import static org.folio.edge.patron.Constants.*; import com.fasterxml.jackson.core.JsonProcessingException; import io.vertx.core.http.HttpHeaders; @@ -277,22 +266,34 @@ public void getRegistrationStatusHandler(RoutingContext ctx) { .setStatusCode(403) .putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN) .end("Access requires permission: patron.account.get"); - } else if(emailId.equals("active@folio.com")) { + } else if(emailId!=null && emailId.equals("active@folio.com")) { ctx.response() .setStatusCode(200) .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) .end(readMockFile("/user_active.json")); - } else if(emailId.equals("multipleuser@folio.com")) { + } else if(emailId!=null && emailId.equals("9eb67301-6f6e-468f-9b1a-6134dc39a699")) { + ctx.response() + .setStatusCode(200) + .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .end(readMockFile("/user_active.json")); + } else if(emailId!=null && emailId.equals("multipleuser@folio.com")) { ctx.response() .setStatusCode(400) .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) .end(readMockFile("/multiple_user_error.json")); - } else if(emailId.equals("usernotfound@folio.com")) { + } else if(emailId!=null && emailId.equals("usernotfound@folio.com")) { + ctx.response() + .setStatusCode(404) + .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .end(readMockFile("/user_not_found_error.json")); + } + else if(emailId!=null && emailId.equals("9eb67301-6f6e-468f-9b1a-6134dc39a700")) { ctx.response() .setStatusCode(404) .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) .end(readMockFile("/user_not_found_error.json")); - } else if(emailId.equals("invalid@folio.com")) { + } + else if(emailId!=null && emailId.equals("invalid@folio.com")) { ctx.response() .setStatusCode(404) .putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN) diff --git a/src/test/resources/user_active.json b/src/test/resources/user_active.json index cc3e1ae..753e657 100644 --- a/src/test/resources/user_active.json +++ b/src/test/resources/user_active.json @@ -1,6 +1,6 @@ { "id": "cacc29d8-cade-4312-a5f2-4eeac55d8697", - "externalSystemId": "active@folio.com", + "externalSystemId": "9eb67301-6f6e-468f-9b1a-6134dc39a699", "active": true, "type": "patron", "patronGroup": "63f8065f-df84-4e76-a36b-3ba32dbdc9e5",