fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-office] 06/14: Add check if office has employee before delete
Date Mon, 22 Jan 2018 15:24:04 GMT
This is an automated email from the ASF dual-hosted git repository.

myrle pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-office.git

commit de1b1acfa022ee8c721749e47b437bf45d27603e
Author: Mark <mark.vanveen@gmail.com>
AuthorDate: Wed Apr 12 17:21:38 2017 +0200

    Add check if office has employee before delete
---
 .../src/main/java/io/mifos/office/TestOffice.java  | 47 ++++++++++++++++++++--
 .../internal/repository/EmployeeRepository.java    |  3 ++
 .../office/internal/service/OfficeService.java     | 22 +++++-----
 .../rest/controller/OfficeRestController.java      | 10 ++++-
 4 files changed, 66 insertions(+), 16 deletions(-)

diff --git a/component-test/src/main/java/io/mifos/office/TestOffice.java b/component-test/src/main/java/io/mifos/office/TestOffice.java
index c28d48d..6399eef 100644
--- a/component-test/src/main/java/io/mifos/office/TestOffice.java
+++ b/component-test/src/main/java/io/mifos/office/TestOffice.java
@@ -24,15 +24,14 @@ import io.mifos.core.test.fixture.mariadb.MariaDBInitializer;
 import io.mifos.core.test.listener.EnableEventRecording;
 import io.mifos.core.test.listener.EventRecorder;
 import io.mifos.office.api.v1.EventConstants;
-import io.mifos.office.api.v1.client.AlreadyExistsException;
-import io.mifos.office.api.v1.client.BadRequestException;
-import io.mifos.office.api.v1.client.OrganizationManager;
-import io.mifos.office.api.v1.client.NotFoundException;
+import io.mifos.office.api.v1.client.*;
 import io.mifos.office.api.v1.domain.Address;
+import io.mifos.office.api.v1.domain.Employee;
 import io.mifos.office.api.v1.domain.Office;
 import io.mifos.office.api.v1.domain.OfficePage;
 import io.mifos.office.rest.config.OfficeRestConfiguration;
 import io.mifos.office.util.AddressFactory;
+import io.mifos.office.util.EmployeeFactory;
 import io.mifos.office.util.OfficeFactory;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.junit.*;
@@ -281,6 +280,46 @@ public class TestOffice {
     Assert.assertEquals(parent.getIdentifier(), savedBranch.getParentIdentifier());
   }
 
+  @Test(expected = NotFoundException.class)
+  public void shouldDeleteOffice() throws Exception{
+    final Office office = OfficeFactory.createRandomOffice();
+    this.organizationManager.createOffice(office);
+    this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, office.getIdentifier());
+
+    this.organizationManager.deleteOffice(office.getIdentifier());
+
+    this.eventRecorder.wait(EventConstants.OPERATION_DELETE_OFFICE, office.getIdentifier());
+
+    this.organizationManager.findOfficeByIdentifier(office.getIdentifier());
+  }
+
+  @Test(expected = ChildrenExistException.class)
+  public void shouldNotDeleteOfficeWithBranches() throws Exception{
+    final Office parent = OfficeFactory.createRandomOffice();
+    this.organizationManager.createOffice(parent);
+    this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, parent.getIdentifier());
+
+    final Office branch = OfficeFactory.createRandomOffice();
+    this.organizationManager.addBranch(parent.getIdentifier(), branch);
+    this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, branch.getIdentifier());
+
+    this.organizationManager.deleteOffice(parent.getIdentifier());
+  }
+
+  @Test(expected = ChildrenExistException.class)
+  public void shouldNotDeleteOfficeWithEmployees() throws Exception{
+    final Office office = OfficeFactory.createRandomOffice();
+    this.organizationManager.createOffice(office);
+    this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, office.getIdentifier());
+
+    final Employee employee = EmployeeFactory.createRandomEmployee();
+    employee.setAssignedOffice(office.getIdentifier());
+    this.organizationManager.createEmployee(employee);
+    this.eventRecorder.wait(EventConstants.OPERATION_POST_EMPLOYEE, employee.getIdentifier());
+
+    this.organizationManager.deleteOffice(office.getIdentifier());
+  }
+
   @Configuration
   @ComponentScan(
       basePackages = "io.mifos.office.listener"
diff --git a/service/src/main/java/io/mifos/office/internal/repository/EmployeeRepository.java
b/service/src/main/java/io/mifos/office/internal/repository/EmployeeRepository.java
index 1b9d687..845cf74 100644
--- a/service/src/main/java/io/mifos/office/internal/repository/EmployeeRepository.java
+++ b/service/src/main/java/io/mifos/office/internal/repository/EmployeeRepository.java
@@ -33,4 +33,7 @@ public interface EmployeeRepository extends JpaRepository<EmployeeEntity,
Long>
   Page<EmployeeEntity> findByAssignedOffice(final OfficeEntity assignedOffice, final
Pageable pageable);
 
   Page<EmployeeEntity> findByIdentifierContaining(String term, Pageable pageRequest);
+
+  @Query("SELECT CASE WHEN COUNT(e) > 0 THEN 'true' ELSE 'false' END FROM EmployeeEntity
e WHERE e.assignedOffice = :office")
+  Boolean existsByAssignedOffice(@Param("office") final OfficeEntity assignedOffice);
 }
diff --git a/service/src/main/java/io/mifos/office/internal/service/OfficeService.java b/service/src/main/java/io/mifos/office/internal/service/OfficeService.java
index b2ce26b..e8b29c2 100644
--- a/service/src/main/java/io/mifos/office/internal/service/OfficeService.java
+++ b/service/src/main/java/io/mifos/office/internal/service/OfficeService.java
@@ -22,10 +22,7 @@ import io.mifos.office.api.v1.domain.Office;
 import io.mifos.office.api.v1.domain.OfficePage;
 import io.mifos.office.internal.mapper.AddressMapper;
 import io.mifos.office.internal.mapper.OfficeMapper;
-import io.mifos.office.internal.repository.AddressEntity;
-import io.mifos.office.internal.repository.AddressRepository;
-import io.mifos.office.internal.repository.OfficeEntity;
-import io.mifos.office.internal.repository.OfficeRepository;
+import io.mifos.office.internal.repository.*;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -44,15 +41,18 @@ public class OfficeService {
   private final Logger logger;
   private final OfficeRepository officeRepository;
   private final AddressRepository addressRepository;
+  private final EmployeeRepository employeeRepository;
 
   @Autowired
   public OfficeService(@Qualifier(ServiceConstants.SERVICE_LOGGER_NAME) final Logger logger,
                        final OfficeRepository officeRepository,
-                       final AddressRepository addressRepository) {
+                       final AddressRepository addressRepository,
+                       final EmployeeRepository employeeRepository) {
     super();
     this.logger = logger;
     this.officeRepository = officeRepository;
     this.addressRepository = addressRepository;
+    this.employeeRepository = employeeRepository;
   }
 
   public boolean officeExists(final String identifier) {
@@ -61,11 +61,13 @@ public class OfficeService {
 
   public boolean branchExists(final String identifier) {
     final Optional<OfficeEntity> officeEntityOptional = this.officeRepository.findByIdentifier(identifier);
-    if (officeEntityOptional.isPresent()) {
-      return this.officeRepository.existsByParentOfficeId(officeEntityOptional.get().getId());
-    } else {
-      throw ServiceException.notFound("Office {0} not found.", identifier);
-    }
+    return officeEntityOptional.map(officeEntity -> this.officeRepository.existsByParentOfficeId(officeEntity.getId())).orElse(false);
+  }
+
+  public boolean hasEmployees(final String officeIdentifier){
+    return this.officeRepository.findByIdentifier(officeIdentifier)
+            .map(this.employeeRepository::existsByAssignedOffice)
+            .orElse(false);
   }
 
   @Transactional(readOnly = true)
diff --git a/service/src/main/java/io/mifos/office/rest/controller/OfficeRestController.java
b/service/src/main/java/io/mifos/office/rest/controller/OfficeRestController.java
index ac60901..ab30f63 100644
--- a/service/src/main/java/io/mifos/office/rest/controller/OfficeRestController.java
+++ b/service/src/main/java/io/mifos/office/rest/controller/OfficeRestController.java
@@ -208,14 +208,20 @@ public class OfficeRestController {
   @ResponseBody
   ResponseEntity<Void> deleteOffice(@PathVariable("identifier") final String identifier)
       throws InterruptedException {
+    if (!this.officeService.officeExists(identifier)) {
+      throw ServiceException.notFound("Office {0} not found.", identifier);
+    }
+
     if (this.officeService.branchExists(identifier)) {
       throw ServiceException.conflict("Office {0} has children.", identifier);
     }
 
-    if (this.officeService.officeExists(identifier)) {
-      this.commandGateway.process(new DeleteOfficeCommand(identifier));
+    if(this.officeService.hasEmployees(identifier)){
+      throw ServiceException.conflict("Office {0} has employees.", identifier);
     }
 
+    this.commandGateway.process(new DeleteOfficeCommand(identifier));
+
     return ResponseEntity.accepted().build();
   }
 

-- 
To stop receiving notification emails like this one, please contact
myrle@apache.org.

Mime
View raw message