Skip to content

Commit

Permalink
improved response handling and removed initial allocation field set
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Nov 29, 2024
1 parent 113e6cc commit a12f4d0
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 24 deletions.
15 changes: 8 additions & 7 deletions src/main/java/org/folio/dao/fund/FundPostgresDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
public class FundPostgresDAO implements FundDAO {
private static final Logger logger = LogManager.getLogger();
private static final String FUND_NOT_FOUND = "Fund not found, id=%s";
private static final String QUERY_UPDATE_CURRENT_FY_BUDGET =
"UPDATE %s SET jsonb = jsonb_set(jsonb,'{budgetStatus}', $1) " +
"WHERE((fundId=$2) " +
"AND (budget.fiscalYearId IN " +
"(SELECT id FROM %s WHERE current_date between (jsonb->>'periodStart')::timestamp " +
"AND (jsonb->>'periodEnd')::timestamp)));";

@Override
public Future<Fund> getFundById(String id, DBConn conn) {
Expand Down Expand Up @@ -64,14 +70,9 @@ public Future<Boolean> isFundStatusChanged(Fund fund, DBConn conn) {
public Future<Void> updateRelatedCurrentFYBudgets(Fund fund, DBConn conn) {
String fullBudgetTableName = getFullTableName(conn.getTenantId(), BUDGET_TABLE);
String fullFYTableName = getFullTableName(conn.getTenantId(), FISCAL_YEAR_TABLE);
String updateQuery = String.format(QUERY_UPDATE_CURRENT_FY_BUDGET, fullBudgetTableName, fullFYTableName);

String sql = "UPDATE " + fullBudgetTableName + " SET jsonb = jsonb_set(jsonb,'{budgetStatus}', $1) " +
"WHERE((fundId=$2) " +
"AND (budget.fiscalYearId IN " +
"(SELECT id FROM " + fullFYTableName + " WHERE current_date between (jsonb->>'periodStart')::timestamp " +
"AND (jsonb->>'periodEnd')::timestamp)));";

return conn.execute(sql, Tuple.of(fund.getFundStatus().value(), UUID.fromString(fund.getId())))
return conn.execute(updateQuery, Tuple.of(fund.getFundStatus().value(), UUID.fromString(fund.getId())))
.mapEmpty();
}

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/folio/rest/impl/FundAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
import org.apache.logging.log4j.Logger;
import org.folio.rest.annotations.Validate;
import org.folio.rest.core.model.RequestContext;
import org.folio.rest.exception.HttpException;
import org.folio.rest.jaxrs.model.Fund;
import org.folio.rest.jaxrs.model.FundCollection;
import org.folio.rest.jaxrs.resource.FinanceStorageFunds;
import org.folio.rest.persist.HelperUtils;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.util.ResponseUtils;
import org.folio.service.fund.FundService;
import org.folio.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -68,14 +67,7 @@ public void putFinanceStorageFundsById(String id, Fund fund, Map<String, String>
logger.debug("Trying to update finance storage fund by id {}", id);
fund.setId(id);
fundService.updateFund(fund, new RequestContext(vertxContext, okapiHeaders))
.onComplete(result -> {
if (result.failed()) {
HttpException cause = (HttpException) result.cause();
logger.error("Failed to update the finance storage fund with Id {}", fund.getId(), cause);
HelperUtils.replyWithErrorResponse(asyncResultHandler, cause);
} else {
asyncResultHandler.handle(succeededFuture(respond204()));
}
});
.onSuccess(result -> asyncResultHandler.handle(succeededFuture(respond204())))
.onFailure(ResponseUtils::handleFailure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private Budget setNewValues(Budget budget, FyFinanceDataCollection entity) {
.orElseThrow();

return budget.withBudgetStatus(Budget.BudgetStatus.fromValue(budgetFinanceData.getBudgetStatus().value()))
.withInitialAllocation(budgetFinanceData.getBudgetInitialAllocation())
.withAllowableExpenditure(budgetFinanceData.getBudgetAllowableExpenditure())
.withAllowableEncumbrance(budgetFinanceData.getBudgetAllowableEncumbrance());
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/folio/rest/impl/FinanceDataApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public void positive_testUpdateFinanceData() {
fyFinanceData.setFundDescription(expectedDescription);
fyFinanceData.setFundTags(new FundTags().withTagList(expectedTags));
fyFinanceData.setBudgetStatus(expectedBudgetStatus);
fyFinanceData.setBudgetInitialAllocation(expectedNumber);
fyFinanceData.setBudgetAllowableExpenditure(expectedNumber);
fyFinanceData.setBudgetAllowableEncumbrance(expectedNumber);

Expand All @@ -130,7 +129,6 @@ public void positive_testUpdateFinanceData() {

assertEquals(expectedDescription, updatedFyFinanceData.getFundDescription());
assertEquals(expectedTags, updatedFyFinanceData.getFundTags().getTagList());
assertEquals(expectedNumber, updatedFyFinanceData.getBudgetInitialAllocation());
assertEquals(expectedNumber, updatedFyFinanceData.getBudgetAllowableEncumbrance());
assertEquals(expectedNumber, updatedFyFinanceData.getBudgetAllowableExpenditure());
}
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/folio/rest/impl/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.io.InputStream;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ private void verifyBudgetUpdates(FyFinanceDataCollection collection) {
Budget updatedBudget = budgetCaptor.getValue().get(0);

assertNotEquals("NAME CHANGED", updatedBudget.getName());
assertNotEquals(1000.0, updatedBudget.getInitialAllocation());

assertEquals(FyFinanceData.BudgetStatus.INACTIVE.value(), updatedBudget.getBudgetStatus().value());
assertEquals(1000.0, updatedBudget.getInitialAllocation());
assertEquals(800.0, updatedBudget.getAllowableExpenditure());
assertEquals(700.0, updatedBudget.getAllowableEncumbrance());
}
Expand Down

0 comments on commit a12f4d0

Please sign in to comment.