Skip to content

Commit

Permalink
PR-1821 Add default patron fee option
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Jul 23, 2024
1 parent cd69c62 commit 7231ea1
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/folio/ncip/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ public class Constants {
public static final String ITEM_SEARCH_BY_BARCODE_URL = "/inventory/items?limit=1&query=barcode%3D%3D%22$barcode$%22";
public static final String ADDRESS_TYPES = "/addresstypes";
public static final String PATRON_PIN_VERIFY = "/patron-pin/verify";

public static final String FEE_OWNER_URL = "/owners?query=owner=%22Reshare-ILL%22&limit=1";
public static final String FEE_FINE_BY_OWNER_AND_TYPE = "/feefines?query=ownerId==$ownerId$%20and%20feeFineType=%22$feeType$%22&limit=1";
public static final String PATRON_GROUP_BY_ID = "/groups/";
public static final String ACCOUNT_URL = "/accounts";

public static final String DEFAULT_TIMEOUT = "30000";
public static final String SERVICE_MGR_TIMEOUT = "service_manager_timeout_ms";
public static final String CHARGE_DEFAULT_PATRON_FEE = "charge-default-patron-fee";
public static final String DEFAULT_PAYMENT_STATUS = "Outstanding";
public static final String DEFAULT_FEE_STATUS = "Open";

}
7 changes: 6 additions & 1 deletion src/main/java/org/folio/ncip/FolioNcipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.extensiblecatalog.ncip.v2.common.Translator;
import org.extensiblecatalog.ncip.v2.common.TranslatorFactory;
import org.extensiblecatalog.ncip.v2.service.BibliographicRecordIdentifierCode;
import org.extensiblecatalog.ncip.v2.service.FiscalActionType;
import org.extensiblecatalog.ncip.v2.service.FiscalTransactionType;
import org.extensiblecatalog.ncip.v2.service.LocationType;
import org.extensiblecatalog.ncip.v2.service.NCIPInitiationData;
import org.extensiblecatalog.ncip.v2.service.NCIPResponseData;
Expand Down Expand Up @@ -98,12 +100,15 @@ private Future<Void> initToolkitDefaults() {

private void setUpMapping(){
SchemeValuePair.allowNullScheme(RequestType.class.getName(), RequestScopeType.class.getName(),
BibliographicRecordIdentifierCode.class.getName(), LocationType.class.getName(), PickupLocation.class.getName());
BibliographicRecordIdentifierCode.class.getName(), LocationType.class.getName(), PickupLocation.class.getName(),
FiscalActionType.class.getName(), FiscalTransactionType.class.getName());
SchemeValuePair.mapBehavior(RequestType.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(RequestScopeType.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(BibliographicRecordIdentifierCode.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(LocationType.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(PickupLocation.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(FiscalActionType.class.getName(), SchemeValueBehavior.ALLOW_ANY);
SchemeValuePair.mapBehavior(FiscalTransactionType.class.getName(), SchemeValueBehavior.ALLOW_ANY);
}

public InputStream ncipProcess(RoutingContext context) throws Exception {
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/folio/ncip/FolioRemoteServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ public JsonObject acceptItem(AcceptItemInitiationData initData, UserId userId, S

returnValues.mergeIn(new JsonObject(requestRespone)).put("item", new JsonObject(itemResponse))
.put("holdings", new JsonObject(holdingsResponse));

addDefaultPatronFee(initData, user.getString("id"), user.getString("patronGroup"), baseUrl);
} catch (Exception e) {
// IF ANY OF THE ABOVE FAILED - ATTEMPT TO DELETE THE INSTANCE, HOLDINGS ITEM
// THAT MAY HAVE BEEN CREATED ALONG THE WAY
Expand All @@ -573,6 +575,48 @@ public JsonObject acceptItem(AcceptItemInitiationData initData, UserId userId, S
return returnValues;
}

protected void addDefaultPatronFee(AcceptItemInitiationData initData, String userId, String patronGroupId, String baseUrl) throws FolioNcipException {
if (initData.getFiscalTransactionInformation() != null && initData.getFiscalTransactionInformation().getFiscalActionType() != null &&
Constants.CHARGE_DEFAULT_PATRON_FEE.equalsIgnoreCase(initData.getFiscalTransactionInformation().getFiscalActionType().getValue())) {
try {
JsonObject owner = new JsonObject(callApiGet(baseUrl + Constants.FEE_OWNER_URL));
if (owner.getJsonArray("owners").isEmpty()) {
throw new FolioNcipException("Failed to find fee owner Reshare-ILL");
}
String ownerId = owner.getJsonArray("owners").getJsonObject(0).getString("id");
JsonObject patronGroup = new JsonObject(callApiGet(baseUrl + Constants.PATRON_GROUP_BY_ID + patronGroupId));
if (patronGroup.getString("group") == null) {
throw new FolioNcipException("Failed to find patron group " + patronGroupId);
}
JsonObject fees = new JsonObject(callApiGet(baseUrl + Constants.FEE_FINE_BY_OWNER_AND_TYPE
.replace("$ownerId$", ownerId).replace("$feeType$", patronGroup.getString("group"))));
if (fees.getJsonArray("feefines").isEmpty()) {
throw new FolioNcipException("Failed to find fee type " + patronGroup.getString("group"));
}
JsonObject fee = fees.getJsonArray("feefines").getJsonObject(0);
JsonObject charge = new JsonObject();
charge.put("ownerId", ownerId);
charge.put("feeFineId", fee.getString("id"));
charge.put("amount", fee.getValue("defaultAmount"));
JsonObject paymentStatus = new JsonObject();
paymentStatus.put("name", Constants.DEFAULT_PAYMENT_STATUS);
charge.put("paymentStatus", paymentStatus);
JsonObject status = new JsonObject();
status.put("name", Constants.DEFAULT_FEE_STATUS);
charge.put("status", status);
charge.put("remaining", fee.getValue("defaultAmount"));
charge.put("feeFineType", fee.getString("feeFineType"));
charge.put("feeFineOwner", owner.getJsonArray("owners").getJsonObject(0).getString("owner"));
charge.put("userId", userId);
charge.put("id", UUID.randomUUID().toString());
callApiPost(baseUrl + Constants.ACCOUNT_URL, charge);
} catch (Exception e) {
logger.error("Failed to add default patron fee", e);
throw new FolioNcipException("Failed to add default patron fee");
}
}
}

private void deleteItemAndRelatedRecords(String baseUrl, String instanceUuid, String holdingsUuid, String itemUuid){
String deleteInstanceUrl = baseUrl + Constants.INSTANCE_URL + "/" + instanceUuid;
String deleteHoldingsUrl = baseUrl + Constants.HOLDINGS_URL + "/" + holdingsUuid;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/folio/ncip/AcceptItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.folio.ncip;

import io.restassured.response.Response;
import org.junit.Test;

import java.net.MalformedURLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AcceptItem extends TestBase {

private static final String REQUEST_ID = "de627598-d468-41e0-a849-450940478477";
@Test
public void acceptItem() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-acceptItem.xml");
String body = response.getBody().prettyPrint();
assertEquals(200, response.getStatusCode());
assertTrue(body.contains(REQUEST_ID));
}

@Test
public void acceptItemFailFee() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-acceptItem-blocked.xml");
String body = response.getBody().prettyPrint();
assertEquals(200, response.getStatusCode());
assertTrue(body.contains("Problem performing AcceptItem"));
}

}
49 changes: 48 additions & 1 deletion src/test/java/org/folio/ncip/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ private Router defineRoutes() {
router.get("/configurations/entries/toolkit").handler(this::getToolkitCofigs);
router.get("/configurations/entries").handler(this::getNcipConfigs);
router.get("/inventory/items").handler(this::items);
router.post("/inventory/items").handler(this::itemsPost);
router.get("/inventory/instances").handler(this::instances);
router.post("/inventory/instances").handler(this::instancePost);
router.get("/holdings-storage/holdings/:id").handler(this::holdingsById);
router.post("/holdings-storage/holdings").handler(this::holdingsById);
router.post("/circulation/requests").handler(this::requestsPost);
router.get("/addresstypes").handler(this::addressTypes);
router.get("/instance-types").handler(this::instanceTypes);
Expand All @@ -124,6 +127,9 @@ private Router defineRoutes() {
router.get("/circulation/requests").handler(this::getCirculationRequestList);
router.put("/circulation/requests/:id").handler(this::putCirculationRequestById);
router.post("/patron-pin/verify").handler(this::verifyPing);
router.get("/owners").handler(this::getFeeOwner);
router.get("/feefines").handler(this::findFee);
router.post("/accounts").handler(this::postAccounts);

return router;
}
Expand Down Expand Up @@ -153,7 +159,12 @@ private void getToolkitCofigs(RoutingContext ctx) {
}

private void groupLookup(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "groups-get.json";
String mockFileName;
if (ctx.request().path().contains("dac3aab4-7a86-422b-b5fc-6fbf7afd7aea")) {
mockFileName = TestConstants.PATH_TO_MOCK_FILES + "groups-getStaff.json";
} else {
mockFileName = TestConstants.PATH_TO_MOCK_FILES + "groups-get.json";
}
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}
Expand Down Expand Up @@ -195,12 +206,24 @@ private void items(RoutingContext ctx) {
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void itemsPost(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "items-post.json";
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void instances(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instancesByHrid-get.json";
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void instancePost(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instances-post.json";
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void holdingsById(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "holdingsById-get.json";
String body = readLineByLine(mockFileName);
Expand Down Expand Up @@ -291,6 +314,30 @@ private void verifyPing(RoutingContext ctx) {
.end();
}

private void getFeeOwner(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "feeOwners-get.json";
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void findFee(RoutingContext ctx) {
String query = ctx.request().getParam("query");
String mockFileName;
if (query.contains("staff")) {
mockFileName = TestConstants.PATH_TO_MOCK_FILES + "feefines-getEmpty.json";
} else {
mockFileName = TestConstants.PATH_TO_MOCK_FILES + "feefines-get.json";
}
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void postAccounts(RoutingContext ctx) {
String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "accounts-post.json";
String body = readLineByLine(mockFileName);
serverResponse(ctx, 200, APPLICATION_JSON, body);
}

private void serverResponse(RoutingContext ctx, int statusCode, String contentType, String body) {
ctx.response()
.setStatusCode(statusCode)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/folio/ncip/NcipTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
LookupUser.class, RequestItem.class, CancelRequestItem.class, DeleteItem.class
LookupUser.class, RequestItem.class, CancelRequestItem.class, DeleteItem.class, AcceptItem.class
})
public class NcipTestSuite {

Expand Down
23 changes: 23 additions & 0 deletions src/test/resources/mockdata/accounts-post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"amount": 5.0,
"remaining": 5.0,
"status": {
"name": "Open"
},
"paymentStatus": {
"name": "Outstanding"
},
"feeFineType": "staff",
"feeFineOwner": "Reshare-ILL",
"metadata": {
"createdDate": "2024-07-23T07:35:12.492+00:00",
"createdByUserId": "7d265701-9b80-5501-bc85-0758b4667022",
"updatedDate": "2024-07-23T07:35:12.492+00:00",
"updatedByUserId": "7d265701-9b80-5501-bc85-0758b4667022"
},
"userId": "d4a845bd-a326-4916-9cb4-6a5ac1a86155",
"feeFineId": "00fb288d-207a-4caa-9538-d18989367fbc",
"ownerId": "fe2307cf-6f39-4b2e-bb37-7668a2b5accf",
"id": "628e0831-30b0-415f-8b21-944950624926",
"contributors": []
}
16 changes: 16 additions & 0 deletions src/test/resources/mockdata/feeOwners-get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"owners": [
{
"owner": "Reshare-ILL",
"servicePointOwner": [],
"metadata": {
"createdDate": "2024-07-19T10:08:41.016+00:00",
"createdByUserId": "7d265701-9b80-5501-bc85-0758b4667022",
"updatedDate": "2024-07-19T10:10:48.414+00:00",
"updatedByUserId": "7d265701-9b80-5501-bc85-0758b4667022"
},
"id": "fe2307cf-6f39-4b2e-bb37-7668a2b5accf"
}
],
"totalRecords": 1
}
23 changes: 23 additions & 0 deletions src/test/resources/mockdata/feefines-get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"feefines": [
{
"automatic": false,
"feeFineType": "staff",
"defaultAmount": 5.0,
"ownerId": "fe2307cf-6f39-4b2e-bb37-7668a2b5accf",
"metadata": {
"createdDate": "2024-07-19T10:11:07.117+00:00",
"createdByUserId": "7d265701-9b80-5501-bc85-0758b4667022",
"updatedDate": "2024-07-19T10:11:07.117+00:00",
"updatedByUserId": "7d265701-9b80-5501-bc85-0758b4667022"
},
"id": "00fb288d-207a-4caa-9538-d18989367fbc"
}
],
"totalRecords": 1,
"resultInfo": {
"totalRecords": 1,
"facets": [],
"diagnostics": []
}
}
10 changes: 10 additions & 0 deletions src/test/resources/mockdata/feefines-getEmpty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"feefines": [
],
"totalRecords": 0,
"resultInfo": {
"totalRecords": 0,
"facets": [],
"diagnostics": []
}
}
12 changes: 12 additions & 0 deletions src/test/resources/mockdata/groups-getStaff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"group": "staff",
"desc": "Staff Member",
"id": "3684a786-6671-4268-8ed0-9db82ebca60b",
"expirationOffsetInDays": 730,
"metadata": {
"createdDate": "2024-03-13T00:03:41.447+00:00",
"createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89",
"updatedDate": "2024-05-16T14:06:48.906+00:00",
"updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89"
}
}
Loading

0 comments on commit 7231ea1

Please sign in to comment.