Skip to content
Snippets Groups Projects
Commit 913e94e2 authored by Filip Piták's avatar Filip Piták
Browse files

Implementation of process reversal

parent 6d597518
No related branches found
No related tags found
No related merge requests found
Showing
with 66 additions and 19 deletions
......@@ -5,7 +5,6 @@ import cz.muni.pa165.banking.transaction.processor.TransactionApi;
import cz.muni.pa165.banking.transaction.processor.dto.ProcessDetailDto;
import cz.muni.pa165.banking.transaction.processor.dto.ProcessDto;
import cz.muni.pa165.banking.transaction.processor.dto.TransactionDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
......@@ -32,11 +31,11 @@ public class TransactionController implements TransactionApi {
ProcessDetailDto result = facade.getStatus(xProcessUuid);
return ResponseEntity.ok(result);
}
@Override
public ResponseEntity<Void> revertTransactionProcess(UUID xProcessUuid) {
facade.revertProcess(xProcessUuid);
return new ResponseEntity<>(HttpStatus.OK);
public ResponseEntity<ProcessDto> revertTransactionProcess(UUID xProcessUuid) {
ProcessDto revertingProcess = facade.revertProcess(xProcessUuid);
return ResponseEntity.ok(revertingProcess);
}
}
......@@ -38,8 +38,8 @@ public class TransactionFacade {
return mapper.map(process, processTransaction);
}
public void revertProcess(UUID uuid) {
service.revertProcess(uuid);
public ProcessDto revertProcess(UUID uuid) {
return mapper.map(service.revertProcess(uuid));
}
}
......@@ -31,7 +31,7 @@ public class AccountServiceImpl implements AccountService {
}
@Override
public void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account) {
public void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account, String information) {
}
}
package cz.muni.pa165.banking.application.service;
import cz.muni.pa165.banking.application.messaging.ProcessProducer;
import cz.muni.pa165.banking.domain.money.Money;
import cz.muni.pa165.banking.domain.process.Process;
import cz.muni.pa165.banking.domain.process.ProcessFactory;
import cz.muni.pa165.banking.domain.process.ProcessTransaction;
import cz.muni.pa165.banking.domain.process.repository.ProcessRepository;
import cz.muni.pa165.banking.domain.process.status.Status;
import cz.muni.pa165.banking.domain.transaction.Transaction;
import cz.muni.pa165.banking.domain.process.repository.ProcessTransactionRepository;
import cz.muni.pa165.banking.domain.transaction.TransactionType;
import cz.muni.pa165.banking.exception.UnexpectedValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -15,6 +21,8 @@ import java.util.UUID;
@Service
public class TransactionProcessesService {
private final Logger LOGGER = LoggerFactory.getLogger(TransactionProcessesService.class);
private final ProcessTransactionRepository processTransactionRepository;
private final ProcessRepository processRepository;
......@@ -30,7 +38,11 @@ public class TransactionProcessesService {
@Transactional(rollbackFor = Exception.class)
public Process createProcessForTransaction(Transaction newTransaction) {
ProcessFactory factory = new ProcessFactory(processTransactionRepository, processRepository);
return factory.create(newTransaction, processProducer);
Process process = factory.create(newTransaction, processProducer);
LOGGER.info("[Create Process] %s" + process.uuid());
return process;
}
@Transactional(readOnly = true)
......@@ -44,8 +56,40 @@ public class TransactionProcessesService {
}
@Transactional(rollbackFor = Exception.class)
public void revertProcess(UUID uuid) {
public Process revertProcess(UUID uuid) {
Process process = processRepository.findById(uuid);
if (!process.getStatus().equals(Status.PROCESSED)) {
LOGGER.error("[Revert Process] Process " + uuid + " not finalized successfully, unable to revert");
throw new UnexpectedValueException(
"Unable to revert process, which is not successfully processed!",
process.toString()
);
}
ProcessTransaction processTransaction = processTransactionRepository.findTransactionByProcessId(uuid);
if (!processTransaction.getType().equals(TransactionType.CROSS_ACCOUNT) || processTransaction.getType().equals(TransactionType.SCHEDULED)) {
LOGGER.error("[Revert Process] Process " + uuid + " not of type CROSS_ACCOUNT/SCHEDULED, unable to revert");
throw new UnexpectedValueException("Unable to revert transaction not type of CROSS_ACCOUNT or SCHEDULED!");
}
Money original = processTransaction.getMoney();
Money reverted = new Money(original.getAmount().negate(), original.getCurrency());
Transaction revertingTransaction = new Transaction(
processTransaction.getTarget(),
processTransaction.getSource(),
processTransaction.getType(),
reverted,
String.format("Admin reversal of executed %s transaction {%s}", processTransaction.getType(), uuid)
);
ProcessFactory factory = new ProcessFactory(processTransactionRepository, processRepository);
Process revertingProcess = factory.create(revertingTransaction, processProducer);
LOGGER.info(String.format("[Revert Process] Created new process {%s} in order to revert process {%s}", revertingProcess.uuid(), uuid));
return revertingProcess;
}
}
......@@ -50,8 +50,8 @@ public class CrossAccountHandler extends ProcessHandler {
targetAmount = currencyConverter.convertTo(currency, targetAccountCurrency, targetAmount);
}
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.CROSS_ACCOUNT, sourceAmount, source);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.CROSS_ACCOUNT, targetAmount, target);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.CROSS_ACCOUNT, sourceAmount, source, processTransaction.getDetail());
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.CROSS_ACCOUNT, targetAmount, target, processTransaction.getDetail());
}
}
......@@ -30,7 +30,7 @@ class DepositHandler extends ProcessHandler {
BigDecimal convertedAmount = currencyConverter.convertTo(money.getCurrency(), accountCurrency, money.getAmount());
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.DEPOSIT, convertedAmount, account);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.DEPOSIT, convertedAmount, account, processTransaction.getDetail());
}
}
......@@ -46,8 +46,8 @@ public class ScheduledHandler extends ProcessHandler {
targetAmount = currencyConverter.convertTo(currency, targetAccountCurrency, targetAmount);
}
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.SCHEDULED, sourceAmount, source);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.SCHEDULED, targetAmount, target);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.SCHEDULED, sourceAmount, source, processTransaction.getDetail());
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.SCHEDULED, targetAmount, target, processTransaction.getDetail());
}
}
......@@ -42,7 +42,7 @@ public class WithdrawHandler extends ProcessHandler {
BigDecimal convertedAmount = currencyConverter.convertTo(money.getCurrency(), accountCurrency, money.getAmount());
BigDecimal calculatedAmount = convertedAmount.multiply(BigDecimal.valueOf(-1L));
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.WITHDRAW, calculatedAmount, account);
accountService.publishAccountChange(processTransaction.getUuid(), TransactionType.WITHDRAW, calculatedAmount, account, processTransaction.getDetail());
}
}
......@@ -30,6 +30,6 @@ public interface AccountService {
/**
* Publish transaction results to update balance of account within processed transaction
*/
void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account);
void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account, String information);
}
......@@ -54,7 +54,7 @@ paths:
required: true
responses:
'200':
description: xx
description: OK
content:
application/json:
schema:
......@@ -80,7 +80,11 @@ paths:
required: true
responses:
'200':
description: Process found and reverted changes in account(s)
description: Process found and created new process reverting changes in accounts
content:
application/json:
schema:
$ref: '#/components/schemas/ProcessDto'
'400':
description: Process found by UUID but not in expected state (PROCESSED)
'404':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment