diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index 09057d8da..1162b3cd8 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -270,6 +270,7 @@ "invoice.invoice-lines.item.put", "invoices.acquisition-units.bypass.execute", "orders-storage.order-invoice-relationships.collection.get", + "orders-storage.settings.collection.get", "consortia.user-tenants.collection.get", "user-tenants.collection.get", "consortia.sharing-instances.item.post" @@ -2151,6 +2152,7 @@ "organizations-storage.organizations.collection.get", "organizations-storage.organizations.item.get", "isbn-utils.convert-to-13.get", + "orders-storage.settings.collection.get", "consortia.user-tenants.collection.get", "user-tenants.collection.get", "consortia.sharing-instances.collection.get", diff --git a/src/main/java/org/folio/service/consortium/ConsortiumConfigurationService.java b/src/main/java/org/folio/service/consortium/ConsortiumConfigurationService.java index 77fac29f3..4f097361a 100644 --- a/src/main/java/org/folio/service/consortium/ConsortiumConfigurationService.java +++ b/src/main/java/org/folio/service/consortium/ConsortiumConfigurationService.java @@ -95,12 +95,16 @@ public Future overrideContextToCentralTenantIfNeeded(RequestCont return Future.succeededFuture(requestContext); } RequestContext centralContext = createContextWithNewTenantId(requestContext, configuration.centralTenantId()); - return settingsRetriever.getSettingByKey(SettingKey.CENTRAL_ORDERING_ENABLED, centralContext) - .map(centralOrdering -> { - logger.info("overrideContextToCentralTenantIdNeeded:: central ordering enabled: {}", centralOrdering); - return centralOrdering.map(Setting::getValue).orElse(null); - }) - .compose(orderingEnabled -> Future.succeededFuture(Boolean.parseBoolean(orderingEnabled) ? centralContext : requestContext)); + return isCentralOrderingEnabled(requestContext) + .compose(isCentralOrderingEnabled -> Future.succeededFuture(isCentralOrderingEnabled ? centralContext : requestContext)); + }); + } + + public Future isCentralOrderingEnabled(RequestContext requestContext) { + return settingsRetriever.getSettingByKey(SettingKey.CENTRAL_ORDERING_ENABLED, requestContext) + .map(centralOrdering -> { + logger.info("isCentralOrderingEnabled:: central ordering enabled: {}", centralOrdering); + return centralOrdering.map(setting -> Boolean.parseBoolean(setting.getValue())).orElse(false); }); } } diff --git a/src/main/java/org/folio/service/orders/CompositePoLineValidationService.java b/src/main/java/org/folio/service/orders/CompositePoLineValidationService.java index 3af2be40b..13247bf16 100644 --- a/src/main/java/org/folio/service/orders/CompositePoLineValidationService.java +++ b/src/main/java/org/folio/service/orders/CompositePoLineValidationService.java @@ -366,7 +366,10 @@ private Future> getUserTenantsIfNeeded(RequestContext requestContex return consortiumConfigurationService.getConsortiumConfiguration(requestContext) .compose(consortiumConfiguration -> consortiumConfiguration - .map(configuration -> consortiumUserTenantsRetriever.getUserTenants(configuration.consortiumId(), configuration.centralTenantId(), requestContext)) + .map(configuration -> consortiumConfigurationService.isCentralOrderingEnabled(requestContext) + .compose(isCentralOrderingEnabled -> isCentralOrderingEnabled + ? consortiumUserTenantsRetriever.getUserTenants(configuration.consortiumId(), configuration.centralTenantId(), requestContext) + : Future.succeededFuture())) .orElse(Future.succeededFuture()) ); } diff --git a/src/test/java/org/folio/service/orders/CompositePoLineValidationServiceTest.java b/src/test/java/org/folio/service/orders/CompositePoLineValidationServiceTest.java index a63797e2a..8beed6e04 100644 --- a/src/test/java/org/folio/service/orders/CompositePoLineValidationServiceTest.java +++ b/src/test/java/org/folio/service/orders/CompositePoLineValidationServiceTest.java @@ -14,6 +14,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import java.util.List; @@ -411,6 +412,8 @@ void testValidateUserUnaffiliatedLocationsAllValidLocations(VertxTestContext tes when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext))) .thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId")))); + when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext))) + .thenReturn(Future.succeededFuture(true)); when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext))) .thenReturn(Future.succeededFuture(List.of("tenant1", "tenant2"))); @@ -428,6 +431,8 @@ void testValidateUserUnaffiliatedLocationsAllValidDuplicateTenantLocations(Vertx when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext))) .thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId")))); + when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext))) + .thenReturn(Future.succeededFuture(true)); when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext))) .thenReturn(Future.succeededFuture(List.of("tenant1", "tenant2"))); @@ -435,6 +440,27 @@ void testValidateUserUnaffiliatedLocationsAllValidDuplicateTenantLocations(Vertx .onComplete(testContext.succeeding(result -> testContext.verify(testContext::completeNow))); } + @Test + void testValidateUserUnaffiliatedLocationsWhenCentralOrderingIsDisabled(VertxTestContext testContext) { + var locationsUpdated = List.of( + new Location().withLocationId(UUID.randomUUID().toString()).withTenantId("tenant1"), + new Location().withLocationId(UUID.randomUUID().toString()).withTenantId("tenant2")); + var updatedPoLineId = UUID.randomUUID().toString(); + + when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext))) + .thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId")))); + when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext))) + .thenReturn(Future.succeededFuture(false)); + when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext))) + .thenReturn(Future.succeededFuture(List.of("tenant1"))); + + compositePoLineValidationService.validateUserUnaffiliatedLocations(updatedPoLineId, locationsUpdated, requestContext) + .onComplete(testContext.succeeding(result -> { + testContext.verify(testContext::completeNow); + verifyNoInteractions(consortiumUserTenantsRetriever); + })); + } + @Test void testValidateUserUnaffiliatedLocationsOneValidAndOneInvalidLocations(VertxTestContext testContext) { var locationsUpdated = List.of( @@ -444,6 +470,8 @@ void testValidateUserUnaffiliatedLocationsOneValidAndOneInvalidLocations(VertxTe when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext))) .thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId")))); + when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext))) + .thenReturn(Future.succeededFuture(true)); when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext))) .thenReturn(Future.succeededFuture(List.of("tenant1"))); @@ -464,6 +492,8 @@ void testValidateUserUnaffiliatedLocationsTwoInvalidLocations(VertxTestContext t when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext))) .thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId")))); + when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext))) + .thenReturn(Future.succeededFuture(true)); when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext))) .thenReturn(Future.succeededFuture(List.of("tenant3")));