-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature#25: Hexagonal architecture improvement and refactoring respon…
…sabilities of services
- Loading branch information
1 parent
783be73
commit 6b72829
Showing
38 changed files
with
1,033 additions
and
608 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
application/src/main/java/com/cdx/bas/application/transaction/TransactionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ation/src/main/java/com/cdx/bas/application/transaction/TransactionStatusServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.cdx.bas.application.transaction; | ||
|
||
import com.cdx.bas.domain.transaction.Transaction; | ||
import com.cdx.bas.domain.transaction.TransactionException; | ||
import com.cdx.bas.domain.transaction.TransactionStatus; | ||
import com.cdx.bas.domain.transaction.TransactionStatusServicePort; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transactional; | ||
|
||
import java.util.Map; | ||
|
||
import static com.cdx.bas.domain.transaction.TransactionStatus.OUTSTANDING; | ||
import static com.cdx.bas.domain.transaction.TransactionStatus.UNPROCESSED; | ||
import static jakarta.transaction.Transactional.TxType.MANDATORY; | ||
|
||
@RequestScoped | ||
public class TransactionStatusServiceImpl implements TransactionStatusServicePort { | ||
|
||
@Inject | ||
TransactionRepository transactionPersistencePort; | ||
|
||
@Override | ||
@Transactional(value = MANDATORY) | ||
public Transaction setAsOutstanding(Transaction transaction) throws TransactionException { | ||
if (UNPROCESSED.equals(transaction.getStatus())) { | ||
transaction.setStatus(OUTSTANDING); | ||
} else { | ||
throw new TransactionException("Transaction is not longer unprocessed."); | ||
} | ||
return transactionPersistencePort.update(transaction); | ||
} | ||
|
||
@Override | ||
@Transactional(value = MANDATORY) | ||
public Transaction setStatus(Transaction transaction, TransactionStatus status, Map<String, String> metadata) throws TransactionException { | ||
if (transaction == null) { | ||
throw new TransactionException("Transaction is null."); | ||
} | ||
transaction.setStatus(status); | ||
transaction.setMetadata(metadata); | ||
return transactionPersistencePort.update(transaction); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...c/main/java/com/cdx/bas/application/transaction/TransactionTypeProcessingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.cdx.bas.application.transaction; | ||
|
||
import com.cdx.bas.application.bank.account.BankAccountServiceImpl; | ||
import com.cdx.bas.domain.bank.account.BankAccount; | ||
import com.cdx.bas.domain.bank.account.BankAccountException; | ||
import com.cdx.bas.domain.bank.account.BankAccountPersistencePort; | ||
import com.cdx.bas.domain.bank.account.BankAccountServicePort; | ||
import com.cdx.bas.domain.transaction.Transaction; | ||
import com.cdx.bas.domain.transaction.TransactionException; | ||
import com.cdx.bas.domain.transaction.TransactionProcessingServicePort; | ||
import com.cdx.bas.domain.transaction.TransactionStatusServicePort; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transactional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
import static com.cdx.bas.domain.transaction.TransactionStatus.*; | ||
import static com.cdx.bas.domain.transaction.TransactionStatus.REFUSED; | ||
import static jakarta.transaction.Transactional.TxType.REQUIRES_NEW; | ||
|
||
@RequestScoped | ||
public class TransactionProcessingServiceImpl implements TransactionProcessingServicePort { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BankAccountServiceImpl.class); | ||
|
||
@Inject | ||
TransactionStatusServicePort transactionStatusService; | ||
|
||
@Inject | ||
BankAccountServicePort bankAccountService; | ||
|
||
|
||
@Override | ||
@Transactional(value = REQUIRES_NEW) | ||
public Transaction credit(Transaction transaction) { | ||
logger.info("Transaction " + transaction.getId() + " processing..."); | ||
|
||
try { | ||
BankAccount emitterBankAccount = bankAccountService.findBankAccount(transaction.getSenderAccountId()); | ||
BankAccount receiverBankAccount = bankAccountService.findBankAccount(transaction.getReceiverAccountId()); | ||
Transaction currentTransaction = transactionStatusService.setAsOutstanding(transaction); | ||
|
||
logger.info("Process credit transaction " + currentTransaction.getId() | ||
+ " from bank account " + currentTransaction.getSenderAccountId() | ||
+ " with amount " + currentTransaction.getAmount() | ||
+ " to bank account " + currentTransaction.getReceiverAccountId()); | ||
|
||
Map<String, String> metadata = new HashMap<>(); | ||
metadata.put("sender_amount_before", emitterBankAccount.getBalance().getAmount().toString()); | ||
metadata.put("receiver_amount_before", receiverBankAccount.getBalance().getAmount().toString()); | ||
bankAccountService.postTransaction(currentTransaction, emitterBankAccount, receiverBankAccount); | ||
metadata.put("sender_amount_after", emitterBankAccount.getBalance().getAmount().toString()); | ||
metadata.put("receiver_amount_after", receiverBankAccount.getBalance().getAmount().toString()); | ||
Transaction completedTransaction = transactionStatusService.setStatus(currentTransaction, COMPLETED, metadata); | ||
BankAccount updatedEmitterBankAccount = bankAccountService.addTransaction(completedTransaction, emitterBankAccount); | ||
bankAccountService.updateBankAccount(receiverBankAccount); | ||
|
||
logger.info("Bank account " + updatedEmitterBankAccount.getId() + " deposit transaction " + + currentTransaction.getId() + " completed."); | ||
return completedTransaction; | ||
} catch (NoSuchElementException exception) { | ||
logger.error("Transaction " + transaction.getId() + " deposit error for amount "+ transaction.getAmount() + ": " + exception.getMessage()); | ||
Map<String, String> metadata = Map.of("error", exception.getMessage()); | ||
return transactionStatusService.setStatus(transaction, ERROR, metadata); | ||
} catch (TransactionException exception) { | ||
logger.error("Transaction " + transaction.getId() + " of " + transaction.getAmount() + " is invalid."); | ||
Map<String, String> metadata = Map.of("error", exception.getMessage()); | ||
return transactionStatusService.setStatus(transaction, REFUSED, metadata); | ||
} catch (BankAccountException exception) { | ||
logger.error("Transaction " + transaction.getId() + " deposit refused for amount "+ transaction.getAmount() + ": " + exception.getMessage()); | ||
Map<String, String> metadata = Map.of("error", exception.getMessage()); | ||
return transactionStatusService.setStatus(transaction, REFUSED, metadata); | ||
} | ||
} | ||
|
||
@Override | ||
public Transaction debit(Transaction transaction) { | ||
//TODO | ||
return null; | ||
} | ||
|
||
@Override | ||
public Transaction deposit(Transaction transaction) { | ||
//TODO | ||
return null; | ||
} | ||
|
||
@Override | ||
public Transaction withdraw(Transaction transaction) { | ||
//TODO | ||
return null; | ||
} | ||
} |
Oops, something went wrong.