Skip to content

Commit

Permalink
[MODCON-130] - Adjust process to add ECS tenant with soft deleted fun…
Browse files Browse the repository at this point in the history
…ctionality
  • Loading branch information
azizbekxm committed Dec 21, 2023
1 parent 472ee38 commit 7e9ff61
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/folio/consortia/service/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public interface TenantService {

/**
* Inserts single tenant based on consortiumId.
* Method checks whether requesting tenant is soft deleted or new tenant.
* For re-adding soft deleted tenant,
* tenant is_deleted flag will be changed to false and dummy user will be created in mod_users.user-tenants table
* For new tenant, all necessary actions will be done.
*
* @param consortiumId the consortiumId
* @param tenantDto the tenantDto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,46 @@ public TenantEntity getByTenantId(String tenantId) {
@Override
@Transactional
public Tenant save(UUID consortiumId, UUID adminUserId, Tenant tenantDto) {
log.info("save:: Trying to save a tenant by consortiumId '{}', tenant object with id '{}' and isCentral={}", consortiumId,
tenantDto.getId(), tenantDto.getIsCentral());
log.info("save:: Trying to save a tenant with id={}, consortiumId={} and isCentral={}", tenantDto.getId(),
consortiumId, tenantDto.getIsCentral());

// validation part
checkTenantNotExistsAndConsortiumExistsOrThrow(consortiumId, tenantDto.getId());
checkCodeAndNameUniqueness(tenantDto);
if (tenantDto.getIsCentral()) {
checkCentralTenantExistsOrThrow();
validateCodeAndNameUniqueness(tenantDto);
validateConsortiumAndTenant(consortiumId, tenantDto);

var requestingTenant = tenantRepository.findById(tenantDto.getId());

// checked whether tenant exists or not.
return requestingTenant.isPresent() ? reAddSoftDeletedTenant(consortiumId, tenantDto)
: addNewTenant(consortiumId, tenantDto, adminUserId);
}

private Tenant reAddSoftDeletedTenant(UUID consortiumId, Tenant tenantDto) {
log.info("reAddSoftDeletedTenant:: Re-adding soft deleted tenant with id={}", tenantDto.getId());
validateExistTenant(tenantDto);

tenantDto.setIsDeleted(false);
lockService.lockTenantSetupWithinTransaction();
var savedTenant = saveTenant(consortiumId, tenantDto, SetupStatusEnum.IN_PROGRESS);

String centralTentId = getCentralTenantId();
try {
createUserTenantWithDummyUser(tenantDto.getId(), centralTentId, consortiumId);
log.info("reAddSoftDeletedTenant:: Dummy user re-created in user-tenants table");
updateTenantSetupStatus(tenantDto.getId(), centralTentId, SetupStatusEnum.COMPLETED);
} catch (Exception e) {
log.error("Failed to create dummy user with centralTenantId: {}, tenant: {}" +
" and error message: {}", centralTentId, tenantDto.getId(), e.getMessage(), e);
updateTenantSetupStatus(tenantDto.getId(), centralTentId, SetupStatusEnum.COMPLETED_WITH_ERRORS);
}

// save tenant to db
return savedTenant;
}

private Tenant addNewTenant(UUID consortiumId, Tenant tenantDto, UUID adminUserId) {
log.info("addNewTenant:: Creating new tenant with id={}, consortiumId={}, and adminUserId={}",
tenantDto.getId(), consortiumId, adminUserId);

lockService.lockTenantSetupWithinTransaction();
Tenant savedTenant = saveTenant(consortiumId, tenantDto, SetupStatusEnum.IN_PROGRESS);

Expand All @@ -147,7 +176,7 @@ public Tenant save(UUID consortiumId, UUID adminUserId, Tenant tenantDto) {
userTenantRepository.save(createUserTenantEntity(consortiumId, shadowAdminUser, tenantDto));
// creating shadow user of consortia system user of central tenant with same permissions.
var centralSystemUser = userService.getByUsername(systemUserUsername)
.orElseThrow(() -> new ResourceNotFoundException("systemUserUsername", systemUserUsername));
.orElseThrow(() -> new ResourceNotFoundException("systemUserUsername", systemUserUsername));
shadowSystemUser = userService.prepareShadowUser(UUID.fromString(centralSystemUser.getId()), folioExecutionContext.getTenantId());
userTenantRepository.save(createUserTenantEntity(consortiumId, shadowSystemUser, tenantDto));
}
Expand Down Expand Up @@ -180,7 +209,7 @@ public Tenant update(UUID consortiumId, String tenantId, Tenant tenantDto) {
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateTenantSetupStatus(String tenantId, String centralTenantId, SetupStatusEnum setupStatus) {
try (var ctx = new FolioExecutionContextSetter(prepareContextForTenant(centralTenantId,
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(centralTenantId,
folioExecutionContext.getFolioModuleMetadata(), folioExecutionContext))) {
tenantDetailsRepository.setSetupStatusByTenantId(setupStatus, tenantId);
log.info("updateTenantSetupStatus:: tenant id={} status updated to {}", tenantId, setupStatus);
Expand Down Expand Up @@ -250,14 +279,7 @@ private void createUserTenantWithDummyUser(String tenantId, String centralTenant
userTenantsClient.postUserTenant(userTenant);
}

private void checkTenantNotExistsAndConsortiumExistsOrThrow(UUID consortiumId, String tenantId) {
consortiumService.checkConsortiumExistsOrThrow(consortiumId);
if (tenantRepository.existsById(tenantId)) {
throw new ResourceAlreadyExistException("id", tenantId);
}
}

private void checkCodeAndNameUniqueness(Tenant tenant) {
private void validateCodeAndNameUniqueness(Tenant tenant) {
if (tenantRepository.existsByName(tenant.getName())) {
throw new ResourceAlreadyExistException("name", tenant.getName());
}
Expand Down Expand Up @@ -289,12 +311,19 @@ public void checkTenantsAndConsortiumExistsOrThrow(UUID consortiumId, List<Strin
}
}

private void checkCentralTenantExistsOrThrow() {
if (tenantRepository.existsByIsCentralTrue()) {
private void validateConsortiumAndTenant(UUID consortiumId, Tenant tenantDto) {
consortiumService.checkConsortiumExistsOrThrow(consortiumId);
if (tenantDto.getIsCentral() && tenantRepository.existsByIsCentralTrue()) {
throw new ResourceAlreadyExistException("isCentral", "true");
}
}

private void validateExistTenant(Tenant tenant) {
if (Boolean.TRUE.equals(tenant.getIsCentral()) || Boolean.FALSE.equals(tenant.getIsDeleted())) {
throw new ResourceAlreadyExistException("id", tenant.getId());
}
}

private void checkAdminUserIdPresentOrThrow(UUID adminUserId) {
if (Objects.isNull(adminUserId)) {
log.warn("checkAdminUserIdPresentOrThrow:: adminUserId is not present");
Expand Down

0 comments on commit 7e9ff61

Please sign in to comment.