From 4cb8ca8c71527fcbd4466e2adcb628d7ed5eb921 Mon Sep 17 00:00:00 2001 From: andrew-dzak Date: Wed, 22 Oct 2014 20:38:21 +0200 Subject: [PATCH] MIFOSX-1175 add extra param to assign staff to group with integration test --- api-docs/apiLive.htm | 19 ++++ .../integrationtests/GroupTest.java | 106 ++++++++++++++++++ .../integrationtests/common/ClientHelper.java | 25 +++++ .../integrationtests/common/GroupHelper.java | 27 +++++ .../integrationtests/common/StaffHelper.java | 60 ++++++++++ .../common/loans/LoanTransactionHelper.java | 5 + .../group/api/GroupingTypesApiConstants.java | 1 + .../group/api/GroupsApiResource.java | 0 .../portfolio/group/domain/Group.java | 4 + .../GroupingTypesDataValidator.java | 7 +- .../GroupingTypesWritePlatformService.java | 0 ...WritePlatformServiceJpaRepositoryImpl.java | 40 ++++++- 12 files changed, 289 insertions(+), 5 deletions(-) mode change 100644 => 100755 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GroupTest.java mode change 100644 => 100755 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java mode change 100644 => 100755 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GroupHelper.java create mode 100755 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/StaffHelper.java mode change 100644 => 100755 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanTransactionHelper.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupingTypesApiConstants.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java mode change 100644 => 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java diff --git a/api-docs/apiLive.htm b/api-docs/apiLive.htm index bf5da8ff5..b3866d49b 100644 --- a/api-docs/apiLive.htm +++ b/api-docs/apiLive.htm @@ -5979,6 +5979,25 @@ Request Body:

Assign a Staff

Allows you to assign Staff to an existing Group.

The selected Staff should be belong to the same office (or an office higher up in the hierarchy) as this group

+ + + + + + + +
Mandatory Fields
staffId
+ + + + + + + + + + +
Optional Fields
inheritStaffForClientAccounts
Optional: Boolean if true all members of the group (i.e all clients with active loans and savings ) will inherit the staffId
POST https://Domain Name/api/v1/groups/{groupId}?command=assignStaff diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GroupTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GroupTest.java old mode 100644 new mode 100755 index 74cce3fbf..e8ee5a10f --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GroupTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GroupTest.java @@ -5,10 +5,12 @@ */ package org.mifosplatform.integrationtests; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mifosplatform.integrationtests.common.ClientHelper; import org.mifosplatform.integrationtests.common.GroupHelper; +import org.mifosplatform.integrationtests.common.StaffHelper; import org.mifosplatform.integrationtests.common.Utils; import com.jayway.restassured.builder.RequestSpecBuilder; @@ -16,6 +18,16 @@ import com.jayway.restassured.builder.ResponseSpecBuilder; import com.jayway.restassured.http.ContentType; import com.jayway.restassured.specification.RequestSpecification; import com.jayway.restassured.specification.ResponseSpecification; +import org.mifosplatform.integrationtests.common.accounting.Account; +import org.mifosplatform.integrationtests.common.loans.LoanApplicationTestBuilder; +import org.mifosplatform.integrationtests.common.loans.LoanProductTestBuilder; +import org.mifosplatform.integrationtests.common.loans.LoanTransactionHelper; + +import java.util.HashMap; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; /** * Group Test for checking Group: Creation, Activation, Client Association, @@ -25,6 +37,12 @@ public class GroupTest { private ResponseSpecification responseSpec; private RequestSpecification requestSpec; + private LoanTransactionHelper loanTransactionHelper; + private String principal = "10000.00"; + private String accountingRule = "1"; + private String numberOfRepayments = "5"; + private String interestRatePerPeriod = "18"; + @Before public void setup() { @@ -32,6 +50,8 @@ public class GroupTest { this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec); + } @Test @@ -58,4 +78,90 @@ public class GroupTest { // GroupHelper.deleteGroup(this.requestSpec, this.responseSpec, groupID.toString()); // GroupHelper.verifyGroupDeleted(this.requestSpec, this.responseSpec, groupID); } + + @Test + public void assignStaffToGroup(){ + Integer groupID = GroupHelper.createGroup(this.requestSpec, this.responseSpec); + GroupHelper.verifyGroupCreatedOnServer(this.requestSpec, this.responseSpec, groupID); + + final String updateGroupName = Utils.randomNameGenerator("Savings Group Help_",5); + groupID = GroupHelper.activateGroup(this.requestSpec, this.responseSpec, groupID.toString()); + Integer updateGroupId = GroupHelper.updateGroup(this.requestSpec,this.responseSpec,updateGroupName,groupID.toString()); + + //create client and add client to group + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + groupID = GroupHelper.associateClient(this.requestSpec, this.responseSpec, groupID.toString(), clientID.toString()); + GroupHelper.verifyGroupMembers(this.requestSpec, this.responseSpec, groupID, clientID); + + + //create staff + Integer createStaffId1 = StaffHelper.createStaff(this.requestSpec, this.responseSpec) ; + System.out.println("--------------creating first staff with id-------------" + createStaffId1); + Assert.assertNotNull(createStaffId1); + + Integer createStaffId2 = StaffHelper.createStaff(this.requestSpec, this.responseSpec); + System.out.println("--------------creating second staff with id-------------" + createStaffId2); + Assert.assertNotNull(createStaffId2); + + //assign staff "createStaffId1" to group + HashMap assignStaffGroupId = (HashMap)GroupHelper.assignStaff(this.requestSpec,this.responseSpec,groupID.toString(),createStaffId1.longValue()); + assertEquals("Verify assigned staff id is the same as id sent",assignStaffGroupId.get("staffId"),createStaffId1); + + + + //assign staff "createStaffId2" to client + final HashMap assignStaffToClientChanges = (HashMap)ClientHelper.assignStaffToClient(this.requestSpec,this.responseSpec,clientID.toString(),createStaffId2.toString()); + assertEquals("Verify assigned staff id is the same as id sent", assignStaffToClientChanges.get("staffId"),createStaffId2); + + + final Integer loanProductId = this.createLoanProduct(); + + final Integer loanId = this.applyForLoanApplication(clientID,loanProductId,this.principal); + + this.loanTransactionHelper.approveLoan("20 September 2014",loanId); + this.loanTransactionHelper.disburseLoan("20 September 2014",loanId); + + final HashMap assignStaffAndInheritStaffForClientAccounts = (HashMap)GroupHelper.assignStaffInheritStaffForClientAccounts(this.requestSpec,this.responseSpec,groupID.toString(),createStaffId1.toString()); + final Integer getClientStaffId = ClientHelper.getClient(this.requestSpec,this.responseSpec,clientID.toString()); + + //assert if client staff officer has change Note client was assigned staff with createStaffId2 + assertNotEquals("Verify if client stuff has changed", assignStaffAndInheritStaffForClientAccounts.get("staffId"), createStaffId2); + assertEquals("Verify if client inherited staff assigned above", assignStaffAndInheritStaffForClientAccounts.get("staffId"), getClientStaffId); + + //assert if clients loan officer has changed + final Integer loanOfficerId = this.loanTransactionHelper.getLoanOfficerId(loanId.toString()); + assertEquals("Verify if client loan inherited staff", assignStaffAndInheritStaffForClientAccounts.get("staffId"), loanOfficerId); + + + } + + private Integer createLoanProduct() { + final String loanProductJSON = new LoanProductTestBuilder() + .withPrincipal(this.principal) + .withNumberOfRepayments(this.numberOfRepayments) + .withinterestRatePerPeriod(this.interestRatePerPeriod) + .withInterestRateFrequencyTypeAsYear() + .build(null); + return this.loanTransactionHelper.getLoanProductId(loanProductJSON); + } + + private Integer applyForLoanApplication(final Integer clientID, final Integer loanProductID, String principal) { + System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + final String loanApplicationJSON = new LoanApplicationTestBuilder() // + .withPrincipal(principal) // + .withLoanTermFrequency("4") // + .withLoanTermFrequencyAsMonths() // + .withNumberOfRepayments("4") // + .withRepaymentEveryAfter("1") // + .withRepaymentFrequencyTypeAsMonths() // + .withInterestRatePerPeriod("2") // + .withAmortizationTypeAsEqualInstallments() // + .withInterestTypeAsDecliningBalance() // + .withInterestCalculationPeriodTypeSameAsRepaymentPeriod() // + .withExpectedDisbursementDate("20 September 2014") // + .withSubmittedOnDate("20 September 2014") // + .build(clientID.toString(), loanProductID.toString(), null); + return this.loanTransactionHelper.getLoanId(loanApplicationJSON); + } + } \ No newline at end of file diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java old mode 100644 new mode 100755 index d0b51fc74..80fab5744 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java @@ -33,6 +33,24 @@ public class ClientHelper { "clientId"); } + public static Object assignStaffToClient(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final String clientId, final String staffId) { + final String CLIENT_ASSIGN_STAFF_URL = "/mifosng-provider/api/v1/clients/" + clientId + "?" + Utils.TENANT_IDENTIFIER + "&command=assignStaff"; + + System.out.println("---------------------------------CREATING A CLIENT---------------------------------------------"); + return Utils.performServerPost(requestSpec, responseSpec, CLIENT_ASSIGN_STAFF_URL, assignStaffToClientAsJson(staffId), + "changes"); + } + + public static Integer getClient(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final String clientId){ + final String GET_CLIENT_URL = "/mifosng-provider/api/v1/clients/" + clientId + "?" + Utils.TENANT_IDENTIFIER ; + + System.out.println("---------------------------------GET A CLIENT---------------------------------------------"); + return Utils.performServerGet(requestSpec, responseSpec, GET_CLIENT_URL,"staffId"); + + } + public static String getTestClientAsJSON(final String dateOfJoining, final String officeId) { final HashMap map = new HashMap<>(); map.put("officeId", officeId); @@ -47,6 +65,13 @@ public class ClientHelper { return new Gson().toJson(map); } + public static String assignStaffToClientAsJson(final String staffId){ + final HashMap map = new HashMap<>(); + map.put("staffId", staffId); + System.out.println("map : " + map); + return new Gson().toJson(map); + } + public static void verifyClientCreatedOnServer(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer generatedClientID) { System.out.println("------------------------------CHECK CLIENT DETAILS------------------------------------\n"); diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GroupHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GroupHelper.java old mode 100644 new mode 100755 index f7d5fa2c2..91a6ce2bc --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GroupHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GroupHelper.java @@ -76,6 +76,19 @@ public class GroupHelper { return Utils.performServerDelete(requestSpec, responseSpec, GROUP_ASSOCIATE_URL, "groupId"); } + + public static Object assignStaff(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final String groupId,final Long staffId){ + final String GROUP_ASSIGN_STAFF_URL = "/mifosng-provider/api/v1/groups/" + groupId + "?" + Utils.TENANT_IDENTIFIER + "&command=assignStaff"; + System.out.println("---------------------------------DELETE GROUP---------------------------------------------"); + return Utils.performServerPost(requestSpec, responseSpec, GROUP_ASSIGN_STAFF_URL,assignStaffAsJSON(staffId),"changes"); + } + public static Object assignStaffInheritStaffForClientAccounts(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final String groupId,final String staffId){ + final String GROUP_ASSIGN_STAFF_URL = "/mifosng-provider/api/v1/groups/" + groupId + "?" + Utils.TENANT_IDENTIFIER + "&command=assignStaff"; + System.out.println("---------------------------------DELETE GROUP---------------------------------------------"); + return Utils.performServerPost(requestSpec, responseSpec, GROUP_ASSIGN_STAFF_URL,assignStaffAndInheritStaffForClientAccountsAsJSON(staffId),"changes"); + } + + public static String getTestGroupAsJSON(final boolean active, final String activationDate) { final HashMap map = new HashMap<>(); map.put("officeId", "1"); @@ -125,6 +138,20 @@ public class GroupHelper { System.out.println("map : " + map); return new Gson().toJson(map); } + public static String assignStaffAsJSON(final Long staffId) { + final HashMap map = new HashMap<>(); + map.put("staffId", staffId); + System.out.println("map : " + map); + return new Gson().toJson(map); + } + public static String assignStaffAndInheritStaffForClientAccountsAsJSON(final String staffId) { + final HashMap map = new HashMap<>(); + map.put("staffId", staffId); + map.put("inheritStaffForClientAccounts","true"); + System.out.println("map : " + map); + return new Gson().toJson(map); + } + public static void verifyGroupCreatedOnServer(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer generatedGroupID) { diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/StaffHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/StaffHelper.java new file mode 100755 index 000000000..0ca102866 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/StaffHelper.java @@ -0,0 +1,60 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.integrationtests.common; + +import com.google.gson.Gson; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +import java.util.HashMap; + +public class StaffHelper { + + private static final String TRANSFER_STAFF_URL = "/mifosng-provider/api/v1/groups"; + + private static final String CREATE_STAFF_URL = "/mifosng-provider/api/v1/staff"; + + private static final String RESOURCE_ID = "resourceId"; + + public static final String GROUP_ID = "groupId"; + + + + public static Integer transferStaffToGroup(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer groupId,final Integer staffToTransfer ,final String note){ + final String url = TRANSFER_STAFF_URL + "/" + groupId + "?command=transferStaff&"+Utils.TENANT_IDENTIFIER; + return Utils.performServerPost(requestSpec, responseSpec, url, transferStaffToGroupAsJSON(staffToTransfer, note), GROUP_ID); + } + + + public static String transferStaffToGroupAsJSON(final Integer staffToTransferId,final String note) { + final HashMap map = new HashMap<>(); + map.put("staffId", staffToTransferId); + map.put("note", note); + System.out.println("map : " + map); + return new Gson().toJson(map); + } + + + + public static Integer createStaff(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { + final String url = CREATE_STAFF_URL +"?"+ Utils.TENANT_IDENTIFIER ; + return Utils.performServerPost(requestSpec, responseSpec, url, createStaffAsJSON(), RESOURCE_ID); + } + + + public static String createStaffAsJSON(){ + final HashMap map = new HashMap<>(); + map.put("officeId",1); + map.put("firstname", Utils.randomNameGenerator("michael_",5)); + map.put("lastname", Utils.randomNameGenerator("Doe_",4)); + map.put("isLoanOfficer", true); + System.out.println("map : " + map); + return new Gson().toJson(map); + } + + +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanTransactionHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanTransactionHelper.java old mode 100644 new mode 100755 index 207d4e3ce..620f657a1 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanTransactionHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanTransactionHelper.java @@ -48,6 +48,11 @@ public class LoanTransactionHelper { public Integer getLoanId(final String loanApplicationJSON) { return Utils.performServerPost(this.requestSpec, this.responseSpec, APPLY_LOAN_URL, loanApplicationJSON, "loanId"); } + public Integer getLoanOfficerId(final String loanId) { + final String GET_LOAN_URL = "/mifosng-provider/api/v1/loans/" + loanId + "?" + Utils.TENANT_IDENTIFIER; + return Utils.performServerGet(this.requestSpec, this.responseSpec, GET_LOAN_URL, "loanOfficerId"); + } + public Integer updateLoan(final Integer id, final String loanApplicationJSON) { return Utils.performServerPut(this.requestSpec, this.responseSpec, "/mifosng-provider/api/v1/loans/" + id diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupingTypesApiConstants.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupingTypesApiConstants.java old mode 100644 new mode 100755 index 23d4050f3..28ab2d571 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupingTypesApiConstants.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupingTypesApiConstants.java @@ -43,6 +43,7 @@ public class GroupingTypesApiConstants { public static final String groupMembersParamName = "groupMembers"; public static final String submittedOnDateParamName = "submittedOnDate"; + public static final String inheritStaffForClientAccounts = "inheritStaffForClientAccounts"; // group parameters public static final String centerIdParamName = "centerId"; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java old mode 100644 new mode 100755 diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java old mode 100644 new mode 100755 index 8d80b0279..e2b8f6f31 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java @@ -567,4 +567,8 @@ public final class Group extends AbstractPersistable { return differences; } + + public Set getClientMembers() { + return this.clientMembers; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java old mode 100644 new mode 100755 index 3559adf12..8720e58d2 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupingTypesDataValidator.java @@ -368,7 +368,7 @@ public final class GroupingTypesDataValidator { final Type typeOfMap = new TypeToken>() {}.getType(); - final Set supportedParametersAssignStaff = new HashSet<>(Arrays.asList(GroupingTypesApiConstants.staffIdParamName)); + final Set supportedParametersAssignStaff = new HashSet<>(Arrays.asList(GroupingTypesApiConstants.staffIdParamName,GroupingTypesApiConstants.inheritStaffForClientAccounts)); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersAssignStaff); final JsonElement element = this.fromApiJsonHelper.parse(json); @@ -382,6 +382,11 @@ public final class GroupingTypesDataValidator { final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().longGreaterThanZero(); + final String inheritStaffForClientAccountsParamName = GroupingTypesApiConstants.inheritStaffForClientAccounts; + final Boolean inheritStaffForClientAccounts= this.fromApiJsonHelper.extractBooleanNamed(inheritStaffForClientAccountsParamName,element); + baseDataValidator.reset().parameter(inheritStaffForClientAccountsParamName).value(inheritStaffForClientAccounts).ignoreIfNull().notBlank().isOneOfTheseValues(true, false); + + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformService.java old mode 100644 new mode 100755 diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java old mode 100644 new mode 100755 index 08ad471dd..747f736e1 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -88,6 +88,8 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group private final SavingsAccountRepository savingsRepository; private final CommandProcessingService commandProcessingService; private final CalendarInstanceRepository calendarInstanceRepository; + private final SavingsAccountRepository savingsAccountRepository; + @Autowired public GroupingTypesWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, @@ -96,7 +98,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group final GroupLevelRepository groupLevelRepository, final GroupingTypesDataValidator fromApiJsonDeserializer, final LoanRepository loanRepository, final SavingsAccountRepository savingsRepository, final CodeValueRepositoryWrapper codeValueRepository, final CommandProcessingService commandProcessingService, - final CalendarInstanceRepository calendarInstanceRepository) { + final CalendarInstanceRepository calendarInstanceRepository,final SavingsAccountRepository savingsAccountRepository) { this.context = context; this.groupRepository = groupRepository; this.clientRepositoryWrapper = clientRepositoryWrapper; @@ -110,6 +112,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group this.codeValueRepository = codeValueRepository; this.commandProcessingService = commandProcessingService; this.calendarInstanceRepository = calendarInstanceRepository; + this.savingsAccountRepository = savingsAccountRepository; } private CommandProcessingResult createGroupingType(final JsonCommand command, final GroupTypes groupingType, final Long centerId) { @@ -405,7 +408,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } @Override - public CommandProcessingResult assignGroupOrCenterStaff(final Long grouptId, final JsonCommand command) { + public CommandProcessingResult assignGroupOrCenterStaff(final Long groupId,final JsonCommand command) { this.context.authenticatedUser(); @@ -413,13 +416,42 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group this.fromApiJsonDeserializer.validateForAssignStaff(command.json()); - final Group groupForUpdate = this.groupRepository.findOneWithNotFoundDetection(grouptId); + final Group groupForUpdate = this.groupRepository.findOneWithNotFoundDetection(groupId); Staff staff = null; final Long staffId = command.longValueOfParameterNamed(GroupingTypesApiConstants.staffIdParamName); + + final boolean inheritStaffForClientAccounts = command.booleanPrimitiveValueOfParameterNamed(GroupingTypesApiConstants.inheritStaffForClientAccounts); staff = this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(staffId, groupForUpdate.getOffice().getHierarchy()); groupForUpdate.updateStaff(staff); + if(inheritStaffForClientAccounts){ + LocalDate loanOfficerReassignmentDate = LocalDate.now(); + /* + update loan officer for client and update loan officer for clients loans and savings + */ + Set clients = groupForUpdate.getClientMembers(); + if(clients !=null){ + for(Client client : clients){ + client.updateStaff(staff); + if(this.loanRepository.doNonClosedLoanAccountsExistForClient(client.getId())){ + for(final Loan loan :this.loanRepository.findLoanByClientId(client.getId())){ + if (loan.isDisbursed() && !loan.isClosed()){ + loan.reassignLoanOfficer(staff,loanOfficerReassignmentDate); + } + } + } + if(this.savingsAccountRepository.doNonClosedSavingAccountsExistForClient(client.getId())){ + for (final SavingsAccount savingsAccount : this.savingsAccountRepository.findSavingAccountByClientId(client.getId())){ + if (!savingsAccount.isClosed()){ + savingsAccount.update(staff); + } + } + } + } + } + } + this.groupRepository.saveAndFlush(groupForUpdate); actualChanges.put(GroupingTypesApiConstants.staffIdParamName, staffId); @@ -427,7 +459,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group return new CommandProcessingResultBuilder() // .withOfficeId(groupForUpdate.officeId()) // .withEntityId(groupForUpdate.getId()) // - .withGroupId(grouptId) // + .withGroupId(groupId) // .with(actualChanges) // .build(); }