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 18, 2023
1 parent 9c00a4f commit cb6ab68
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.folio.spring.config.FeignClientConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -12,4 +13,7 @@ public interface UserTenantsClient {

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
void postUserTenant(@RequestBody UserTenant userTenant);

@DeleteMapping
void deleteUserTenants();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ public abstract class AbstractTenantEntity extends AuditableEntity {
private String name;
private UUID consortiumId;
private Boolean isCentral;
private Boolean isDeleted;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AbstractTenantEntity that)) return false;
return Objects.equals(id, that.id) && Objects.equals(code, that.code) && Objects.equals(name, that.name) && Objects.equals(consortiumId, that.consortiumId) && Objects.equals(isCentral, that.isCentral);
return Objects.equals(id, that.id) && Objects.equals(code, that.code) &&
Objects.equals(name, that.name) && Objects.equals(consortiumId, that.consortiumId) &&
Objects.equals(isCentral, that.isCentral) && Objects.equals(isDeleted, that.isDeleted);
}

@Override
public int hashCode() {
return Objects.hash(id, code, name, consortiumId, isCentral);
return Objects.hash(id, code, name, consortiumId, isCentral, isDeleted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
@Repository
public interface TenantRepository extends JpaRepository<TenantEntity, String> {

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

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

@Query("SELECT t FROM TenantEntity t where t.isCentral = true")
@Query("SELECT t FROM TenantEntity t WHERE t.isCentral = true")
Optional<TenantEntity> findCentralTenant();

boolean existsByIsCentralTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,27 @@ public void updateTenantSetupStatus(String tenantId, String centralTenantId, Set
@Transactional
public void delete(UUID consortiumId, String tenantId) {
consortiumService.checkConsortiumExistsOrThrow(consortiumId);
checkTenantExistsOrThrow(tenantId);
var tenant = tenantRepository.findById(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));
}

var softDeletedTenant = tenant.get();
softDeletedTenant.setIsDeleted(true);
// clean publish coordinator tables first, because after tenant removal it will be ignored by cleanup service
cleanupService.clearPublicationTables();
tenantRepository.deleteById(tenantId);
tenantRepository.save(softDeletedTenant);

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

private Tenant saveTenant(UUID consortiumId, Tenant tenantDto, SetupStatusEnum setupStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ 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().map(o -> converter.convert(o, UserTenant.class)).toList());
result.setUserTenants(userTenantPage.stream()
.filter(ut -> !ut.getTenant().getIsDeleted())
.map(o -> converter.convert(o, UserTenant.class)).toList());
result.setTotalRecords((int) userTenantPage.getTotalElements());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@
<column name="setup_status" type="setup_status"/>
</addColumn>
</changeSet>

<changeSet id="MOCON-127@add-is-deleted-flag" author="azizbekxm">
<addColumn tableName="tenant">
<column name="is_deleted" type="boolean" defaultValueBoolean="false">
<constraints unique="false" nullable="false"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>
2 changes: 2 additions & 0 deletions src/main/resources/swagger.api/schemas/tenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Tenant:
maxLength: 150
isCentral:
type: boolean
isDeleted:
type: boolean
additionalProperties: false
required:
- id
Expand Down

0 comments on commit cb6ab68

Please sign in to comment.