Skip to content

Commit

Permalink
avniproject/avni-webapp#1304 - moved category and status to database
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Aug 7, 2024
1 parent 27e62ff commit 943f204
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni.server.dao;

import org.avni.server.domain.organisation.OrganisationCategory;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RepositoryRestResource(collectionResourceRel = "organisationCategory", path = "organisationCategory")
public interface OrganisationCategoryRepository extends AvniJpaRepository<OrganisationCategory, Long>, CHSRepository<OrganisationCategory> {
@RestResource(path = "findAllById", rel = "findAllById")
List<OrganisationCategory> findByIdIn(@Param("ids") Long[] ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni.server.dao;

import org.avni.server.domain.organisation.OrganisationStatus;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RepositoryRestResource(collectionResourceRel = "organisationStatus", path = "organisationStatus")
public interface OrganisationStatusRepository extends AvniJpaRepository<OrganisationStatus, Long>, CHSRepository<OrganisationStatus> {
@RestResource(path = "findAllById", rel = "findAllById")
List<OrganisationStatus> findByIdIn(@Param("ids") Long[] ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class Organisation extends ETLEntity {
private Account account;

@NotNull
@Enumerated(EnumType.STRING)
@ManyToOne(fetch = FetchType.EAGER)
private OrganisationCategory category;

@NotNull
@Enumerated(EnumType.STRING)
@ManyToOne(fetch = FetchType.EAGER)
private OrganisationStatus status;

public Organisation() {
Expand Down Expand Up @@ -86,19 +86,29 @@ public String getEffectiveUsernameSuffix() {
return usernameSuffix == null ? getDbUser() : usernameSuffix;
}

@JsonIgnore
public OrganisationCategory getCategory() {
return category;
}

public Long getCategoryId() {
return category.getId();
}

public void setCategory(OrganisationCategory organisationCategory) {
this.category = organisationCategory;
}

@JsonIgnore
public OrganisationStatus getStatus() {
return status;
}

public void setStatus(OrganisationStatus status) {
this.status = status;
}

public Long getStatusId() {
return status.getId();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package org.avni.server.domain.organisation;

public enum OrganisationCategory {
Production, UAT, Prototype, Temporary, Trial
import org.avni.server.domain.CHSEntity;

import javax.persistence.Entity;

@Entity
public class OrganisationCategory extends CHSEntity {
public static final String Production = "Production";
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package org.avni.server.domain.organisation;

public enum OrganisationStatus {
Archived, Live
import org.avni.server.domain.CHSEntity;

import javax.persistence.Entity;

@Entity
public class OrganisationStatus extends CHSEntity {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.avni.server.domain.*;
import org.avni.server.domain.accessControl.GroupPrivilege;
import org.avni.server.domain.accessControl.Privilege;
import org.avni.server.domain.organisation.OrganisationCategory;
import org.avni.server.domain.organisation.OrganisationStatus;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
Expand Down Expand Up @@ -37,6 +39,8 @@ public void configureRepositoryRestConfiguration(RepositoryRestConfiguration con
config.exposeIdsFor(CommentThread.class);
config.exposeIdsFor(RuleFailureTelemetry.class);
config.exposeIdsFor(Individual.class);
config.exposeIdsFor(OrganisationCategory.class);
config.exposeIdsFor(OrganisationStatus.class);

//TODO
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ResponseEntity delete(@Param("deleteMetadata") boolean deleteMetadata) {
return new ResponseEntity<>("Super admin cannot delete implementation data", HttpStatus.FORBIDDEN);
}
Organisation organisation = organisationService.getCurrentOrganisation();
if (OrganisationCategory.Production.equals(organisation.getCategory())) {
if (OrganisationCategory.Production.equals(organisation.getCategory().getName())) {
return new ResponseEntity<>("Production organisation's data cannot be deleted", HttpStatus.CONFLICT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ public class OrganisationController implements RestControllerResourceProcessor<O
private final ImplementationRepository implementationRepository;
private final AccessControlService accessControlService;
private final OrganisationService organisationService;
private final OrganisationCategoryRepository organisationCategoryRepository;
private final OrganisationStatusRepository organisationStatusRepository;

@Autowired
public OrganisationController(OrganisationRepository organisationRepository, AccountRepository accountRepository, ImplementationRepository implementationRepository, AccessControlService accessControlService, OrganisationService organisationService) {
public OrganisationController(OrganisationRepository organisationRepository, AccountRepository accountRepository, ImplementationRepository implementationRepository, AccessControlService accessControlService, OrganisationService organisationService, OrganisationCategoryRepository organisationCategoryRepository, OrganisationStatusRepository organisationStatusRepository) {
this.organisationRepository = organisationRepository;
this.accountRepository = accountRepository;
this.implementationRepository = implementationRepository;
this.accessControlService = accessControlService;
this.organisationService = organisationService;
this.organisationCategoryRepository = organisationCategoryRepository;
this.organisationStatusRepository = organisationStatusRepository;
}

@RequestMapping(value = "/organisation", method = RequestMethod.POST)
Expand All @@ -50,8 +54,8 @@ public ResponseEntity save(@RequestBody OrganisationContract request) {
org.setUuid(request.getUuid() == null ? UUID.randomUUID().toString() : request.getUuid());
org.setDbUser(request.getDbUser());
org.setSchemaName(request.getSchemaName());
org.setCategory(request.getCategory());
org.setStatus(request.getStatus());
org.setCategory(organisationCategoryRepository.findEntity(request.getCategoryId()));
org.setStatus(organisationStatusRepository.findEntity(request.getStatusId()));
setAttributesOnOrganisation(request, org);
setOrgAccountByIdOrDefault(org, request.getAccountId());

Expand Down Expand Up @@ -136,8 +140,8 @@ private void setAttributesOnOrganisation(@RequestBody OrganisationContract reque
}
organisation.setMediaDirectory(request.getMediaDirectory());
organisation.setVoided(request.isVoided());
organisation.setCategory(request.getCategory());
organisation.setStatus(request.getStatus());
organisation.setCategory(organisationCategoryRepository.findEntity(request.getCategoryId()));
organisation.setStatus(organisationStatusRepository.findEntity(request.getStatusId()));
}

private void setOrgAccountByIdOrDefault(Organisation organisation, Long accountId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class OrganisationContract extends ETLContract {
private String mediaDirectory;
private String usernameSuffix;
private Long accountId;
private OrganisationCategory category;
private OrganisationStatus status;
private Long categoryId;
private Long statusId;

public static OrganisationContract fromEntity(Organisation organisation) {
OrganisationContract organisationContract = new OrganisationContract();
Expand All @@ -20,12 +20,20 @@ public static OrganisationContract fromEntity(Organisation organisation) {
organisationContract.setMediaDirectory(organisation.getMediaDirectory());
organisationContract.setUsernameSuffix(organisation.getEffectiveUsernameSuffix());
organisationContract.setAccountId(organisation.getAccount() == null ? null : organisation.getAccount().getId());
organisationContract.setCategory(organisation.getCategory());
organisationContract.setStatus(organisation.getStatus());
organisationContract.setCategoryId(organisation.getCategory().getId());
organisationContract.setStatusId(organisation.getStatus().getId());
mapEntity(organisationContract, organisation);
return organisationContract;
}

private void setStatusId(Long id) {
this.statusId = id;
}

private void setCategoryId(Long id) {
this.categoryId = id;
}

public Long getParentOrganisationId() {
return parentOrganisationId;
}
Expand Down Expand Up @@ -58,19 +66,11 @@ public void setMediaDirectory(String mediaDirectory) {
this.mediaDirectory = mediaDirectory;
}

public OrganisationCategory getCategory() {
return category;
}

public void setCategory(OrganisationCategory category) {
this.category = category;
}

public OrganisationStatus getStatus() {
return status;
public Long getCategoryId() {
return categoryId;
}

public void setStatus(OrganisationStatus status) {
this.status = status;
public Long getStatusId() {
return statusId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import java.util.List;

public class UserInfoWebResponse extends UserInfoContract {
private OrganisationCategory organisationCategory;
private long lastSessionTime;
private boolean hasAllPrivileges;
private List<UserPrivilegeWebResponse> privileges;
private boolean isAdmin;
private String organisationCategoryName;

private UserInfoWebResponse() {
}
Expand All @@ -29,7 +29,7 @@ public static UserInfoWebResponse createForAdminUser(List<UserPrivilegeWebRespon
response.setOrganisationId(contextOrganisation.getId());
response.setOrganisationName(contextOrganisation.getName());
response.setUsernameSuffix(contextOrganisation.getEffectiveUsernameSuffix());
response.organisationCategory = contextOrganisation.getCategory();
response.organisationCategoryName = contextOrganisation.getCategory().getName();
}
return response;
}
Expand All @@ -39,7 +39,7 @@ public UserInfoWebResponse(String username, String orgName, Long orgId, String u
this.privileges = privileges;
this.hasAllPrivileges = hasAllPrivileges;
this.lastSessionTime = lastSessionTime;
this.organisationCategory = organisationCategory;
this.organisationCategoryName = organisationCategory.getName();
}

public List<UserPrivilegeWebResponse> getPrivileges() {
Expand All @@ -62,7 +62,7 @@ public void setLastSessionTime(long lastSessionTime) {
this.lastSessionTime = lastSessionTime;
}

public OrganisationCategory getOrganisationCategory() {
return organisationCategory;
public String getOrganisationCategoryName() {
return organisationCategoryName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
create table if not exists organisation_category
(
id serial primary key,
uuid varchar(255) not null,
is_voided boolean NOT NULL DEFAULT FALSE,
name varchar(255) not null,
created_date_time timestamp(3) with time zone not null,
last_modified_date_time timestamp(3) with time zone not null,
created_by_id int not null,
last_modified_by_id int not null
);

insert into organisation_category (uuid, name, created_date_time, last_modified_date_time, created_by_id, last_modified_by_id)
values ('71e1bf3b-48fb-4d4f-90f3-71c39e15fbf0', 'Production', now(), now(), 1, 1),
('95e89458-c152-4557-9929-85f1a275d6a3', 'UAT', now(), now(), 1, 1),
('283af4ea-0024-4440-857f-c8a82328a61d', 'Prototype', now(), now(), 1, 1),
('f0b0a48d-8d4b-4d13-8956-c1bc577b4971', 'Temporary', now(), now(), 1, 1),
('470ecdab-f7be-4336-a52a-1fa280080168', 'Trial', now(), now(), 1, 1),
('d75e667e-b7ea-40dd-8d85-1328943d3b65', 'Training', now(), now(), 1, 1),
('27eeb3e7-2396-45ac-ba50-b1b50690bcfc', 'Dev', now(), now(), 1, 1);

alter table organisation add column if not exists category_id int null;

update organisation set category_id = organisation_category.id
from organisation_category
where organisation.category = organisation_category.name;

alter table organisation drop column category;

alter table organisation alter column category_id set not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
create table if not exists organisation_status
(
id serial primary key,
uuid varchar(255) not null,
is_voided boolean NOT NULL DEFAULT FALSE,
name varchar(255) not null,
created_date_time timestamp(3) with time zone not null,
last_modified_date_time timestamp(3) with time zone not null,
created_by_id int not null,
last_modified_by_id int not null
);

insert into organisation_status (uuid, name, created_date_time, last_modified_date_time, created_by_id, last_modified_by_id)
values ('338be2e2-d0e5-4186-b113-b8197ce879c5', 'Live', now(), now(), 1, 1),
('7e609db3-ff79-472c-8f28-5b12933faaf5', 'Archived', now(), now(), 1, 1);

alter table organisation add column if not exists status_id int null;

update organisation set status_id = organisation_status.id
from organisation_status
where organisation.status = organisation_status.name;

alter table organisation drop column status;

alter table organisation alter column status_id set not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table organisation_category add column if not exists version int not null default 1;
alter table organisation_status add column if not exists version int not null default 1;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TestOrganisationBuilder {

public TestOrganisationBuilder withMandatoryFields() {
String placeholder = UUID.randomUUID().toString();
return withUuid(placeholder).withDbUser("testDbUser").withName(placeholder).withSchemaName(placeholder).setCategory(OrganisationCategory.Production).withStatus(OrganisationStatus.Live);
return withUuid(placeholder).withDbUser("testDbUser").withName(placeholder).withSchemaName(placeholder);
}

public TestOrganisationBuilder setId(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ public class TestDataSetupService {
private TestLocationService testLocationService;
@Autowired
private TestCatchmentService testCatchmentService;
@Autowired
private OrganisationCategoryRepository organisationCategoryRepository;
@Autowired
private OrganisationStatusRepository organisationStatusRepository;

public TestOrganisationData setupOrganisation(String orgSuffix) {
Group group = new TestGroupBuilder().withMandatoryFieldsForNewEntity().build();
User user1 = new UserBuilder().withDefaultValuesForNewEntity().userName(String.format("user@%s", orgSuffix)).withAuditUser(userRepository.getDefaultSuperAdmin()).build();
User user2 = new UserBuilder().withDefaultValuesForNewEntity().userName(String.format("user2@%s", orgSuffix)).withAuditUser(userRepository.getDefaultSuperAdmin()).build();
Organisation organisation = new TestOrganisationBuilder().withMandatoryFields().withAccount(accountRepository.getDefaultAccount()).build();
Organisation organisation = new TestOrganisationBuilder()
.setCategory(organisationCategoryRepository.findEntity(1L))
.withStatus(organisationStatusRepository.findEntity(1L))
.withMandatoryFields()
.withAccount(accountRepository.getDefaultAccount()).build();
testOrganisationService.createOrganisation(organisation, user1);
testOrganisationService.createUser(organisation, user2);
userRepository.save(new UserBuilder(user1).withAuditUser(user1).build());
Expand Down
Loading

0 comments on commit 943f204

Please sign in to comment.