Skip to content
Permalink
Browse files
improved exception handling
  • Loading branch information
mgeiss committed Aug 4, 2017
1 parent 11d8f58 commit cae3aff05c26d296636a8c14e1c8c8c2a592e10d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
@@ -24,7 +24,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestTellerManagement.class,
TestTellerOperation.class,
TestTellerOperation.class
})
public class TestSuite extends SuiteTestEnvironment {
}
@@ -15,8 +15,11 @@
*/
package io.mifos.teller.service.internal.service.helper;

import io.mifos.core.api.util.NotFoundException;
import io.mifos.core.lang.ServiceException;
import io.mifos.individuallending.api.v1.domain.product.AccountDesignators;
import io.mifos.individuallending.api.v1.domain.workflow.Action;
import io.mifos.office.api.v1.client.BadRequestException;
import io.mifos.portfolio.api.v1.client.PortfolioManager;
import io.mifos.portfolio.api.v1.domain.AccountAssignment;
import io.mifos.portfolio.api.v1.domain.ChargeDefinition;
@@ -52,35 +55,47 @@ public List<Charge> getCharges(final String productIdentifier, final String case

final List<Charge> charges = new ArrayList<>();

final List<CostComponent> costComponentsForAction =
this.portfolioManager.getCostComponentsForAction(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name());

costComponentsForAction.forEach(costComponent -> {
final ChargeDefinition chargeDefinition =
this.portfolioManager.getChargeDefinition(productIdentifier, costComponent.getChargeIdentifier());

final Charge charge = new Charge();
charge.setCode(chargeDefinition.getIdentifier());
charge.setName(chargeDefinition.getName());
charge.setAmount(costComponent.getAmount().doubleValue());
charges.add(charge);
});
try {
final List<CostComponent> costComponentsForAction =
this.portfolioManager.getCostComponentsForAction(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name());

costComponentsForAction.forEach(costComponent -> {
final ChargeDefinition chargeDefinition =
this.portfolioManager.getChargeDefinition(productIdentifier, costComponent.getChargeIdentifier());

final Charge charge = new Charge();
charge.setCode(chargeDefinition.getIdentifier());
charge.setName(chargeDefinition.getName());
charge.setAmount(costComponent.getAmount().doubleValue());
charges.add(charge);
});
} catch (final NotFoundException | BadRequestException ex) {
throw ServiceException.internalError(
"Could not fetch portfolio information, reason: {0}", ex.getCause() + " - " + ex.getMessage()
);
}

return charges;
}

public void processRepayment(final String productIdentifier, final String caseIdentifier, final String tellerAccount) {

final List<CostComponent> costComponentsForAction =
this.portfolioManager.getCostComponentsForAction(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name());

final Command repaymentCommand = new Command();
repaymentCommand.setOneTimeAccountAssignments(this.getAccountAssignments(tellerAccount));
repaymentCommand.setPaymentSize(BigDecimal.valueOf(
costComponentsForAction.stream().mapToDouble(value -> value.getAmount().doubleValue()).sum())
);

portfolioManager.executeCaseCommand(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name(), repaymentCommand);
try {
final List<CostComponent> costComponentsForAction =
this.portfolioManager.getCostComponentsForAction(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name());

final Command repaymentCommand = new Command();
repaymentCommand.setOneTimeAccountAssignments(this.getAccountAssignments(tellerAccount));
repaymentCommand.setPaymentSize(BigDecimal.valueOf(
costComponentsForAction.stream().mapToDouble(value -> value.getAmount().doubleValue()).sum())
);

portfolioManager.executeCaseCommand(productIdentifier, caseIdentifier, Action.ACCEPT_PAYMENT.name(), repaymentCommand);
} catch (final NotFoundException | BadRequestException ex) {
throw ServiceException.internalError(
"Could not process repayment, reason: {0}", ex.getCause() + " - " + ex.getMessage()
);
}
}

private List<AccountAssignment> getAccountAssignments(final String tellerAccount) {

0 comments on commit cae3aff

Please sign in to comment.