Skip to content

Commit

Permalink
Add tenant deletion functionality (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
saschadoemer authored May 21, 2024
1 parent 4eef629 commit d17bb5a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/de/app/fivegla/business/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public Optional<Tenant> findTenantByName(String name) {
return tenantRepository.getTenant(name);
}

/**
* Deletes a tenant by its tenantId.
* @param tenantId The tenantId of the tenant to delete.
*/
public void delete(String tenantId) {
tenantRepository.deleteTenant(tenantId);
}

/**
* Represents a combination of Tenant object and an access token.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,31 @@ public ResponseEntity<? extends Response> findAll() {
return ResponseEntity.ok(ReadTenantsResponse.builder().tenants(tenants).build());
}

@Operation(
operationId = "tenant.delete",
description = "Delete a tenant based on the provided request.",
tags = BaseMappings.TENANT
)
@ApiResponse(
responseCode = "204",
description = "The tenant was deleted successfully.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@ApiResponse(
responseCode = "400",
description = "The request is invalid.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@DeleteMapping(value = "/{tenantId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<? extends Response> delete(@PathVariable String tenantId) {
tenantService.delete(tenantId);
return ResponseEntity.noContent().build();
}

}
32 changes: 32 additions & 0 deletions src/main/java/de/app/fivegla/persistence/TenantRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import de.app.fivegla.api.exceptions.BusinessException;
import de.app.fivegla.persistence.entity.Tenant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Component
@RequiredArgsConstructor
public class TenantRepository {
Expand Down Expand Up @@ -85,4 +87,34 @@ public Optional<Tenant> getTenant(String tenantId) {
public List<Tenant> findTenants() {
return applicationData.getTenants();
}


/**
* Deletes a tenant with the specified tenant ID.
*
* @param tenantId The ID of the tenant to delete.
*/
public void deleteTenant(String tenantId) {
if (null != applicationData.getTenants()) {
log.info("Deleting tenant with tenantId: {}", tenantId);
applicationData.setTenants(applicationData.getTenants().stream()
.filter(t -> !t.getTenantId().equals(tenantId))
.toList());
applicationData.persist();
}
if (null != applicationData.getGroups()) {
log.info("Deleting groups for tenant with tenantId: {}", tenantId);
applicationData.setGroups(applicationData.getGroups().stream()
.filter(g -> !g.getTenant().getTenantId().equals(tenantId))
.toList());
applicationData.persist();
}
if (null != applicationData.getThirdPartyApiConfigurations()) {
log.info("Deleting third party API configurations for tenant with tenantId: {}", tenantId);
applicationData.setThirdPartyApiConfigurations(applicationData.getThirdPartyApiConfigurations().stream()
.filter(t -> !t.getTenantId().equals(tenantId))
.toList());
applicationData.persist();
}
}
}

0 comments on commit d17bb5a

Please sign in to comment.