Skip to content

Commit 0d942c7

Browse files
authored
Merge pull request #7 from cardano-foundation/feat/approva_transactions_endpoint
feat: Approve transaction
2 parents 6f1cc8e + e8cdeda commit 0d942c7

File tree

6 files changed

+141
-11
lines changed

6 files changed

+141
-11
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
import lombok.extern.slf4j.Slf4j;
1313
import lombok.val;
1414
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.TransactionType;
15-
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.Rejection;
1615
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.RejectionCode;
1716
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.model.AccountingCorePresentationViewService;
1817
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.model.AccountingCoreResourceService;
1918
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.BatchSearchRequest;
2019
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.ExtractionRequest;
2120
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.SearchRequest;
21+
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.TransactionsApprove;
2222
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.BatchView;
2323
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.BatchsDetailView;
24+
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.TransactionProcessView;
2425
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.TransactionView;
2526
import org.json.JSONArray;
2627
import org.json.JSONException;
@@ -63,14 +64,14 @@ public ResponseEntity<?> listAllAction(@Valid @RequestBody SearchRequest body) {
6364
{@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = TransactionView.class))}
6465
)
6566
})
66-
@GetMapping(value = "/transactions/{transactionId}", produces = MediaType.APPLICATION_JSON_VALUE)
67-
public ResponseEntity<?> transactionDetailSpecific(@Valid @PathVariable("transactionId") @Parameter(example = "7e9e8bcbb38a283b41eab57add98278561ab51d23a16f3e3baf3daa461b84ab4") String transactionId) {
67+
@GetMapping(value = "/transactions/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
68+
public ResponseEntity<?> transactionDetailSpecific(@Valid @PathVariable("id") @Parameter(example = "7e9e8bcbb38a283b41eab57add98278561ab51d23a16f3e3baf3daa461b84ab4") String id) {
6869

69-
val transactionEntity = accountingCorePresentationService.transactionDetailSpecific(transactionId);
70+
val transactionEntity = accountingCorePresentationService.transactionDetailSpecific(id);
7071
if (transactionEntity.isEmpty()) {
7172
val issue = Problem.builder()
7273
.withTitle("TX_NOT_FOUND")
73-
.withDetail(STR."Transaction with id: {\{transactionId}} could not be found")
74+
.withDetail(STR."Transaction with id: {\{id}} could not be found")
7475
.withStatus(Status.NOT_FOUND)
7576
.build();
7677

@@ -105,7 +106,7 @@ public ResponseEntity<?> transactionType() throws JSONException {
105106
@Tag(name = "Transactions", description = "Transactions API")
106107
@Operation(description = "Rejection types", responses = {
107108
@ApiResponse(content =
108-
{@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, array = @ArraySchema(schema = @Schema(implementation = RejectionCode.class)))}
109+
{@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, array = @ArraySchema(schema = @Schema(implementation = RejectionCode.class)))}
109110
)
110111
})
111112
@GetMapping(value = "/rejection-types", produces = MediaType.APPLICATION_JSON_VALUE, name = "Rejection types")
@@ -158,6 +159,29 @@ public ResponseEntity<?> extractionTrigger(@Valid @RequestBody ExtractionRequest
158159
.body(response.toString());
159160
}
160161

162+
@Tag(name = "Transactions", description = "Transactions API")
163+
@PostMapping(value = "/transactions/approve", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
164+
@Operation(description = "Approve transactions",
165+
responses = {
166+
@ApiResponse(content = {
167+
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, array = @ArraySchema(schema = @Schema(implementation = TransactionProcessView.class)))
168+
})
169+
}
170+
)
171+
public ResponseEntity<?> approveTransaction(@Valid @RequestBody TransactionsApprove transactionsApprove) {
172+
val resul = accountingCorePresentationService.approveTransactions(transactionsApprove.getTransactionApproves());
173+
174+
if (resul.isEmpty()) {
175+
return ResponseEntity
176+
.status(HttpStatusCode.valueOf(404))
177+
.body(resul);
178+
}
179+
180+
return ResponseEntity
181+
.status(HttpStatusCode.valueOf(202))
182+
.body(resul);
183+
}
184+
161185
@Tag(name = "Batchs", description = "Batchs API")
162186
@PostMapping(value = "/batchs", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
163187
@Operation(description = "Batch list",

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.model;
22

3+
import io.vavr.control.Either;
34
import lombok.RequiredArgsConstructor;
45
import lombok.extern.slf4j.Slf4j;
56
import lombok.val;
@@ -10,20 +11,21 @@
1011
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.BatchSearchRequest;
1112
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.ExtractionRequest;
1213
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.SearchRequest;
14+
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.TransactionApprove;
1315
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.*;
1416
import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.AccountingCoreService;
1517
import org.jmolecules.ddd.annotation.Service;
18+
import org.springframework.dao.DataAccessException;
1619
import org.springframework.transaction.annotation.Transactional;
20+
import org.zalando.problem.Problem;
21+
import org.zalando.problem.Status;
22+
import org.zalando.problem.ThrowableProblem;
1723

1824
import java.math.BigDecimal;
1925
import java.time.LocalDate;
2026
import java.util.*;
2127
import java.util.stream.Collectors;
2228

23-
import static org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.Source.ERP;
24-
import static org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.Violation.Severity.ERROR;
25-
import static org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.ViolationCode.AMOUNT_FCY_IS_ZERO;
26-
2729
@Service
2830
@org.springframework.stereotype.Service
2931
@Slf4j
@@ -137,6 +139,36 @@ public void extractionTrigger(ExtractionRequest body) {
137139

138140
}
139141

142+
public List<TransactionProcessView> approveTransactions(List<TransactionApprove> transactionApproves) {
143+
144+
val transactionProcessViews = new ArrayList<TransactionProcessView>(List.of());
145+
for (val transactionAp : transactionApproves) {
146+
147+
try {
148+
Either<Problem, Boolean> approveTransactionE = accountingCoreService.approveTransaction(transactionAp.getId());
149+
val resu = approveTransactionE.fold(problem -> {
150+
return TransactionProcessView.createFail(transactionAp.getId(), problem);
151+
}, success -> {
152+
return TransactionProcessView.createSucess(transactionAp.getId());
153+
});
154+
155+
transactionProcessViews.add(resu);
156+
} catch (DataAccessException exception) {
157+
val problem = Problem.builder()
158+
.withTitle("TRANSACTION_DB_ERROR")
159+
.withDetail(STR."DAtabse serialsation problem for the ID: \{transactionAp.getId()}")
160+
.with("transactionId", transactionAp.getId())
161+
.withStatus(Status.INTERNAL_SERVER_ERROR)
162+
.with("cause", exception.getMessage())
163+
.build();
164+
transactionProcessViews.add(TransactionProcessView.createFail(transactionAp.getId(), problem));
165+
}
166+
167+
}
168+
169+
return transactionProcessViews;
170+
}
171+
140172
private Set<TransactionView> getTransaction(TransactionBatchEntity transactionBatchEntity) {
141173
return transactionBatchEntity.getTransactions().stream()
142174
.map(this::getTransactionView)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class TransactionApprove {
14+
//48335c2b63cffcef2a3cd0678b65c4fb16420f51110033024209957fbd58ec4e
15+
@Schema(example = "7e9e8bcbb38a283b41eab57add98278561ab51d23a16f3e3baf3daa461b84ab4")
16+
private String id;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests;
2+
3+
import io.swagger.v3.oas.annotations.media.ArraySchema;
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
import lombok.Setter;
9+
10+
import java.util.List;
11+
12+
@Getter
13+
@Setter
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class TransactionsApprove {
17+
@ArraySchema(arraySchema = @Schema(example = "[ {" +
18+
"\"id\": \"7e9e8bcbb38a283b41eab57add98278561ab51d23a16f3e3baf3daa461b84ab4\"}," +
19+
"{\"id\": \"7bce71783ff8e6501b33ce9797097f5633c069f17e4731d96467cdb311693fcb\"}," +
20+
"{\"id\": \"38e7e04304c86c1156128f7bdc548d51f175d5bdf83df1b3edda1832cac385dd\"}," +
21+
"{\"id\": \"95b5fb0d3ea32847d9d6bda2ff9da0be11bd5ba3175aad6f3cacafd14f9d28a3\"}," +
22+
"{\"id\": \"8b346f4d914fe652bde477fa3f6b630fbcf7ffd9859daf8df4fc63cdd1562e5c\"}," +
23+
"{\"id\": \"48335c2b63cffcef2a3cd0678b65c4fb16420f51110033024209957fbd58ec4e\"}" +
24+
"]"))
25+
26+
private List<TransactionApprove> transactionApproves;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.views;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import org.zalando.problem.Problem;
8+
9+
import java.util.Optional;
10+
11+
@Getter
12+
@Setter
13+
@AllArgsConstructor
14+
public class TransactionProcessView {
15+
16+
private String id;
17+
18+
private Boolean success;
19+
20+
private Optional<Problem> error;
21+
22+
public static TransactionProcessView createSucess(String id) {
23+
return new TransactionProcessView(id, true, Optional.empty());
24+
}
25+
26+
public static TransactionProcessView createFail(String id, Problem error) {
27+
return new TransactionProcessView(id, false, Optional.of(error));
28+
}
29+
}

accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/AccountingCoreService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.cardanofoundation.lob.app.accounting_reporting_core.service.business_rules.ProcessorFlags;
1212
import org.springframework.context.ApplicationEventPublisher;
1313
import org.springframework.stereotype.Service;
14+
import org.springframework.transaction.annotation.Propagation;
1415
import org.springframework.transaction.annotation.Transactional;
1516
import org.zalando.problem.Problem;
1617

@@ -83,7 +84,7 @@ public Either<Problem, Boolean> scheduleReIngestionForFailed(String batchId) {
8384
return Either.right(true);
8485
}
8586

86-
@Transactional
87+
@Transactional(propagation = Propagation.REQUIRES_NEW)
8788
public Either<Problem, Boolean> approveTransaction(String txId) {
8889
return transactionRepositoryGateway.approveTransaction(txId);
8990
}

0 commit comments

Comments
 (0)