Skip to content

Commit

Permalink
Merge pull request #28 from indexdata/PR-1861
Browse files Browse the repository at this point in the history
PR-1861 Implement CreateUserFiscalTransaction for charging fees
  • Loading branch information
JanisSaldabols authored Aug 2, 2024
2 parents d9fa8ed + 8a6dd8a commit 5b0fe20
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/folio/ncip/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Constants {
public static final String REQUEST_ITEM_MISSING_PROBLEM = "Problem occurred while fetching item";
public static final String CANCEL_REQUEST_ITEM_PROBLEM = "Problem performing CancelRequestItem";
public static final String DELETE_ITEM_PROBLEM = "Problem performing DeleteItem";
public static final String CREATE_USER_FISCAL_TRANSACTION_PROBLEM = "Problem performing CreateUserFiscalTransaction";

//MISC
public static final String NCIP_TOOLKIT_PROPS = "ncip_toolkit_props";
Expand Down Expand Up @@ -79,6 +80,7 @@ public class Constants {
public static final String REQUEST_CANCEL_ADDITIONAL_INFO = "NCIP cancel";
public static final String REQUEST_CANCEL_PATRON_ADDITIONAL_INFO = "Patron did not pickup item";
public static final String AUTH_TYPE_PIN = "pin";
public static final String PATRON_GROUP = "patronGroup";


//ENDPOINTS
Expand Down
4 changes: 3 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,7 @@
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.CurrencyCode;
import org.extensiblecatalog.ncip.v2.service.FiscalActionType;
import org.extensiblecatalog.ncip.v2.service.FiscalTransactionType;
import org.extensiblecatalog.ncip.v2.service.LocationType;
Expand Down Expand Up @@ -101,14 +102,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(),
FiscalActionType.class.getName(), FiscalTransactionType.class.getName());
FiscalActionType.class.getName(), FiscalTransactionType.class.getName(), CurrencyCode.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);
SchemeValuePair.mapBehavior(CurrencyCode.class.getName(), SchemeValueBehavior.ALLOW_ANY);
}

public InputStream ncipProcess(RoutingContext context) throws Exception {
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/folio/ncip/FolioRemoteServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.extensiblecatalog.ncip.v2.service.FiscalTransactionInformation;
import org.extensiblecatalog.ncip.v2.service.RemoteServiceManager;
import org.extensiblecatalog.ncip.v2.service.UserId;
import org.folio.util.StringUtil;
Expand Down Expand Up @@ -565,7 +566,7 @@ 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);
addDefaultPatronFee(initData.getFiscalTransactionInformation(), user.getString("id"), user.getString(Constants.PATRON_GROUP), 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 @@ -575,9 +576,9 @@ public JsonObject acceptItem(AcceptItemInitiationData initData, UserId userId, S
return returnValues;
}

protected void addDefaultPatronFee(AcceptItemInitiationData initData, String userId, String patronGroupId, String baseUrl) throws Exception {
if (initData.getFiscalTransactionInformation() != null && initData.getFiscalTransactionInformation().getFiscalActionType() != null &&
Constants.CHARGE_DEFAULT_PATRON_FEE.equalsIgnoreCase(initData.getFiscalTransactionInformation().getFiscalActionType().getValue())) {
protected void addDefaultPatronFee(FiscalTransactionInformation fiscalTransactionInformation, String userId, String patronGroupId, String baseUrl) throws Exception {
if (fiscalTransactionInformation != null && fiscalTransactionInformation.getFiscalActionType() != null &&
Constants.CHARGE_DEFAULT_PATRON_FEE.equalsIgnoreCase(fiscalTransactionInformation.getFiscalActionType().getValue())) {
try {
JsonObject owner = new JsonObject(callApiGet(baseUrl + Constants.FEE_OWNER_URL));
JsonArray ownersArray = owner.getJsonArray("owners");
Expand Down Expand Up @@ -736,7 +737,7 @@ private String retreiveItemTitle(AcceptItemInitiationData initData) {
public JsonObject gatherPatronData(JsonObject user, String userId) throws Exception {

String baseUrl = okapiHeaders.get(Constants.X_OKAPI_URL);
String groupId = user.getString("patronGroup");
String groupId = user.getString(Constants.PATRON_GROUP);
final String userIdQuery = "query=(" + PercentCodec.encode("userId==" + StringUtil.cqlEncode(userId) + ")");
final long LONG_DELAY_MS = 10000;

Expand Down Expand Up @@ -1085,4 +1086,15 @@ public void deleteItem(String itemId, String agencyId) throws Exception {
throw exception;
}
}

public void createUserFiscalTransaction(UserId userId, FiscalTransactionInformation fiscalTransactionInformation) throws Exception {
try {
String baseUrl = okapiHeaders.get(Constants.X_OKAPI_URL);
JsonObject user = lookupPatronRecord(userId);
addDefaultPatronFee(fiscalTransactionInformation, user.getString("id"), user.getString(Constants.PATRON_GROUP), baseUrl);
} catch (Exception exception) {
logger.error("Exception occurred during CreateUserFiscalTransaction");
throw exception;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.folio.ncip.services;

import org.extensiblecatalog.ncip.v2.service.CreateUserFiscalTransactionInitiationData;
import org.extensiblecatalog.ncip.v2.service.CreateUserFiscalTransactionResponseData;
import org.extensiblecatalog.ncip.v2.service.NCIPService;
import org.extensiblecatalog.ncip.v2.service.RemoteServiceManager;
import org.extensiblecatalog.ncip.v2.service.ServiceContext;
import org.extensiblecatalog.ncip.v2.service.ServiceException;
import org.extensiblecatalog.ncip.v2.service.ValidationException;

public interface CreateUserFiscalTransactionService extends NCIPService<CreateUserFiscalTransactionInitiationData, CreateUserFiscalTransactionResponseData> {
CreateUserFiscalTransactionResponseData performService(CreateUserFiscalTransactionInitiationData initData, ServiceContext serviceContext, RemoteServiceManager remoteServiceManager) throws ServiceException, ValidationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public class FolioCancelRequestItemService extends FolioNcipService implements CancelRequestItemService {

private static final Logger LOGGER = Logger.getLogger(FolioCheckInItemService.class);
private static final Logger LOGGER = Logger.getLogger(FolioCancelRequestItemService.class);

@Override
public CancelRequestItemResponseData performService(CancelRequestItemInitiationData cancelRequestItemInitiationData,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.folio.ncip.services;

import org.apache.log4j.Logger;
import org.extensiblecatalog.ncip.v2.service.CreateUserFiscalTransactionInitiationData;
import org.extensiblecatalog.ncip.v2.service.CreateUserFiscalTransactionResponseData;
import org.extensiblecatalog.ncip.v2.service.Problem;
import org.extensiblecatalog.ncip.v2.service.ProblemType;
import org.extensiblecatalog.ncip.v2.service.RemoteServiceManager;
import org.extensiblecatalog.ncip.v2.service.ServiceContext;
import org.extensiblecatalog.ncip.v2.service.ServiceException;
import org.extensiblecatalog.ncip.v2.service.UserId;
import org.extensiblecatalog.ncip.v2.service.ValidationException;
import org.folio.ncip.Constants;
import org.folio.ncip.FolioRemoteServiceManager;

import java.util.ArrayList;

public class FolioCreateUserFiscalTransactionService extends FolioNcipService implements CreateUserFiscalTransactionService {
private static final Logger LOGGER = Logger.getLogger(FolioCreateUserFiscalTransactionService.class);

@Override
public CreateUserFiscalTransactionResponseData performService(CreateUserFiscalTransactionInitiationData initData, ServiceContext serviceContext, RemoteServiceManager remoteServiceManager) throws ServiceException, ValidationException {
CreateUserFiscalTransactionResponseData responseData = new CreateUserFiscalTransactionResponseData();
UserId userId = initData.getUserId();
try {
validateUserId(userId);
initData.getFiscalTransactionInformation().getFiscalActionType();
} catch (Exception exception) {
Problem problem = new Problem(new ProblemType(Constants.CREATE_USER_FISCAL_TRANSACTION_PROBLEM), Constants.CREATE_USER_FISCAL_TRANSACTION_PROBLEM,
exception.getMessage());
LOGGER.error("Request validation failed");
return addProblem(responseData, problem);
}

try {
((FolioRemoteServiceManager)remoteServiceManager).createUserFiscalTransaction(userId, initData.getFiscalTransactionInformation());
} catch(Exception e) {
Problem problem = new Problem(new ProblemType(Constants.CREATE_USER_FISCAL_TRANSACTION_PROBLEM), Constants.UNKNOWN_DATA_ELEMENT,
Constants.CREATE_USER_FISCAL_TRANSACTION_PROBLEM, e.getMessage());
return addProblem(responseData, problem);
}
responseData.setUserId(userId);
return responseData;
}

private CreateUserFiscalTransactionResponseData addProblem(CreateUserFiscalTransactionResponseData responseData, Problem problem){
if (responseData.getProblems() == null) {
responseData.setProblems(new ArrayList<>());
}
responseData.getProblems().add(problem);
return responseData;
}
}
1 change: 1 addition & 0 deletions src/main/resources/toolkit.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CheckInItemService.class=org.folio.ncip.services.FolioCheckInItemService
RequestItemService.class=org.folio.ncip.services.FolioRequestItemService
CancelRequestItemService.class=org.folio.ncip.services.FolioCancelRequestItemService
DeleteItemService.class=org.folio.ncip.services.FolioDeleteItemService
CreateUserFiscalTransactionService.class=org.folio.ncip.services.FolioCreateUserFiscalTransactionService

CoreConfiguration.AddedSVPClassesAllowNullScheme=org.extensiblecatalog.ncip.v2.service.RequestedActionType

Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/folio/ncip/CreateUserFiscalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 CreateUserFiscalTransaction extends TestBase {

private static final String USER_ID = "8377631";
private static final String PROBLEM = "Problem";

@Test
public void callCreateUserFiscalTransaction() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-createUserFiscalTransaction.xml");
String body = response.getBody().prettyPrint();
assertEquals(200, response.getStatusCode());
assertTrue(body.contains(USER_ID));
}

@Test
public void callCreateUserFiscalTransactionNoTransaction() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-createUserFiscalTransactionNoTrans.xml");
String body = response.getBody().prettyPrint();
assertEquals(200, response.getStatusCode());
assertTrue(body.contains(PROBLEM));
}

@Test
public void callCreateUserFiscalTransactionBlocked() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-createUserFiscalTransactionBlocked.xml");
String body = response.getBody().prettyPrint();
assertEquals(200, response.getStatusCode());
assertTrue(body.contains(PROBLEM));
}
}
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, AcceptItem.class
LookupUser.class, RequestItem.class, CancelRequestItem.class, DeleteItem.class, AcceptItem.class, CreateUserFiscalTransaction.class
})
public class NcipTestSuite {

Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/mockdata/ncip-createUserFiscalTransaction.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip" ns1:version="http://www.niso.org/schemas/ncip/v2_0/imp1/xsd/ncip_v2_0.xsd">
<ns1:CreateUserFiscalTransaction>
<ns1:InitiationHeader>
<ns1:FromAgencyId>
<ns1:AgencyId>Relais</ns1:AgencyId>
</ns1:FromAgencyId>
<ns1:ToAgencyId>
<ns1:AgencyId>Lehigh University</ns1:AgencyId>
</ns1:ToAgencyId>
</ns1:InitiationHeader>
<ns1:UserId>
<ns1:AgencyId>LEH</ns1:AgencyId>
<ns1:UserIdentifierValue>8377631</ns1:UserIdentifierValue>
</ns1:UserId>
<ns1:FiscalTransactionInformation>
<ns1:FiscalActionType>charge-default-patron-fee</ns1:FiscalActionType>
<ns1:FiscalTransactionType>ILL-fee</ns1:FiscalTransactionType>
<ns1:Amount>
<ns1:MonetaryValue>0</ns1:MonetaryValue>
</ns1:Amount>
</ns1:FiscalTransactionInformation>
</ns1:CreateUserFiscalTransaction>
</ns1:NCIPMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip" ns1:version="http://www.niso.org/schemas/ncip/v2_0/imp1/xsd/ncip_v2_0.xsd">
<ns1:CreateUserFiscalTransaction>
<ns1:InitiationHeader>
<ns1:FromAgencyId>
<ns1:AgencyId>Relais</ns1:AgencyId>
</ns1:FromAgencyId>
<ns1:ToAgencyId>
<ns1:AgencyId>Lehigh University</ns1:AgencyId>
</ns1:ToAgencyId>
</ns1:InitiationHeader>
<ns1:UserId>
<ns1:AgencyId>LEH</ns1:AgencyId>
<ns1:UserIdentifierValue>5551213</ns1:UserIdentifierValue>
</ns1:UserId>
<ns1:FiscalTransactionInformation>
<ns1:FiscalActionType>charge-default-patron-fee</ns1:FiscalActionType>
<ns1:FiscalTransactionType>ILL-fee</ns1:FiscalTransactionType>
<ns1:Amount>
<ns1:MonetaryValue>0</ns1:MonetaryValue>
</ns1:Amount>
</ns1:FiscalTransactionInformation>
</ns1:CreateUserFiscalTransaction>
</ns1:NCIPMessage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip" ns1:version="http://www.niso.org/schemas/ncip/v2_0/imp1/xsd/ncip_v2_0.xsd">
<ns1:CreateUserFiscalTransaction>
<ns1:InitiationHeader>
<ns1:FromAgencyId>
<ns1:AgencyId>Relais</ns1:AgencyId>
</ns1:FromAgencyId>
<ns1:ToAgencyId>
<ns1:AgencyId>Lehigh University</ns1:AgencyId>
</ns1:ToAgencyId>
</ns1:InitiationHeader>
<ns1:UserId>
<ns1:AgencyId>LEH</ns1:AgencyId>
<ns1:UserIdentifierValue>8377631</ns1:UserIdentifierValue>
</ns1:UserId>
</ns1:CreateUserFiscalTransaction>
</ns1:NCIPMessage>

0 comments on commit 5b0fe20

Please sign in to comment.