Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODDCB-156 Implement the code for the feature UXPROD-5005 #140

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/folio/dcb/client/feign/UsersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.folio.spring.config.FeignClientConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

Expand All @@ -16,4 +18,7 @@ public interface UsersClient {

@GetMapping
UserCollection fetchUserByBarcodeAndId(@RequestParam("query") String query);

@PutMapping("/{userId}")
void updateUser(@PathVariable("userId") String userId, @RequestBody User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ public void saveDcbTransaction(String dcbTransactionId, DcbTransaction dcbTransa
transactionRepository.save(transactionEntity);
}

public void checkUserTypeAndThrowIfMismatch(String userType) {
if(ObjectUtils.notEqual(userType, DCB_TYPE) && ObjectUtils.notEqual(userType, SHADOW_TYPE)) {
throw new IllegalArgumentException(String.format("User with type %s is retrieved. so unable to create transaction", userType));
}
}

public void updateTransactionStatus(TransactionEntity dcbTransaction, TransactionStatus transactionStatus) {
log.debug("updateTransactionStatus:: Updating dcbTransaction {} to status {} ", dcbTransaction, transactionStatus);
var currentStatus = dcbTransaction.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public TransactionStatusResponse createCirculation(String dcbTransactionId, DcbT
var patron = dcbTransaction.getPatron();

var user = userService.fetchOrCreateUser(patron);
baseLibraryService.checkUserTypeAndThrowIfMismatch(user.getType());
ServicePointRequest pickupServicePoint = servicePointService.createServicePointIfNotExists(dcbTransaction.getPickup());
dcbTransaction.getPickup().setServicePointId(pickupServicePoint.getId());
CirculationRequest pageRequest = requestService.createRequestBasedOnItemStatus(user, item, pickupServicePoint.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public TransactionStatusResponse createCirculation(String dcbTransactionId, DcbT
var patron = dcbTransaction.getPatron();

var user = userService.fetchOrCreateUser(patron);
baseLibraryService.checkUserTypeAndThrowIfMismatch(user.getType());
baseLibraryService.checkItemExistsInInventoryAndThrow(itemVirtual.getBarcode());
CirculationItem item = circulationItemService.checkIfItemExistsAndCreate(itemVirtual, dcbTransaction.getPickup().getServicePointId());
dcbTransaction.getItem().setId(item.getId());
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/folio/dcb/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.folio.dcb.client.feign.UsersClient;
import org.folio.dcb.domain.dto.DcbPatron;
import org.folio.dcb.domain.dto.Personal;
Expand All @@ -15,6 +16,7 @@
import java.util.Objects;

import static org.folio.dcb.utils.DCBConstants.DCB_TYPE;
import static org.folio.dcb.utils.DCBConstants.SHADOW_TYPE;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -48,9 +50,15 @@ public User fetchOrCreateUser(DcbPatron patronDetails) {
if(Objects.isNull(user)) {
log.info("fetchOrCreateUser:: Unable to find existing user with barcode {} and id {}. Hence, creating new user",
patronDetails.getBarcode(), patronDetails.getId());
user = createUser(patronDetails);
return createUser(patronDetails);
} else {
validateDcbUserType(user.getType());
var groupId = patronGroupService.fetchPatronGroupIdByName(patronDetails.getGroup());
if (!groupId.equals(user.getPatronGroup())) {
return updateUserGroup(user, groupId);
}
return user;
}
return user;
}

private User createUser(DcbPatron patronDetails) {
Expand Down Expand Up @@ -80,4 +88,19 @@ private User createVirtualUser(DcbPatron patron, String groupId) {
.personal(Personal.builder().lastName(LAST_NAME).build())
.build();
}

private User updateUserGroup(User user, String patronGroupId) {
log.info("updatePatronGroup:: updating patron group from {} to {} for user with barcode {}",
user.getPatronGroup(), patronGroupId, user.getBarcode());
user.setPatronGroup(patronGroupId);
usersClient.updateUser(user.getId(), user);
return user;
}

private void validateDcbUserType(String userType) {
if(ObjectUtils.notEqual(userType, DCB_TYPE) && ObjectUtils.notEqual(userType, SHADOW_TYPE)) {
throw new IllegalArgumentException(String.format("User with type %s is retrieved. so unable to create transaction", userType));
}
}

}
11 changes: 0 additions & 11 deletions src/test/java/org/folio/dcb/service/BaseLibraryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,6 @@ void createBorrowingTransactionTestThrowException() {
assertThrows(IllegalArgumentException.class, () -> baseLibraryService.createBorrowingLibraryTransaction(DCB_TRANSACTION_ID, transaction, PICKUP_SERVICE_POINT_ID));
}

@Test
void checkUserTypeAndThrowIfMismatchTest() {
var user = createUser();
baseLibraryService.checkUserTypeAndThrowIfMismatch(user.getType());

user.setType("shadow");
baseLibraryService.checkUserTypeAndThrowIfMismatch(user.getType());

assertThrows(IllegalArgumentException.class, () -> baseLibraryService.checkUserTypeAndThrowIfMismatch("patron"));
}

@Test
void testTransactionCancelTest(){
var transactionEntity = createTransactionEntity();
Expand Down
27 changes: 24 additions & 3 deletions src/test/java/org/folio/dcb/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,32 @@ class UserServiceTest {
private PatronGroupServiceImpl patronGroupService;

@Test
void fetchUserTest() {
when(usersClient.fetchUserByBarcodeAndId(any())).thenReturn(createUserCollection());
void fetchUserTestWithSameGroup() {
var userCollection = createUserCollection();
var groupId = UUID.randomUUID().toString();
userCollection.getUsers().get(0).setPatronGroup(groupId);
when(usersClient.fetchUserByBarcodeAndId(any())).thenReturn(userCollection);
when(patronGroupService.fetchPatronGroupIdByName("staff"))
.thenReturn(groupId);
userService.fetchOrCreateUser(createDefaultDcbPatron());
verify(usersClient).fetchUserByBarcodeAndId(any());
verify(patronGroupService).fetchPatronGroupIdByName("staff");
verify(usersClient, never()).updateUser(any(), any());
verify(usersClient, never()).createUser(any());
}

@Test
void fetchUserTestWithDifferentGroup() {
var userCollection = createUserCollection();
var groupId = UUID.randomUUID().toString();
userCollection.getUsers().get(0).setPatronGroup(groupId);
when(usersClient.fetchUserByBarcodeAndId(any())).thenReturn(userCollection);
when(patronGroupService.fetchPatronGroupIdByName("staff"))
.thenReturn(UUID.randomUUID().toString());
userService.fetchOrCreateUser(createDefaultDcbPatron());
verify(usersClient).fetchUserByBarcodeAndId(any());
verify(patronGroupService, never()).fetchPatronGroupIdByName("staff");
verify(patronGroupService).fetchPatronGroupIdByName("staff");
verify(usersClient).updateUser(any(), any());
verify(usersClient, never()).createUser(any());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/mappings/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"response": {
"status": 200,
"body": "{\"users\": [{\"username\": \"leopold\",\n \"id\": \"910c512c-ebc5-40c6-96a5-a20bfd81e154\",\n \"barcode\": \"DCB_PATRON\",\n \"active\": true,\n \"type\": \"dcb\" }], \"totalRecords\": 1, \"resultInfo\": {\"totalRecords\": 1, \"facets\": [],\"diagnostics\": []}}",
"body": "{\"users\": [{\"username\": \"leopold\",\n \"patronGroup\":\"3684a786-6671-4268-8ed0-9db82ebca60b\", \"id\": \"910c512c-ebc5-40c6-96a5-a20bfd81e154\",\n \"barcode\": \"DCB_PATRON\",\n \"active\": true,\n \"type\": \"dcb\" }], \"totalRecords\": 1, \"resultInfo\": {\"totalRecords\": 1, \"facets\": [],\"diagnostics\": []}}",
"headers": {
"Content-Type": "application/json"
}
Expand Down
Loading