Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package org.cardanofoundation.lob.app.accounting_reporting_core.repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;

import lombok.RequiredArgsConstructor;
Expand All @@ -20,7 +14,6 @@
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.LedgerDispatchStatus;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.TxItemValidationStatus;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.TransactionItemEntity;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.TransactionItemAggregateView;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -128,101 +121,4 @@ cc.Id.customerCode in (SELECT cc2.parent.Id.customerCode from CostCenter cc2 whe
return query.getResultList();
}

public long countItemsByAccountDateAggregated(String orgId, LocalDate dateFrom, LocalDate dateTo, Set<String> event, Set<String> currency, Optional<BigDecimal> minAmount, Optional<BigDecimal> maxAmount, Set<String> transactionHash) {
String jpql = """
SELECT COUNT(1) FROM accounting_reporting_core.TransactionItemEntity ti
JOIN ti.transaction te LEFT JOIN CostCenter cc ON ti.costCenter.customerCode = cc.id.customerCode
""";

String where = constructWhereClauseForExtraction(orgId, event, currency, minAmount, maxAmount, transactionHash);
String grouping = "GROUP BY ti.accountEvent.code, ti.fxRate, ti.project.customerCode, cc.parentCustomerCode, ti.document.num";
Query resultQuery = em.createQuery(jpql + where + grouping);

resultQuery.setParameter("dateFrom", dateFrom);
resultQuery.setParameter("dateTo", dateTo);

return resultQuery.getResultList().size();
}

public List<TransactionItemEntity> findByItemAccountDateAggregated(String orgId, LocalDate dateFrom, LocalDate dateTo, Set<String> event, Set<String> currency, Optional<BigDecimal> minAmount, Optional<BigDecimal> maxAmount, Set<String> transactionHash, int page, int limit) {

String jpql = """
SELECT NEW org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.TransactionItemAggregateView(MIN(ti.id), SUM(ti.amountLcy), SUM(ti.amountFcy))
FROM accounting_reporting_core.TransactionItemEntity ti
JOIN ti.transaction te LEFT JOIN CostCenter cc ON ti.costCenter.customerCode = cc.id.customerCode
""";

String where = constructWhereClauseForExtraction(orgId, event, currency, minAmount, maxAmount, transactionHash);

String grouping = "GROUP BY ti.accountEvent.code, ti.fxRate, ti.project.customerCode, cc.parentCustomerCode, ti.document.num";
Query resultQuery = em.createQuery(jpql + where + grouping);

resultQuery.setParameter("dateFrom", dateFrom);
resultQuery.setParameter("dateTo", dateTo);

// adding pagination
resultQuery.setFirstResult(page * limit);
resultQuery.setMaxResults(limit);
List<TransactionItemAggregateView> itemAggregateViews = resultQuery.getResultList();
Map<String, TransactionItemAggregateView> collect =
itemAggregateViews.stream().collect(Collectors.toMap(TransactionItemAggregateView::getTxId, item -> item));
List<TransactionItemEntity> allById = transactionItemRepository.findAllById(collect.keySet());
allById.stream().forEach(item -> {
TransactionItemAggregateView aggregateView = collect.get(item.getId());
if (aggregateView != null) {
item.setAmountFcy(aggregateView.getAmountFcyAggregated());
item.setAmountLcy(aggregateView.getAmountLcyAggregated());
}
});
return allById;
}

private static String constructWhereClauseForExtraction(String orgId, Set<String> event, Set<String> currency, Optional<BigDecimal> minAmount, Optional<BigDecimal> maxAmount, Set<String> transactionHash) {
minAmount = Optional.ofNullable(minAmount).orElse(Optional.empty());
maxAmount = Optional.ofNullable(maxAmount).orElse(Optional.empty());


String where = """
WHERE te.entryDate >= :dateFrom AND te.entryDate <= :dateTo
AND te.organisation.id = '%s'
AND ti.status = '%s'
""".formatted(orgId, TxItemValidationStatus.OK);

if (!event.isEmpty()) {
where += """
AND (ti.accountEvent.code in (%s) )
""".formatted(event.stream().map(code -> "'" + code + "'").collect(Collectors.joining(",")));
}

if (!currency.isEmpty()) {
where += """
AND (ti.document.currency.customerCode in (%s) )
""".formatted(currency.stream().map(code -> "'" + code + "'").collect(Collectors.joining(",")));
}

if (minAmount.isPresent()) {
where += """
AND ABS(ti.amountFcy) >= %s
""".formatted(minAmount.get());
}

if (maxAmount.isPresent()) {
where += """
AND ABS(ti.amountFcy) <= %s
""".formatted(maxAmount.get());
}

if (!transactionHash.isEmpty() && 0 < transactionHash.stream().count()) {
where += """
AND (te.ledgerDispatchReceipt.primaryBlockchainHash in (%s))
""".formatted(transactionHash.stream().map(code -> "'" + code + "'").collect(Collectors.joining(",")));
}

where += """
AND te.ledgerDispatchStatus = '%s'
""".formatted(LedgerDispatchStatus.FINALIZED);
return where;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty(value = "lob.accounting_reporting_core.enabled", havingValue = "true", matchIfMissing = true)
/**
* For now this controller + the services behind it will be set to internal only. It is now duplicated code to the indexer.
* This will be removed once we have a proper dashboard metric builder in the indexer.
*/
public class MetricController {

private final MetricService metricService;

@Tag(name = "Metrics", description = "Available Metrics")
@GetMapping(value = "/availableMetrics", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAuditorRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<MetricView> availableDashboards() {
return ResponseEntity.ok(new MetricView(metricService.getAvailableMetrics()));
}

@Tag(name = "Metrics", description = "Get Data from Metrics")
@PostMapping(value = "/data", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAuditorRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<MetricDataResponse> getDashboardData(@RequestBody GetMetricDataRequest getMetricDataRequest) {
return ResponseEntity.ok(new MetricDataResponse(metricService.getData(
getMetricDataRequest.getMetricView().getMetrics(),
Expand All @@ -68,6 +74,7 @@ public ResponseEntity<Boolean> saveDashboard(@RequestBody SaveDashboardRequest s

@Tag(name = "Dashboards", description = "Get Dashboards")
@GetMapping(value = "/dashboards/{organisationID}", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAuditorRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<List<DashboardView>> getDashboards(@PathVariable("organisationID") String organisationID) {
return ResponseEntity.ok(metricService.getAllDashboards(organisationID));
}
Expand Down

This file was deleted.

Loading