From 9d755bfc2fe0da858270642f0a0231fa64d22f84 Mon Sep 17 00:00:00 2001 From: SinghAdes <90304030+SinghAdes@users.noreply.github.com> Date: Mon, 10 Jun 2024 19:03:32 +0530 Subject: [PATCH] [EDGEPATRON-132]-Added GET edge api for LOC patron (#114) * [EDGEPATRON-132]-Added GET edge api for LOC patron * [EDGEPATRON-132]-Added GET edge api for LOC patron --- ramls/edge-patron.raml | 46 +++++++++++++++++++ .../java/org/folio/edge/patron/Constants.java | 1 + .../org/folio/edge/patron/MainVerticle.java | 3 ++ .../org/folio/edge/patron/PatronHandler.java | 12 +++++ .../edge/patron/utils/PatronOkapiClient.java | 11 +++++ .../folio/edge/patron/MainVerticleTest.java | 11 +++++ .../edge/patron/utils/PatronMockOkapi.java | 19 ++++++++ 7 files changed, 103 insertions(+) diff --git a/ramls/edge-patron.raml b/ramls/edge-patron.raml index ba860e7..a965adb 100644 --- a/ramls/edge-patron.raml +++ b/ramls/edge-patron.raml @@ -77,6 +77,52 @@ types: body: text/plain: example: internal server error, contact administrator + /by-email/{emailId}: + displayName: Get Accounts By email + description: Service endpoints that manage accounts by an existing email + uriParameters: + emailId: + description: The email of external patron + type: string + get: + description: Return account details for the specified external patron email + queryParameters: + apikey: + description: "API Key" + type: string + responses: + 200: + description: Returns the external patron account info + body: + application/json: + type: external_patron + example: !include examples/external_patron.json + 400: + description: Bad request + body: + text/plain: + example: unable to process request -- constraint violation + 401: + description: Not authorized to perform requested action + body: + text/plain: + example: unable to get account -- unauthorized + 404: + description: Item with a given EMAIL not found + body: + text/plain: + example: account not found + 403: + description: Access Denied + body: + text/plain: + example: Access Denied + 500: + description: Internal server error, e.g. due to misconfiguration + body: + text/plain: + example: internal server error, contact administrator + /{id}: displayName: Manage Accounts By Id description: Service endpoints that manage accounts by an existing Id diff --git a/src/main/java/org/folio/edge/patron/Constants.java b/src/main/java/org/folio/edge/patron/Constants.java index 7009965..5e76604 100644 --- a/src/main/java/org/folio/edge/patron/Constants.java +++ b/src/main/java/org/folio/edge/patron/Constants.java @@ -23,6 +23,7 @@ public class Constants { public static final String PARAM_ITEM_ID = "itemId"; public static final String PARAM_INSTANCE_ID = "instanceId"; public static final String PARAM_HOLD_ID = "holdId"; + public static final String PARAM_EMAIL_ID = "emailId"; public static final String PARAM_REQUEST_ID = "requestId"; public static final String MSG_ACCESS_DENIED = "Access Denied"; diff --git a/src/main/java/org/folio/edge/patron/MainVerticle.java b/src/main/java/org/folio/edge/patron/MainVerticle.java index 57425e3..7de4ba9 100644 --- a/src/main/java/org/folio/edge/patron/MainVerticle.java +++ b/src/main/java/org/folio/edge/patron/MainVerticle.java @@ -57,6 +57,9 @@ public Router defineRoutes() { router.route(HttpMethod.GET, "/patron/account/:patronId") .handler(patronHandler::handleGetAccount); + router.route(HttpMethod.GET, "/patron/account/:patronId/by-email/:emailId") + .handler(patronHandler::handleGetExtPatronAccountByEmail); + router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/renew") .handler(patronHandler::handleRenew); diff --git a/src/main/java/org/folio/edge/patron/PatronHandler.java b/src/main/java/org/folio/edge/patron/PatronHandler.java index cdaa019..66d81fb 100644 --- a/src/main/java/org/folio/edge/patron/PatronHandler.java +++ b/src/main/java/org/folio/edge/patron/PatronHandler.java @@ -8,6 +8,7 @@ import static org.folio.edge.patron.Constants.MSG_HOLD_NOBODY; import static org.folio.edge.patron.Constants.MSG_INTERNAL_SERVER_ERROR; import static org.folio.edge.patron.Constants.MSG_REQUEST_TIMEOUT; +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; @@ -137,6 +138,17 @@ public void handleRenew(RoutingContext ctx) { } + public void handleGetExtPatronAccountByEmail(RoutingContext ctx) { + handleCommon(ctx, + new String[] { PARAM_PATRON_ID, PARAM_EMAIL_ID }, + new String[] {}, + (client, params) -> ((PatronOkapiClient) client).getExtPatronAccountByEmail( + params.get(PARAM_EMAIL_ID), + resp -> handleProxyResponse(ctx, resp), + t -> handleProxyException(ctx, t))); + } + + public void handlePlaceItemHold(RoutingContext ctx) { if (ctx.body().asJsonObject() == null) { badRequest(ctx, MSG_HOLD_NOBODY); 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 e3c2040..e874b0c 100644 --- a/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java +++ b/src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java @@ -92,6 +92,17 @@ public void getAccount(String patronId, boolean includeLoans, boolean includeCha exceptionHandler); } + public void getExtPatronAccountByEmail(String email, Handler> responseHandler, + Handler exceptionHandler) { + String url = String.format("%s/patron/account/by-email/%s", okapiURL, email); + get( + url, + tenant, + null, + responseHandler, + exceptionHandler); + } + public void renewItem(String patronId, String itemId, Handler> responseHandler, Handler exceptionHandler) { post( diff --git a/src/test/java/org/folio/edge/patron/MainVerticleTest.java b/src/test/java/org/folio/edge/patron/MainVerticleTest.java index 79a4032..af070bc 100644 --- a/src/test/java/org/folio/edge/patron/MainVerticleTest.java +++ b/src/test/java/org/folio/edge/patron/MainVerticleTest.java @@ -256,6 +256,17 @@ public void testGetAccountPatronNotFound(TestContext context) throws Exception { assertEquals(expectedStatusCode, msg.httpStatusCode); } + @Test + public void testGetAccountByEmail(TestContext context) { + logger.info("=== Test request for getting external_patron by email ==="); + + RestAssured + .get(String.format("/patron/account/%s/by-email/%s?apikey=%s", extPatronId, "fgh@mail", apiKey)) + .then() + .statusCode(200) + .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + } + @Test public void testGetAccountNoApiKey(TestContext context) throws Exception { logger.info("=== Test request with malformed apiKey ==="); 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 08d2305..580e39d 100644 --- a/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java +++ b/src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java @@ -135,6 +135,9 @@ public Router defineRoutes() { router.route(HttpMethod.GET, "/patron/account/:patronId") .handler(this::getAccountHandler); + router.route(HttpMethod.GET, "/patron/account/by-email/:emailId") + .handler(this::getExtPatronAccountHandler); + router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/renew") .handler(this::renewItemHandler); @@ -230,6 +233,22 @@ public void getAccountHandler(RoutingContext ctx) { } } + public void getExtPatronAccountHandler(RoutingContext ctx) { + String token = ctx.request().getHeader(X_OKAPI_TOKEN); + + if (token == null || !token.equals(MOCK_TOKEN)) { + ctx.response() + .setStatusCode(403) + .putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN) + .end("Access requires permission: patron.account.get"); + } else { + ctx.response() + .setStatusCode(200) + .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .end(getPatron().toString()); + } + } + public void renewItemHandler(RoutingContext ctx) { String patronId = ctx.request().getParam(PARAM_PATRON_ID); String itemId = ctx.request().getParam(PARAM_ITEM_ID);