Skip to content

Commit

Permalink
MODCON-88 Save setupStatus for Tenant and add GET endpoint to return …
Browse files Browse the repository at this point in the history
…tenant details with setup status (#91)

* MODCON-88 Add setupStatus to tenant table and add GET endpoint which returns tenant details containing setup status

* MODCON-88 refactor

* MODCON-88 add tests

* MODCON-88 fix codesmell

* MODCON-88 add unit test

* MODCON-88 formatting and logging minor updates

* MODCON-88 add logging and test

* MODCON-88 add test

* MODCON-88 remove COMPLETED_WITH_ERRORS status

* MODCON-88 minor rename

* MODCON-88 move @transactional to service

* MODCON-88 address review comments

* MODCON-88 fix type

* MODCON-88 remove not used imports

* MODCON-88 update exception
  • Loading branch information
tatsiana-tarhonskaya authored Aug 18, 2023
1 parent c5a2df7 commit da159ff
Show file tree
Hide file tree
Showing 20 changed files with 512 additions and 80 deletions.
16 changes: 16 additions & 0 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
],
"modulePermissions": []
},
{
"methods": [
"GET"
],
"pathPattern": "/consortia/{consortiumId}/tenants/{tenantId}",
"permissionsRequired": [
"consortia.tenants.item.get"
],
"modulePermissions": []
},
{
"methods": [
"GET"
Expand Down Expand Up @@ -354,6 +364,7 @@
"consortia.tenants.item.post",
"consortia.tenants.item.put",
"consortia.tenants.item.delete",
"consortia.tenants.item.get",
"consortia.user-tenants.collection.get",
"consortia.user-tenants.item.get",
"consortia.consortium.item.post",
Expand Down Expand Up @@ -395,6 +406,11 @@
"displayName": "delete tenant",
"description": "Delete tenant"
},
{
"permissionName": "consortia.tenants.item.get",
"displayName": "get tenant details",
"description": "Get tenant details"
},
{
"permissionName": "consortia.sync-primary-affiliations.item.post",
"displayName": "synchronize consortia primary affiliations",
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/folio/consortia/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.folio.consortia.config;

import org.folio.consortia.domain.converter.ConsortiumConverter;
import org.folio.consortia.domain.converter.TenantConverter;
import org.folio.consortia.domain.converter.TenantEntityToTenantConverter;
import org.folio.consortia.domain.converter.UserTenantConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -24,7 +24,7 @@ public class AppConfig implements WebMvcConfigurer {

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new TenantConverter());
registry.addConverter(new TenantEntityToTenantConverter());
registry.addConverter(new UserTenantConverter());
registry.addConverter(new ConsortiumConverter());
}
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/org/folio/consortia/controller/TenantController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.folio.consortia.domain.dto.SyncPrimaryAffiliationBody;
import org.folio.consortia.domain.dto.Tenant;
import org.folio.consortia.domain.dto.TenantCollection;
import org.folio.consortia.domain.dto.TenantDetails;
import org.folio.consortia.domain.dto.TenantDetails.SetupStatusEnum;
import org.folio.consortia.rest.resource.TenantsApi;
import org.folio.consortia.service.SyncPrimaryAffiliationService;
import org.folio.consortia.service.TenantService;
Expand All @@ -24,9 +26,11 @@
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

@RestController
@RequestMapping("/consortia/{consortiumId}")
@Log4j2
@RequiredArgsConstructor
public class TenantController implements TenantsApi {

Expand Down Expand Up @@ -55,19 +59,36 @@ public ResponseEntity<Void> deleteTenantById(UUID consortiumId, String tenantId)
return ResponseEntity.status(NO_CONTENT).build();
}

@Override
public ResponseEntity<TenantDetails> getTenantDetailsById(UUID consortiumId, String tenantId) {
return ResponseEntity.ok(service.getTenantDetailsById(consortiumId, tenantId));
}

@Override
public ResponseEntity<Void> syncPrimaryAffiliations(UUID consortiumId, String tenantId, @NotNull String centralTenantId) {
asyncTaskExecutor.execute(getRunnableWithCurrentFolioContext(
() -> syncPrimaryAffiliationService.syncPrimaryAffiliations(consortiumId, tenantId, centralTenantId)));
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
try {
asyncTaskExecutor.execute(getRunnableWithCurrentFolioContext(
() -> syncPrimaryAffiliationService.syncPrimaryAffiliations(consortiumId, tenantId, centralTenantId)));
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
log.error("syncPrimaryAffiliations:: error syncing user primary affiliations", e);
service.updateTenantSetupStatus(tenantId, centralTenantId, SetupStatusEnum.FAILED);
throw e;
}
}

@Override
public ResponseEntity<Void> createPrimaryAffiliations(UUID consortiumId, String tenantId, @NotNull String centralTenantId,
SyncPrimaryAffiliationBody syncPrimaryAffiliationBody) {
var context = prepareContextForTenant(centralTenantId, folioExecutionContext.getFolioModuleMetadata(), folioExecutionContext);
asyncTaskExecutor.execute(getRunnableWithFolioContext(context,
() -> syncPrimaryAffiliationService.createPrimaryUserAffiliations(consortiumId, centralTenantId, syncPrimaryAffiliationBody)));
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
try {
var context = prepareContextForTenant(centralTenantId, folioExecutionContext.getFolioModuleMetadata(), folioExecutionContext);
asyncTaskExecutor.execute(getRunnableWithFolioContext(context,
() -> syncPrimaryAffiliationService.createPrimaryUserAffiliations(consortiumId, centralTenantId, syncPrimaryAffiliationBody)));
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
log.error("createPrimaryAffiliations:: error creating user primary affiliations", e);
service.updateTenantSetupStatus(tenantId, centralTenantId, SetupStatusEnum.FAILED);
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.folio.consortia.domain.converter;

import org.folio.consortia.domain.dto.Tenant;
import org.folio.consortia.domain.entity.TenantDetailsEntity;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class TenantDetailsEntityToTenantConverter implements Converter<TenantDetailsEntity, Tenant> {
@Override
public Tenant convert(TenantDetailsEntity source) {
Tenant tenant = new Tenant();
tenant.setId(source.getId());
tenant.setCode(source.getCode());
tenant.setName(source.getName());
tenant.setIsCentral(source.getIsCentral());
return tenant;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.folio.consortia.domain.converter;

import org.folio.consortia.domain.dto.TenantDetails;
import org.folio.consortia.domain.entity.TenantDetailsEntity;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class TenantDetailsEntityToTenantDetailsConverter implements Converter<TenantDetailsEntity, TenantDetails> {
@Override
public TenantDetails convert(TenantDetailsEntity source) {
return new TenantDetails()
.id(source.getId())
.code(source.getCode())
.name(source.getName())
.isCentral(source.getIsCentral())
.setupStatus(source.getSetupStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Component;

@Component
public class TenantConverter implements Converter<TenantEntity, Tenant> {
public class TenantEntityToTenantConverter implements Converter<TenantEntity, Tenant> {

@Override
public Tenant convert(TenantEntity source) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.folio.consortia.domain.entity;

import java.util.Objects;
import java.util.UUID;

import org.folio.consortia.domain.entity.base.AuditableEntity;

import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@MappedSuperclass
public abstract class AbstractTenantEntity extends AuditableEntity {
@Id
private String id;
private String code;
private String name;
private UUID consortiumId;
private Boolean isCentral;

@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);
}

@Override
public int hashCode() {
return Objects.hash(id, code, name, consortiumId, isCentral);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.folio.consortia.domain.entity;

import org.folio.consortia.domain.dto.TenantDetails;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(name = "tenant")
public class TenantDetailsEntity extends AbstractTenantEntity {
@Enumerated(EnumType.STRING)
private TenantDetails.SetupStatusEnum setupStatus;
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
package org.folio.consortia.domain.entity;

import java.util.Objects;
import java.util.UUID;

import org.folio.consortia.domain.entity.base.AuditableEntity;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "tenant")
public class TenantEntity extends AuditableEntity {
@Id
private String id;
private String code;
private String name;
private UUID consortiumId;
private Boolean isCentral;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TenantEntity 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);
}

@Override
public int hashCode() {
return Objects.hash(id, code, name, consortiumId, isCentral);
}
public class TenantEntity extends AbstractTenantEntity {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.folio.consortia.repository;

import org.folio.consortia.domain.dto.TenantDetails;
import org.folio.consortia.domain.entity.TenantDetailsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface TenantDetailsRepository extends JpaRepository<TenantDetailsEntity, String> {
@Modifying
@Query("UPDATE TenantDetailsEntity t SET t.setupStatus= ?1 WHERE t.id= ?2")
void setSetupStatusByTenantId(TenantDetails.SetupStatusEnum setupStatus, String tenantId);
}
20 changes: 20 additions & 0 deletions src/main/java/org/folio/consortia/service/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.folio.consortia.domain.dto.Tenant;
import org.folio.consortia.domain.dto.TenantCollection;
import org.folio.consortia.domain.dto.TenantDetails;
import org.folio.consortia.domain.dto.TenantDetails.SetupStatusEnum;
import org.folio.consortia.domain.entity.TenantEntity;
import org.folio.consortia.exception.ResourceNotFoundException;

Expand Down Expand Up @@ -47,6 +49,15 @@ public interface TenantService {
*/
Tenant update(UUID consortiumId, String tenantId, Tenant tenantDto);

/**
* Updates tenant's setup status.
*
* @param tenantId the tenantId
* @param centralTenantId the consortiumId
* @param setupStatus the setup status
*/
void updateTenantSetupStatus(String tenantId, String centralTenantId, SetupStatusEnum setupStatus);

/**
* Deletes single tenant based on consortiumId.
* @param consortiumId the consortiumId
Expand All @@ -62,6 +73,15 @@ public interface TenantService {
*/
TenantEntity getByTenantId(String tenantId);

/**
* Gets tenant details based on tenantId.
*
* @param consortiumId the consortiumId
* @param tenantId the tenantId
* @return tenant details
*/
TenantDetails getTenantDetailsById(UUID consortiumId, String tenantId);

/**
* Gets central tenant id from db
* @return central tenant id
Expand Down
Loading

0 comments on commit da159ff

Please sign in to comment.