diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java index aa99cbd731b..1e3339cc1de 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java @@ -145,6 +145,10 @@ List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, Stri @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 AND l.langCode=?3 AND NOT code=?4") List findByNameAndLevelLangCodeNotCode(String name,Short hierarchyLevel, String langCode, String code); + @Query("FROM Location l WHERE l.name=?1 AND l.parentLocCode=?5 AND l.hierarchyLevel=?2 AND l.langCode=?3 AND NOT code=?4") + List findByNameParentlocCodeAndLevelLangCodeNotCode(String name,Short hierarchyLevel, String langCode, String code,String parentLocCode); + + /** * give list of the immediate Locations for the given parent location code * diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java index 64416859674..3341314f5da 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java @@ -313,8 +313,10 @@ public LocationPutResponseDto updateLocationDetails(LocationDto locationDto) { throw new RequestException(LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorCode(), LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorMessage()); } - List list = locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), - locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()); + List list = (null==locationDto.getParentLocCode() || locationDto.getParentLocCode().isEmpty())? locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()):locationRepository.findByNameParentlocCodeAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode(),locationDto.getParentLocCode()); + if (list != null && !list.isEmpty()) { auditUtil.auditRequest( String.format(MasterDataConstant.FAILURE_CREATE, LocationDto.class.getSimpleName()), diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java index ec0a59c4581..654c01dc9ed 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java @@ -245,6 +245,7 @@ public void updateLocationAlreadyExistsUnderHeirarchyExceptionTest() throws Exce String requestJson = mapper.writeValueAsString(request); when(repo.findLocationHierarchyByCodeAndLanguageCode(Mockito.any(), Mockito.any())) .thenReturn(Arrays.asList(location1)); + when(repo.findByNameParentlocCodeAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any())).thenReturn(Arrays.asList(location1)); when(repo.findByNameAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(location1)); mockMvc.perform(put("/locations").contentType(MediaType.APPLICATION_JSON).content(requestJson)) .andExpect(status().isOk());