Skip to content

Commit

Permalink
MODTLR-77 Pickup transaction attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkurash committed Oct 7, 2024
1 parent 9de1aac commit 307ef4c
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 145 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/folio/domain/entity/EcsTlrEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public class EcsTlrEntity {
private UUID secondaryRequestId;
private String secondaryRequestTenantId;
private UUID secondaryRequestDcbTransactionId;
private UUID intermediateRequestId;
private String intermediateRequestTenantId;
private UUID intermediateRequestDcbTransactionId;

}
4 changes: 4 additions & 0 deletions src/main/java/org/folio/service/ConsortiaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

public interface ConsortiaService {
TenantCollection getAllDataTenants(String consortiumId);

// boolean isCurrentTenantCentral();

// <T> T executeInTenant(String tenantId, Callable<T> action);
}
3 changes: 2 additions & 1 deletion src/main/java/org/folio/service/DcbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

public interface DcbService {
void createLendingTransaction(EcsTlrEntity ecsTlr);
void createBorrowingTransaction(EcsTlrEntity ecsTlr, Request request);
void createBorrowingTransaction(EcsTlrEntity ecsTlr, Request request, String tenantId);
void createPickupTransaction(EcsTlrEntity ecsTlr, Request request, String tenantId);
TransactionStatusResponse getTransactionStatus(UUID transactionId, String tenantId);
TransactionStatusResponse updateTransactionStatus(UUID transactionId,
TransactionStatus.StatusEnum newStatus, String tenantId);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/folio/service/RequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import org.folio.domain.dto.InventoryItem;
import org.folio.domain.dto.ReorderQueue;
import org.folio.domain.dto.Request;
import org.folio.domain.entity.EcsTlrEntity;

public interface RequestService {
RequestWrapper createPrimaryRequest(Request request, String borrowingTenantId);
RequestWrapper createPrimaryRequest(Request request, String primaryRequestTenantId);

RequestWrapper createSecondaryRequest(Request request, String borrowingTenantId,
Collection<String> lendingTenantIds);
RequestWrapper createSecondaryRequest(Request request, String primaryRequestTenantId,
Collection<String> secondaryRequestTenantIds);

CirculationItem createCirculationItem(EcsTlrEntity ecsTlr, Request secondaryRequest,
String borrowingTenantId, String lendingTenantId);
// CirculationItem createCirculationItem(String itemId, String instanceId,
// String circulationItemTenantId, String inventoryTenantId);

CirculationItem createCirculationItem(String itemId, String instanceId,
String pickupLocation, String circulationItemTenantId, String inventoryTenantId);

CirculationItem updateCirculationItemOnRequestCreation(CirculationItem circulationItem,
Request secondaryRequest);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/folio/service/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.folio.domain.entity.EcsTlrEntity;

public interface TenantService {
Optional<String> getBorrowingTenant(EcsTlrEntity ecsTlr);
Optional<String> getPrimaryRequestTenantId(EcsTlrEntity ecsTlr);

List<String> getLendingTenants(EcsTlrEntity ecsTlr);
List<String> getSecondaryRequestTenants(EcsTlrEntity ecsTlr);
}
2 changes: 2 additions & 0 deletions src/main/java/org/folio/service/UserTenantsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface UserTenantsService {
UserTenant findFirstUserTenant();

String getCentralTenantId();
}
33 changes: 33 additions & 0 deletions src/main/java/org/folio/service/impl/ConsortiaServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.folio.client.feign.ConsortiaClient;
import org.folio.domain.dto.TenantCollection;
import org.folio.service.ConsortiaService;
import org.folio.service.UserTenantsService;
import org.folio.spring.service.SystemUserScopedExecutionService;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
Expand All @@ -13,9 +15,40 @@
@RequiredArgsConstructor
public class ConsortiaServiceImpl implements ConsortiaService {
private final ConsortiaClient consortiaClient;
private final UserTenantsService userTenantsService;
private final SystemUserScopedExecutionService systemUserScopedExecutionService;

@Override
public TenantCollection getAllDataTenants(String consortiumId) {
return consortiaClient.getConsortiaTenants(consortiumId);
}

// @Override
// public boolean isCurrentTenantCentral() {
// var userTenant = userTenantsService.findFirstUserTenant();
// var centralTenantId = userTenant.getCentralTenantId();
// var currentTenantId = userTenant.getTenantId();
//
// if (centralTenantId == null || currentTenantId == null) {
// log.warn("isCurrentTenantCentral:: Cannot determine central tenant or current tenant");
// return false;
// }
//
// return centralTenantId.equals(currentTenantId);
// }
//
// @Override
// public <T> T executeInTenant(String tenantId, Callable<T> action) {
// if (isCurrentTenantCentral()) {
// try {
// return action.call();
// } catch (Exception e) {
// log.info("executeInTenant:: Failed to execute in Central tenant");
// return null;
// }
// } else {
// return systemUserScopedExecutionService.executeSystemUserScoped(
// tenantId, action);
// }
// }
}
22 changes: 20 additions & 2 deletions src/main/java/org/folio/service/impl/DcbServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.folio.domain.dto.DcbTransaction.RoleEnum.BORROWER;
import static org.folio.domain.dto.DcbTransaction.RoleEnum.LENDER;
import static org.folio.domain.dto.DcbTransaction.RoleEnum.PICKUP;

import java.util.UUID;

Expand Down Expand Up @@ -50,7 +51,7 @@ public void createLendingTransaction(EcsTlrEntity ecsTlr) {
}

@Override
public void createBorrowingTransaction(EcsTlrEntity ecsTlr, Request request) {
public void createBorrowingTransaction(EcsTlrEntity ecsTlr, Request request, String tenantId) {
log.info("createBorrowingTransaction:: creating borrowing transaction for ECS TLR {}", ecsTlr::getId);
DcbItem dcbItem = new DcbItem()
.id(request.getItemId())
Expand All @@ -60,12 +61,29 @@ public void createBorrowingTransaction(EcsTlrEntity ecsTlr, Request request) {
.requestId(ecsTlr.getPrimaryRequestId().toString())
.item(dcbItem)
.role(BORROWER);
final UUID borrowingTransactionId = createTransaction(transaction, ecsTlr.getPrimaryRequestTenantId());
final UUID borrowingTransactionId = createTransaction(transaction, tenantId);
ecsTlr.setPrimaryRequestDcbTransactionId(borrowingTransactionId);
log.info("createBorrowingTransaction:: borrowing transaction {} for ECS TLR {} created",
() -> borrowingTransactionId, ecsTlr::getId);
}

@Override
public void createPickupTransaction(EcsTlrEntity ecsTlr, Request request, String tenantId) {
log.info("createPickupTransaction:: creating borrowing transaction for ECS TLR {}", ecsTlr::getId);
DcbItem dcbItem = new DcbItem()
.id(request.getItemId())
.title(request.getInstance().getTitle())
.barcode(request.getItem().getBarcode());
DcbTransaction transaction = new DcbTransaction()
.requestId(ecsTlr.getPrimaryRequestId().toString())
.item(dcbItem)
.role(PICKUP);
final UUID transactionId = createTransaction(transaction, tenantId);
ecsTlr.setIntermediateRequestDcbTransactionId(transactionId);
log.info("createPickupTransaction:: pickup transaction {} for ECS TLR {} created",
() -> transactionId, ecsTlr::getId);
}

private UUID createTransaction(DcbTransaction transaction, String tenantId) {
final UUID transactionId = UUID.randomUUID();
log.info("createTransaction:: creating transaction {} in tenant {}", transactionId, tenantId);
Expand Down
101 changes: 74 additions & 27 deletions src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class EcsTlrServiceImpl implements EcsTlrService {
private final TenantService tenantService;
private final RequestService requestService;
private final DcbService dcbService;
private final UserTenantsServiceImpl userTenantsService;

@Override
public Optional<EcsTlr> get(UUID id) {
Expand All @@ -47,23 +48,53 @@ public EcsTlr create(EcsTlr ecsTlrDto) {
ecsTlrDto.getInstanceId(), ecsTlrDto.getItemId(), ecsTlrDto.getRequesterId());

final EcsTlrEntity ecsTlr = requestsMapper.mapDtoToEntity(ecsTlrDto);
String borrowingTenantId = getBorrowingTenant(ecsTlr);
Collection<String> lendingTenantIds = getLendingTenants(ecsTlr);
RequestWrapper secondaryRequest = requestService.createSecondaryRequest(
buildSecondaryRequest(ecsTlr), borrowingTenantId, lendingTenantIds);

log.info("create:: Creating circulation item for ECS TLR (ILR) {}", ecsTlrDto.getId());
CirculationItem circulationItem = requestService.createCirculationItem(ecsTlr,
secondaryRequest.request(), borrowingTenantId, secondaryRequest.tenantId());

String primaryRequestTenantId = getPrimaryRequestTenant(ecsTlr);
Collection<String> secondaryRequestsTenantIds = getSecondaryRequestTenants(ecsTlr);

log.info("create:: Creating secondary request for ECS TLR (ILR) {}", ecsTlrDto.getId());
RequestWrapper secondaryRequestWrapper = requestService.createSecondaryRequest(
buildSecondaryRequest(ecsTlr), primaryRequestTenantId, secondaryRequestsTenantIds);
Request secondaryRequest = secondaryRequestWrapper.request();
String secondaryRequestTenantId = secondaryRequestWrapper.tenantId();

log.info("create:: Creating circulation item for ECS TLR (ILR) {} in the primary request tenant", ecsTlrDto.getId());
CirculationItem circulationItem = requestService.createCirculationItem(
secondaryRequest.getItemId(), secondaryRequest.getInstanceId(),
secondaryRequest.getPickupServicePointId(), primaryRequestTenantId,
secondaryRequestTenantId);

log.info("create:: Creating primary request for ECS TLR (ILR) {}", ecsTlrDto.getId());
RequestWrapper primaryRequest = requestService.createPrimaryRequest(
buildPrimaryRequest(secondaryRequest.request()), borrowingTenantId);
buildPrimaryRequest(secondaryRequest), primaryRequestTenantId);

log.info("create:: Updating circulation item for ECS TLR (ILR) {}", ecsTlrDto.getId());
requestService.updateCirculationItemOnRequestCreation(circulationItem,
secondaryRequest.request());
secondaryRequest);

updateEcsTlr(ecsTlr, primaryRequest, secondaryRequestWrapper);

updateEcsTlr(ecsTlr, primaryRequest, secondaryRequest);
createDcbTransactions(ecsTlr, secondaryRequest.request());
var centralTenantId = userTenantsService.getCentralTenantId();
RequestWrapper intermediateRequest = null;
if (!primaryRequestTenantId.equals(centralTenantId)) {
log.info("create:: Primary request tenant is not central, creating intermediate request");

log.info("create:: Creating circulation item for ECS TLR (ILR) {} in the central tenant", ecsTlrDto.getId());
CirculationItem centralTenantCirculationItem = requestService.createCirculationItem(
secondaryRequest.getItemId(), secondaryRequest.getInstanceId(),
secondaryRequest.getPickupServicePointId(), centralTenantId,
secondaryRequestTenantId);

log.info("create:: Creating intermediate request for ECS TLR (ILR) {}", ecsTlrDto.getId());
intermediateRequest = requestService.createPrimaryRequest(
buildPrimaryRequest(secondaryRequest), primaryRequestTenantId);

log.info("create:: Updating circulation item for ECS TLR (ILR) {}", ecsTlrDto.getId());
requestService.updateCirculationItemOnRequestCreation(centralTenantCirculationItem,
secondaryRequest);
}

createDcbTransactions(ecsTlr, secondaryRequest,
intermediateRequest == null ? null : intermediateRequest.request(), centralTenantId);

return requestsMapper.mapEntityToDto(save(ecsTlr));
}
Expand All @@ -90,25 +121,25 @@ public boolean delete(UUID requestId) {
return false;
}

private String getBorrowingTenant(EcsTlrEntity ecsTlr) {
log.info("getBorrowingTenant:: getting borrowing tenant");
final String borrowingTenantId = tenantService.getBorrowingTenant(ecsTlr)
private String getPrimaryRequestTenant(EcsTlrEntity ecsTlr) {
log.info("getPrimaryRequestTenant:: getting primary request tenant");
final String primaryRequestTenantId = tenantService.getPrimaryRequestTenantId(ecsTlr)
.orElseThrow(() -> new TenantPickingException("Failed to get borrowing tenant"));
log.info("getBorrowingTenant:: borrowing tenant: {}", borrowingTenantId);
log.info("getPrimaryRequestTenant:: primary request tenant: {}", primaryRequestTenantId);

return borrowingTenantId;
return primaryRequestTenantId;
}

private Collection<String> getLendingTenants(EcsTlrEntity ecsTlr) {
private Collection<String> getSecondaryRequestTenants(EcsTlrEntity ecsTlr) {
final String instanceId = ecsTlr.getInstanceId().toString();
log.info("getLendingTenants:: looking for lending tenants for instance {}", instanceId);
List<String> tenantIds = tenantService.getLendingTenants(ecsTlr);
log.info("getSecondaryRequestTenants:: looking for secondary request tenants for instance {}", instanceId);
List<String> tenantIds = tenantService.getSecondaryRequestTenants(ecsTlr);
if (tenantIds.isEmpty()) {
log.error("getLendingTenants:: failed to find lending tenants for instance: {}", instanceId);
throw new TenantPickingException("Failed to find lending tenants for instance " + instanceId);
log.error("getSecondaryRequestTenants:: failed to find lending tenants for instance: {}", instanceId);
throw new TenantPickingException("Failed to find secondary request tenants for instance " + instanceId);
}

log.info("getLendingTenants:: lending tenants found: {}", tenantIds);
log.info("getSecondaryRequestTenants:: secondary request tenants found: {}", tenantIds);
return tenantIds;
}

Expand Down Expand Up @@ -159,13 +190,29 @@ private static void updateEcsTlr(EcsTlrEntity ecsTlr, RequestWrapper primaryRequ
log.debug("updateEcsTlr:: ECS TLR: {}", () -> ecsTlr);
}

private void createDcbTransactions(EcsTlrEntity ecsTlr, Request secondaryRequest) {
private void createDcbTransactions(EcsTlrEntity ecsTlr, Request secondaryRequest,
Request intermediateRequest, String centralTenantId) {

if (secondaryRequest.getItemId() == null) {
log.info("createDcbTransactions:: secondary request has no item ID");
return;
}
dcbService.createBorrowingTransaction(ecsTlr, secondaryRequest);
dcbService.createLendingTransaction(ecsTlr);

if (intermediateRequest == null) {
log.info("createDcbTransactions:: intermediateRequest is null");
dcbService.createBorrowingTransaction(ecsTlr, secondaryRequest, ecsTlr.getPrimaryRequestTenantId());
log.info("createDcbTransactions:: Creating lending transaction");
dcbService.createLendingTransaction(ecsTlr);
} else {
log.info("createDcbTransactions:: intermediateRequest is not null. " +
"Creating borrowing transaction in the central tenant");
dcbService.createBorrowingTransaction(ecsTlr, secondaryRequest, centralTenantId);
log.info("createDcbTransactions:: Creating lending transaction");
dcbService.createLendingTransaction(ecsTlr);
log.info("createDcbTransactions:: Creating pickup transaction in tenant {}",
ecsTlr.getPrimaryRequestTenantId());
dcbService.createPickupTransaction(ecsTlr, secondaryRequest, ecsTlr.getPrimaryRequestTenantId());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ private void processItemIdUpdate(EcsTlrEntity ecsTlr, Request updatedRequest) {
log.info("processItemIdUpdate:: updating ECS TLR {} with itemId {}", ecsTlr::getId,
updatedRequest::getItemId);
ecsTlr.setItemId(UUID.fromString(updatedRequest.getItemId()));
// TODO: change this if Page request works
dcbService.createLendingTransaction(ecsTlr);
dcbService.createBorrowingTransaction(ecsTlr, updatedRequest);
dcbService.createBorrowingTransaction(ecsTlr, updatedRequest, ecsTlr.getPrimaryRequestTenantId());
ecsTlrRepository.save(ecsTlr);
log.info("processItemIdUpdate: ECS TLR {} is updated", ecsTlr::getId);
}
Expand Down
Loading

0 comments on commit 307ef4c

Please sign in to comment.