Skip to content

Commit

Permalink
[MODCON-127] - Implement ECS tenant soft deletion functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Dec 19, 2023
1 parent b109ee4 commit eb949c1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Tenant convert(TenantEntity source) {
tenant.setCode(source.getCode());
tenant.setName(source.getName());
tenant.setIsCentral(source.getIsCentral());
tenant.setIsDeleted(source.getIsDeleted());
return tenant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
@Repository
public interface TenantRepository extends JpaRepository<TenantEntity, String> {

@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and t.isDeleted = false")
@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and (t.isDeleted IS NULL OR t.isDeleted = FALSE)")
Page<TenantEntity> findByConsortiumId(UUID consortiumId, Pageable pageable);

@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and t.isDeleted = false")
@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and (t.isDeleted IS NULL OR t.isDeleted = FALSE)")
List<TenantEntity> findByConsortiumId(UUID consortiumId);

@Query("SELECT t FROM TenantEntity t WHERE t.isCentral = true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

@Repository
public interface UserTenantRepository extends JpaRepository<UserTenantEntity, UUID> {

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.tenant.isDeleted IS NULL OR ut.tenant.isDeleted = FALSE")
Page<UserTenantEntity> getAll(Pageable pageable);

Page<UserTenantEntity> findByUserId(UUID userId, Pageable pageable);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.username= ?1 AND ut.tenant.id= ?2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public class TenantServiceImpl implements TenantService {
private static final String SHADOW_ADMIN_PERMISSION_FILE_PATH = "permissions/admin-user-permissions.csv";
private static final String SHADOW_SYSTEM_USER_PERMISSION_FILE_PATH = "permissions/system-user-permissions.csv";
private static final String TENANTS_IDS_NOT_MATCHED_ERROR_MSG = "Request body tenantId and path param tenantId should be identical";
private static final String TENANT_HAS_ACTIVE_USER_ASSOCIATIONS_ERROR_MSG = "Cannot delete tenant with ID {tenantId} because it has an association with a user. "
+ "Please remove the user association before attempting to delete the tenant.";

private static final String DUMMY_USERNAME = "dummy_user";
@Value("${folio.system-user.username}")
private String systemUserUsername;
Expand Down Expand Up @@ -91,8 +90,7 @@ public TenantCollection get(UUID consortiumId, Integer offset, Integer limit) {
public TenantCollection getAll(UUID consortiumId) {
TenantCollection result = new TenantCollection();
List<Tenant> list = tenantRepository.findByConsortiumId(consortiumId)
.stream()
.map(o -> converter.convert(o, Tenant.class)).toList();
.stream().map(o -> converter.convert(o, Tenant.class)).toList();
result.setTenants(list);
result.setTotalRecords(list.size());
return result;
Expand Down Expand Up @@ -198,9 +196,6 @@ public void delete(UUID consortiumId, String tenantId) {
if (tenant.isEmpty()) {
throw new ResourceNotFoundException("id", tenantId);
}
if (userTenantRepository.existsByTenantId(tenantId)) {
throw new IllegalArgumentException(TENANT_HAS_ACTIVE_USER_ASSOCIATIONS_ERROR_MSG);
}
if (tenant.get().getIsCentral()) {
throw new IllegalArgumentException(String.format("central tenant %s cannot be deleted", tenantId));
}
Expand All @@ -211,9 +206,9 @@ public void delete(UUID consortiumId, String tenantId) {
cleanupService.clearPublicationTables();
tenantRepository.save(softDeletedTenant);

try (var ignored = new FolioExecutionContextSetter(contextHelper.getSystemUserFolioExecutionContext(tenantId))) {
userTenantsClient.deleteUserTenants();
}
// try (var ignored = new FolioExecutionContextSetter(contextHelper.getSystemUserFolioExecutionContext(tenantId))) {
// userTenantsClient.deleteUserTenants();
// }
}

private Tenant saveTenant(UUID consortiumId, Tenant tenantDto, SetupStatusEnum setupStatus) {
Expand Down Expand Up @@ -314,6 +309,7 @@ private TenantEntity toTenantEntity(UUID consortiumId, Tenant tenantDto) {
entity.setCode(tenantDto.getCode());
entity.setIsCentral(tenantDto.getIsCentral());
entity.setConsortiumId(consortiumId);
entity.setIsDeleted(tenantDto.getIsDeleted());
return entity;
}

Expand All @@ -325,6 +321,7 @@ private TenantDetailsEntity toTenantDetailsEntity(UUID consortiumId, Tenant tena
entity.setIsCentral(tenantDto.getIsCentral());
entity.setConsortiumId(consortiumId);
entity.setSetupStatus(setupStatus);
entity.setIsDeleted(tenantDto.getIsDeleted());
return entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ public class UserTenantServiceImpl implements UserTenantService {
public UserTenantCollection get(UUID consortiumId, Integer offset, Integer limit) {
consortiumService.checkConsortiumExistsOrThrow(consortiumId);
var result = new UserTenantCollection();
Page<UserTenantEntity> userTenantPage = userTenantRepository.findAll(PageRequest.of(offset, limit));
result.setUserTenants(userTenantPage.stream()
.filter(ut -> !ut.getTenant().getIsDeleted())
.map(o -> converter.convert(o, UserTenant.class)).toList());
Page<UserTenantEntity> userTenantPage = userTenantRepository.getAll(PageRequest.of(offset, limit));
result.setUserTenants(userTenantPage.map(o -> converter.convert(o, UserTenant.class)).getContent());
result.setTotalRecords((int) userTenantPage.getTotalElements());
return result;
}
Expand Down

0 comments on commit eb949c1

Please sign in to comment.