Skip to content

Commit

Permalink
MIFOSX-2049 and fix failing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwasbabu committed Sep 5, 2015
1 parent 66ce3b4 commit 8f1eb39
Show file tree
Hide file tree
Showing 17 changed files with 713 additions and 675 deletions.
6 changes: 3 additions & 3 deletions api-docs/apiLive.htm
Original file line number Diff line number Diff line change
Expand Up @@ -35848,8 +35848,8 @@ <h4>Add Client Charge</h4>
<div class="method-description">
<h4>List Client Charges</h4>

<p>The <i>list</i> capability of client charges can support <b>pagination</b> and <b>sorting</b>.</p>
<h5>Mandatory Arguments</h5>
<p>The <i>list</i> capability of client charges supports <b>pagination</b>.</p>
<h5>Optional Arguments</h5>
<dl class="argument-list">
<dt>offset</dt>
<dd>
Expand All @@ -35867,7 +35867,7 @@ <h5>Mandatory Arguments</h5>
<dd>
String <span>optional</span>
</dd>
<dd>Only returns those charges which are pending payment (neither paid or waived).</dd>
<dd> Filters charges that are pending payment (neither paid or waived).</dd>
</dl>
<p>Example Requests:</p>
<div class=apiClick>clients/189/charges</div>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ public void clientChargeTest() {
final String clientChargePaidTransactionId = ClientHelper.payChargesForClients(this.requestSpec, this.responseSpec, clientId,
clientChargeId, ClientHelper.getPayChargeJSON("25 AUGUST 2015", "10"));
Assert.assertNotNull(clientChargePaidTransactionId);
isValidOutstandingAmount(ClientHelper.getClientCharge(requestSpec, responseSpec, clientId.toString()), (float) 190.0);
isValidOutstandingAmount(ClientHelper.getClientCharge(requestSpec, responseSpec, clientId.toString(), clientChargeId.toString()),
(float) 190.0);

/**
* Revert the paid client charge transaction by passing the
* clientChargePaidTransactionId and ensure the same is reverted.
*/
final Integer undoTrxnId = ClientHelper.revertClientChargeTransaction(this.requestSpec, this.responseSpec, clientId.toString(),
clientChargePaidTransactionId);
final Integer undoTrxnId = ClientHelper.revertClientChargeTransaction(this.requestSpec, this.responseSpec,
clientId.toString(), clientChargePaidTransactionId);
Assert.assertNotNull(undoTrxnId);
isReversedTransaction(clientId.toString(), undoTrxnId.toString());
/**
Expand All @@ -103,8 +104,8 @@ public void clientChargeTest() {
* waiveOffClientChargeTransactionId and ensured the transaction is
* reversed.
*/
final Integer undoWaiveTrxnId = ClientHelper.revertClientChargeTransaction(this.requestSpec, this.responseSpec,
clientId.toString(), waiveOffClientChargeTransactionId);
final Integer undoWaiveTrxnId = ClientHelper.revertClientChargeTransaction(this.requestSpec, this.responseSpec, clientId.toString(),
waiveOffClientChargeTransactionId);
Assert.assertNotNull(undoWaiveTrxnId);
isReversedTransaction(clientId.toString(), undoWaiveTrxnId.toString());
/**
Expand All @@ -130,7 +131,8 @@ public void clientChargeTest() {
clientChargeId, ClientHelper.getPayChargeJSON("25 AUGUST 2015", "100"));
Assert.assertNotNull(chargePaid_responseId);

isValidOutstandingAmount(ClientHelper.getClientCharge(requestSpec, responseSpec, clientId.toString()), (float) 100.0);
isValidOutstandingAmount(ClientHelper.getClientCharge(requestSpec, responseSpec, clientId.toString(), clientChargeId.toString()),
(float) 100.0);

}

Expand All @@ -147,8 +149,8 @@ private void isReversedTransaction(String clientId, String transactionId) {
}

/**
* Check whether the outStandingAmount is equal to expected Amount or not after
* paying or after waiving off the client charge.
* Check whether the outStandingAmount is equal to expected Amount or not
* after paying or after waiving off the client charge.
*
* @param outStandingAmount
* @param expectedAmount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.HashMap;

import org.mifosplatform.integrationtests.common.system.CodeHelper;
Expand Down Expand Up @@ -57,12 +56,12 @@ public static Integer createClient(final RequestSpecification requestSpec, final
"clientId");
}

public static Integer createClientForAccountPreference(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec, final Integer clientType, String jsonAttributeToGetBack) {
public static Integer createClientForAccountPreference(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final Integer clientType, String jsonAttributeToGetBack) {
final String activationDate = "04 March 2011";
final String officeId = "1";
System.out
.println("---------------------------------CREATING A CLIENT BASED ON ACCOUNT PREFERENCE---------------------------------------------");
System.out.println(
"---------------------------------CREATING A CLIENT BASED ON ACCOUNT PREFERENCE---------------------------------------------");
return Utils.performServerPost(requestSpec, responseSpec, CREATE_CLIENT_URL,
getTestClientWithClientTypeAsJSON(activationDate, officeId, clientType.toString()), jsonAttributeToGetBack);
}
Expand Down Expand Up @@ -305,7 +304,7 @@ public static Integer addChargesForClient(final RequestSpecification requestSpec
final Integer clientId, final String request) {
System.out.println("--------------------------------- ADD CHARGES FOR Client --------------------------------");
final String ADD_CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/charges?" + Utils.TENANT_IDENTIFIER;
final HashMap response = Utils.performServerPost(requestSpec, responseSpec, ADD_CHARGES_URL, request, "");
final HashMap<?, ?> response = Utils.performServerPost(requestSpec, responseSpec, ADD_CHARGES_URL, request, "");
return (Integer) response.get("resourceId");
}

Expand All @@ -314,7 +313,7 @@ public static String payChargesForClients(final RequestSpecification requestSpec
System.out.println("--------------------------------- PAY CHARGES FOR CLIENT --------------------------------");
final String CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/charges/" + clientChargeId + "?command=paycharge&"
+ Utils.TENANT_IDENTIFIER;
final HashMap response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, json, "");
final HashMap<?, ?> response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, json, "");
return response.get("transactionId") != null ? response.get("transactionId").toString() : null;
}

Expand All @@ -324,7 +323,7 @@ public static String waiveChargesForClients(final RequestSpecification requestSp
final String CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/charges/" + clientChargeId + "?command=waive&"
+ Utils.TENANT_IDENTIFIER;

final HashMap response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, json, "");
final HashMap<?, ?> response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, json, "");
return response.get("transactionId").toString();
}

Expand All @@ -334,17 +333,17 @@ public static Integer revertClientChargeTransaction(final RequestSpecification r
final String CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/transactions/" + clientChargeId + "?command=undo&"
+ Utils.TENANT_IDENTIFIER;

final HashMap response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, "", "");
final HashMap<?, ?> response = Utils.performServerPost(requestSpec, responseSpec, CHARGES_URL, "", "");
return (Integer) response.get("resourceId");

}

public static Object getClientCharge(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String clientId) {
final String clientId, final String clientChargeId) {
System.out.println("---------------------------------GET CLIENT CHARGE---------------------------------------------");
final String CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/charges?paid=false&" + Utils.TENANT_IDENTIFIER;
ArrayList list = Utils.performServerGet(requestSpec, responseSpec, CHARGES_URL, "amountOutstanding");
return list != null && !list.isEmpty() ? list.get(0) : null;
final String CHARGES_URL = "/mifosng-provider/api/v1/clients/" + clientId + "/charges/" + clientChargeId + "?"
+ Utils.TENANT_IDENTIFIER;
return Utils.performServerGet(requestSpec, responseSpec, CHARGES_URL, "amountOutstanding");
}

public static Boolean getClientTransactions(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ public static SearchParameters forPagination(final Integer offset, final Integer
loanId, savingsId, orphansOnly);
}

public static SearchParameters forPagination(final Integer offset, final Integer limit) {

final Integer maxLimitAllowed = getCheckedLimit(limit);
final Long staffId = null;
final Long loanId = null;
final Long savingsId = null;
final Boolean orphansOnly = false;
final String orderBy = null;
final String sortOrder = null;

return new SearchParameters(null, null, null, null, null, null, null, offset, maxLimitAllowed, orderBy, sortOrder, staffId, null,
loanId, savingsId, orphansOnly);
}

public static SearchParameters forSavings(final String sqlSearch, final String externalId, final Integer offset, final Integer limit,
final String orderBy, final String sortOrder) {

Expand Down Expand Up @@ -171,10 +185,10 @@ private SearchParameters(final String sqlSearch, final Long officeId, final Stri
this.currencyCode = null;
}

public SearchParameters(final String sqlSearch, final Long officeId, final String externalId, final String name,
final String hierarchy, final String firstname, final String lastname, final Integer offset, final Integer limit,
final String orderBy, final String sortOrder, final Long staffId, final String accountNo, final Long loanId,
final Long savingsId, final Boolean orphansOnly, final String currencyCode) {
public SearchParameters(final String sqlSearch, final Long officeId, final String externalId, final String name, final String hierarchy,
final String firstname, final String lastname, final Integer offset, final Integer limit, final String orderBy,
final String sortOrder, final Long staffId, final String accountNo, final Long loanId, final Long savingsId,
final Boolean orphansOnly, final String currencyCode) {
this.sqlSearch = sqlSearch;
this.officeId = officeId;
this.externalId = externalId;
Expand Down Expand Up @@ -245,11 +259,11 @@ public String getSqlSearch() {
public Long getOfficeId() {
return this.officeId;
}
public String getCurrencyCode(){

public String getCurrencyCode() {
return this.currencyCode;
}

public String getExternalId() {
return this.externalId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.mifosplatform.infrastructure.core.service.Page;
import org.mifosplatform.infrastructure.core.service.SearchParameters;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.charge.data.ChargeData;
import org.mifosplatform.portfolio.charge.service.ChargeReadPlatformService;
Expand Down Expand Up @@ -89,9 +90,10 @@ public String retrieveAllClientCharges(@PathParam("clientId") final Long clientI
new Object[] { ClientApiConstants.CLIENT_CHARGE_QUERY_PARAM_STATUS_VALUE_ALL,
ClientApiConstants.CLIENT_CHARGE_QUERY_PARAM_STATUS_VALUE_ACTIVE,
ClientApiConstants.CLIENT_CHARGE_QUERY_PARAM_STATUS_VALUE_INACTIVE }); }
final SearchParameters searchParameters = SearchParameters.forPagination(offset, limit);

final Page<ClientChargeData> clientCharges = this.clientChargeReadPlatformService.retrieveClientCharges(clientId, chargeStatus,
pendingPayment, limit, offset);
pendingPayment, searchParameters);
return this.toApiJsonSerializer.serialize(settings, clientCharges, ClientApiConstants.CLIENT_CHARGES_RESPONSE_DATA_PARAMETERS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.mifosplatform.infrastructure.core.service.Page;
import org.mifosplatform.infrastructure.core.service.SearchParameters;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.client.data.ClientTransactionData;
import org.mifosplatform.portfolio.client.service.ClientTransactionReadPlatformService;
Expand Down Expand Up @@ -61,8 +62,10 @@ public ClientTransactionsApiResource(final PlatformSecurityContext context,
public String retrieveAllClientTransactions(@PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo,
@QueryParam("offset") final Integer offset, @QueryParam("limit") final Integer limit) {
this.context.authenticatedUser().validateHasReadPermission(ClientApiConstants.CLIENT_CHARGES_RESOURCE_NAME);

SearchParameters searchParameters = SearchParameters.forPagination(offset, limit);
final Page<ClientTransactionData> clientTransactions = this.clientTransactionReadPlatformService.retrieveAllTransactions(clientId,
limit, offset);
searchParameters);

final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, clientTransactions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.mifosplatform.portfolio.client.domain;

import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.client.exception.ClientNotActiveException;
import org.mifosplatform.portfolio.client.exception.ClientNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,10 +21,12 @@
public class ClientRepositoryWrapper {

private final ClientRepository repository;
private final PlatformSecurityContext context;

@Autowired
public ClientRepositoryWrapper(final ClientRepository repository) {
public ClientRepositoryWrapper(final ClientRepository repository, final PlatformSecurityContext context) {
this.repository = repository;
this.context = context;
}

public Client findOneWithNotFoundDetection(final Long id) {
Expand All @@ -44,9 +47,11 @@ public void delete(final Client client) {
this.repository.delete(client);
}

public Client getActiveClient(Long clientId) {
public Client getActiveClientInUserScope(Long clientId) {
final Client client = this.findOneWithNotFoundDetection(clientId);
if (client.isNotActive()) { throw new ClientNotActiveException(client.getId()); }
this.context.validateAccessRights(client.getOffice().getHierarchy());
return client;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package org.mifosplatform.portfolio.client.service;

import org.mifosplatform.infrastructure.core.service.Page;
import org.mifosplatform.infrastructure.core.service.SearchParameters;
import org.mifosplatform.portfolio.client.data.ClientChargeData;

public interface ClientChargeReadPlatformService {


Page<ClientChargeData> retrieveClientCharges(Long clientId, String status, Boolean isPaid, Integer limit, Integer offset);
Page<ClientChargeData> retrieveClientCharges(Long clientId, String status, Boolean pendingPayment, SearchParameters parameters);

ClientChargeData retrieveClientCharge(Long clientId, Long clientChargeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.mifosplatform.infrastructure.core.service.Page;
import org.mifosplatform.infrastructure.core.service.PaginationHelper;
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.infrastructure.core.service.SearchParameters;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
import org.mifosplatform.portfolio.charge.data.ChargeData;
Expand Down Expand Up @@ -118,8 +119,8 @@ public ClientChargeData retrieveClientCharge(Long clientId, Long clientChargeId)
}

@Override
public Page<ClientChargeData> retrieveClientCharges(Long clientId, String status, Boolean pendingPayment, Integer limit,
Integer offset) {
public Page<ClientChargeData> retrieveClientCharges(Long clientId, String status, Boolean pendingPayment,
SearchParameters searchParameters) {
final ClientChargeMapper rm = new ClientChargeMapper();
final StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("select SQL_CALC_FOUND_ROWS ").append(rm.schema()).append(" where cc.client_id=? ");
Expand All @@ -133,13 +134,21 @@ public Page<ClientChargeData> retrieveClientCharges(Long clientId, String status

// filter for paid charges
if (pendingPayment != null && pendingPayment) {
sqlBuilder.append(" and ( cc.is_paid_derived = 0 and cc.waived = 1) ");
sqlBuilder.append(" and ( cc.is_paid_derived = 0 and cc.waived = 0) ");
} else if (pendingPayment != null && !pendingPayment) {
sqlBuilder.append(" and (cc.is_paid_derived = 1 or cc.waived = 0) ");
sqlBuilder.append(" and (cc.is_paid_derived = 1 or cc.waived = 1) ");
}

sqlBuilder.append(" order by cc.charge_time_enum ASC, cc.charge_due_date DESC, cc.is_penalty ASC ");

// apply limit and offsets
if (searchParameters.isLimited()) {
sqlBuilder.append(" limit ").append(searchParameters.getLimit());
if (searchParameters.isOffset()) {
sqlBuilder.append(" offset ").append(searchParameters.getOffset());
}
}

sqlBuilder.append(
" order by cc.charge_time_enum ASC, cc.charge_due_date DESC, cc.is_penalty ASC " + "limit " + limit + " offset " + offset);
final String sqlCountRows = "SELECT FOUND_ROWS()";
return this.paginationHelper.fetchPage(this.jdbcTemplate, sqlCountRows, sqlBuilder.toString(), new Object[] { clientId },
this.clientChargeMapper);
Expand Down
Loading

0 comments on commit 8f1eb39

Please sign in to comment.