Skip to content

Commit 9b79c95

Browse files
authored
Merge pull request #6 from cardano-foundation/feat/pagination_page_zero
fix: pagination fix
2 parents 01d5b48 + 1de306b commit 9b79c95

File tree

8 files changed

+66
-26
lines changed

8 files changed

+66
-26
lines changed

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/CustomTransactionBatchRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
public interface CustomTransactionBatchRepository {
99
List<TransactionBatchEntity> findByFilter(BatchSearchRequest body);
10+
11+
Long findByFilterCount(BatchSearchRequest body);
1012
}

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/CustomTransactionBatchRepositoryImpl.java

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.BatchSearchRequest;
1717
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.LedgerDispatchStatusView;
1818

19+
import java.time.LocalDateTime;
1920
import java.time.YearMonth;
2021
import java.util.ArrayList;
22+
import java.util.Collection;
2123
import java.util.List;
2224

2325
import static org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.LedgerDispatchStatus.DISPATCHED;
@@ -34,6 +36,50 @@ public List<TransactionBatchEntity> findByFilter(BatchSearchRequest body) {
3436
CriteriaBuilder builder = em.getCriteriaBuilder();
3537
CriteriaQuery<TransactionBatchEntity> criteriaQuery = builder.createQuery(TransactionBatchEntity.class);
3638
Root<TransactionBatchEntity> rootEntry = criteriaQuery.from(TransactionBatchEntity.class);
39+
40+
Collection<Predicate> andPredicates = queryCriteria(rootEntry, builder, body);
41+
42+
criteriaQuery.select(rootEntry);
43+
criteriaQuery.where(andPredicates.toArray(new Predicate[0]));
44+
criteriaQuery.orderBy(builder.desc(rootEntry.get("createdAt")));
45+
// Without this line the query only returns one row.
46+
criteriaQuery.groupBy(rootEntry.get("id"));
47+
48+
TypedQuery<TransactionBatchEntity> theQuery = em.createQuery(criteriaQuery);
49+
50+
theQuery.setMaxResults(body.getLimit());
51+
52+
if (null != body.getPage() && 0 < body.getPage()) {
53+
theQuery.setFirstResult(body.getPage() * body.getLimit());
54+
}
55+
56+
return theQuery.getResultList();
57+
58+
}
59+
60+
@Override
61+
public Long findByFilterCount(BatchSearchRequest body) {
62+
63+
CriteriaBuilder builder = em.getCriteriaBuilder();
64+
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
65+
Root<TransactionBatchEntity> rootEntry = criteriaQuery.from(TransactionBatchEntity.class);
66+
Collection<Predicate> andPredicates = queryCriteria(rootEntry, builder, body);
67+
68+
69+
criteriaQuery.select(builder.count(rootEntry));
70+
criteriaQuery.where(andPredicates.toArray(new Predicate[0]));
71+
criteriaQuery.orderBy(builder.desc(rootEntry.get("createdAt")));
72+
// Without this line the query only returns one row.
73+
criteriaQuery.groupBy(rootEntry.get("id"));
74+
75+
TypedQuery<Long> theQuery = em.createQuery(criteriaQuery);
76+
77+
78+
return theQuery.getResultList().stream().count();
79+
80+
}
81+
82+
private Collection<Predicate> queryCriteria(Root<TransactionBatchEntity> rootEntry, CriteriaBuilder builder, BatchSearchRequest body) {
3783
List<Predicate> andPredicates = new ArrayList<>();
3884

3985
andPredicates.add(builder.equal(rootEntry.get("filteringParameters").get("organisationId"), body.getOrganisationId()));
@@ -72,35 +118,22 @@ public List<TransactionBatchEntity> findByFilter(BatchSearchRequest body) {
72118
}
73119

74120
if (null != body.getFrom()) {
75-
andPredicates.add(builder.greaterThanOrEqualTo(rootEntry.get("filteringParameters").get("from"), body.getFrom()));
121+
LocalDateTime localDateTime1 = body.getFrom().atStartOfDay();
122+
andPredicates.add(builder.greaterThanOrEqualTo(rootEntry.get("createdAt"), localDateTime1));
123+
76124
}
77125

78126
if (null != body.getTo()) {
79-
andPredicates.add(builder.lessThanOrEqualTo(rootEntry.get("filteringParameters").get("to"), body.getTo()));
127+
LocalDateTime localDateTime2 = body.getTo().atTime(23,59, 59);
128+
andPredicates.add(builder.lessThanOrEqualTo(rootEntry.get("createdAt"), localDateTime2));
80129
}
81130

82131
if (!body.getTxStatus().isEmpty()) {
83132
Join<TransactionBatchEntity, TransactionEntity> transactionEntityJoin = rootEntry.join("transactions", JoinType.INNER);
84133
andPredicates.add(builder.in(transactionEntityJoin.get("status")).value(body.getTxStatus()));
85134
}
86135

87-
criteriaQuery.select(rootEntry);
88-
criteriaQuery.where(andPredicates.toArray(new Predicate[0]));
89-
criteriaQuery.orderBy(builder.desc(rootEntry.get("createdAt")));
90-
// Without this line the query only returns one row.
91-
criteriaQuery.groupBy(rootEntry.get("id"));
92-
93-
TypedQuery<TransactionBatchEntity> theQuery = em.createQuery(criteriaQuery);
94-
95-
theQuery.setMaxResults(body.getLimit());
96-
97-
if (null != body.getPage() && 0 < body.getPage()) {
98-
body.setPage(body.getPage() - 1);
99-
theQuery.setFirstResult(body.getPage() * body.getLimit());
100-
}
101-
102-
return theQuery.getResultList();
103-
136+
return andPredicates;
104137
}
105138

106139
}

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/TransactionBatchRepositoryGateway.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public List<TransactionBatchEntity> findByOrganisationId(String organisationId)
3232
public List<TransactionBatchEntity> findByFilter(BatchSearchRequest body) {
3333
return transactionBatchRepository.findByFilter(body);
3434
}
35+
36+
public Long findByFilterCount(BatchSearchRequest body) {
37+
return transactionBatchRepository.findByFilterCount(body);
38+
}
3539
}

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/AccountingCoreResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public ResponseEntity<?> extractionTrigger(@Valid @RequestBody ExtractionRequest
168168
}
169169
)
170170
public ResponseEntity<?> listAllBatch(@Valid @RequestBody BatchSearchRequest body,
171-
@RequestParam(name = "page", defaultValue = "1") int page,
171+
@RequestParam(name = "page", defaultValue = "0") int page,
172172
@RequestParam(name = "limit", defaultValue = "10") int limit) {
173173
body.setLimit(limit);
174174
body.setPage(page);

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/model/AccountingCorePresentationViewService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ public BatchsDetailView listAllBatch(BatchSearchRequest body) {
117117
Set.of()
118118
)
119119
).toList();
120+
120121
batchDetail.setBatchs(batches);
121-
batchDetail.setTotal(batchDetail.getBatchs().stream().count());
122+
batchDetail.setTotal(Long.valueOf(transactionBatchRepositoryGateway.findByFilterCount(body)));
122123
return batchDetail;
123124
}
124125

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/model/AccountingCoreResourceService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public boolean checkFromToDates(Organisation org,
3030
LocalDate dateToObj = LocalDate.parse(dateTo);
3131

3232
LocalDate today = LocalDate.now();
33-
LocalDate monthsAgo = today.minusMonths(org.getAccountPeriodMonths());
34-
LocalDate yesterday = today.minusDays(1);
33+
LocalDate monthsAgo = today.minusMonths(org.getAccountPeriodMonths()).minusDays(1);
3534

36-
return dateFromObj.isAfter(monthsAgo) && dateToObj.isBefore(yesterday);
35+
return dateFromObj.isAfter(monthsAgo) && dateToObj.isBefore(today);
3736
}
3837

3938
}

accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/CustomTransactionBatchRepositoryImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void findByFilterTest() {
4040

4141
BatchSearchRequest body = new BatchSearchRequest();
4242
body.setLimit(10);
43-
body.setPage(2);
43+
body.setPage(1);
4444
body.setOrganisationId("TestOrgId");
4545
body.setBatchStatistics(Set.of(LedgerDispatchStatusView.APPROVE, LedgerDispatchStatusView.PENDING, LedgerDispatchStatusView.INVALID, LedgerDispatchStatusView.PUBLISH, LedgerDispatchStatusView.PUBLISHED));
4646
body.setTxStatus(Set.of(TransactionStatus.OK));
@@ -84,7 +84,7 @@ void findByFilterAllTest() {
8484

8585
BatchSearchRequest body = new BatchSearchRequest();
8686
body.setLimit(10);
87-
body.setPage(2);
87+
body.setPage(1);
8888
body.setOrganisationId("TestOrgId");
8989

9090
Mockito.when(em.getCriteriaBuilder()).thenReturn(builder);

accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/model/AccountingCorePresentationConverterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ void testListAllBatchModel() {
227227
transactionBatchEntity.setBatchStatistics(batchStatistics);
228228

229229
when(transactionBatchRepositoryGateway.findByFilter(batchSearchRequest)).thenReturn(List.of(transactionBatchEntity));
230+
when(transactionBatchRepositoryGateway.findByFilterCount(batchSearchRequest)).thenReturn(Long.valueOf(1));
230231

231232
BatchsDetailView batchsDetailView = accountingCorePresentationConverter.listAllBatch(batchSearchRequest);
232233
List<BatchView> result = batchsDetailView.getBatchs();

0 commit comments

Comments
 (0)