From cb4d4c21243c51cf8c81f30fd32b8c196c5de813 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filip=20Pit=C3=A1k?= <xpitak@fi.muni.cz>
Date: Tue, 23 Apr 2024 22:10:54 +0200
Subject: [PATCH] Implement service for processing remote calls

---
 .../banking/application/mapper/DtoMapper.java |  8 ++--
 .../application/proxy/AccountApiProxy.java    |  8 ++++
 .../proxy/SystemServiceApiProxy.java          |  8 ++++
 .../service/AccountServiceImpl.java           | 41 +++++++++++++++----
 4 files changed, 54 insertions(+), 11 deletions(-)
 create mode 100644 transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/AccountApiProxy.java
 create mode 100644 transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/SystemServiceApiProxy.java

diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/mapper/DtoMapper.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/mapper/DtoMapper.java
index 91672d3..61a32d6 100644
--- a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/mapper/DtoMapper.java
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/mapper/DtoMapper.java
@@ -36,18 +36,18 @@ public interface DtoMapper {
     default ProcessDto map(Process source) {
         ProcessDto dto = new ProcessDto();
         
-        dto.setIdentifier(source.uuid());
+        dto.setIdentifier(source.getUuid());
         dto.setStatus(map(source.getStatus()));
-        dto.setInfo(source.getStatusInformation());
+        dto.setInfo(source.getInformation());
         
         return dto;
     }
     
     default ProcessDetailDto map(Process process, Transaction transaction) {
         ProcessDetailDto dto = new ProcessDetailDto();
-        dto.identifier(process.uuid());
+        dto.identifier(process.getUuid());
         dto.status(map(process.getStatus()));
-        dto.info(process.getStatusInformation());
+        dto.info(process.getInformation());
         dto.source(map(transaction.getSource()));
         
         if (transaction.getTarget() != null) {
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/AccountApiProxy.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/AccountApiProxy.java
new file mode 100644
index 0000000..32a821e
--- /dev/null
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/AccountApiProxy.java
@@ -0,0 +1,8 @@
+package cz.muni.pa165.banking.application.proxy;
+
+import cz.muni.pa165.banking.account.management.AccountApi;
+import org.springframework.cloud.openfeign.FeignClient;
+
+@FeignClient(url = "${banking.apps.management.url}", name = "AccountApi")
+public interface AccountApiProxy extends AccountApi {
+}
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/SystemServiceApiProxy.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/SystemServiceApiProxy.java
new file mode 100644
index 0000000..abe170c
--- /dev/null
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/proxy/SystemServiceApiProxy.java
@@ -0,0 +1,8 @@
+package cz.muni.pa165.banking.application.proxy;
+
+import cz.muni.pa165.banking.account.query.SystemServiceApi;
+import org.springframework.cloud.openfeign.FeignClient;
+
+@FeignClient(url = "${banking.apps.query.url}", name = "SystemServiceApi")
+public interface SystemServiceApiProxy extends SystemServiceApi {
+}
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/AccountServiceImpl.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/AccountServiceImpl.java
index a65602e..bf5f658 100644
--- a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/AccountServiceImpl.java
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/AccountServiceImpl.java
@@ -1,5 +1,9 @@
 package cz.muni.pa165.banking.application.service;
 
+import cz.muni.pa165.banking.account.management.AccountApi;
+import cz.muni.pa165.banking.account.management.dto.AccountDto;
+import cz.muni.pa165.banking.account.query.CustomerServiceApi;
+import cz.muni.pa165.banking.account.query.SystemServiceApi;
 import cz.muni.pa165.banking.domain.account.Account;
 import cz.muni.pa165.banking.domain.remote.AccountService;
 import cz.muni.pa165.banking.domain.transaction.TransactionType;
@@ -7,31 +11,54 @@ import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
 import java.util.Currency;
+import java.util.Objects;
 import java.util.UUID;
 
 @Service
 public class AccountServiceImpl implements AccountService {
     
-    // TODO add proxies to other services of teammates and calls with validations
-    //  -> Milestone2
+    private final AccountApi accountApi;
     
+    private final CustomerServiceApi publicBalanceApi;
+    
+    private final SystemServiceApi privateBalanceApi;
+
+    public AccountServiceImpl(AccountApi accountApi, CustomerServiceApi publicBalanceApi, SystemServiceApi privateBalanceApi) {
+        this.accountApi = accountApi;
+        this.publicBalanceApi = publicBalanceApi;
+        this.privateBalanceApi = privateBalanceApi;
+    }
+
     @Override
     public Currency getAccountCurrency(Account account) {
-        return Currency.getInstance("EUR");
+        AccountDto accountDto = accountApi.findByAccountNumber(account.getAccountNumber()).getBody();
+        Objects.requireNonNull(accountDto);
+        return Currency.getInstance(accountDto.getCurrency());
     }
 
     @Override
     public boolean isValid(Account account) {
-        return true;
+        AccountDto accountDto = accountApi.findByAccountNumber(account.getAccountNumber()).getBody();
+        return accountDto != null;
     }
 
     @Override
     public boolean accountHasSufficientFunds(Account account, BigDecimal amount) {
-        return true;
+        BigDecimal currentBalance = publicBalanceApi.getBalance(account.getAccountNumber()).getBody();
+        return Objects.requireNonNull(currentBalance).compareTo(amount) >= 0;
     }
 
     @Override
-    public void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account, String information) {
-
+    public void publishAccountChange(UUID processUuid, TransactionType transactionType, BigDecimal amount, Account account) {
+        privateBalanceApi.addTransactionToBalance(
+                account.getAccountNumber(),
+                amount,
+                processUuid,
+                convertType(transactionType)
+        );
+    }
+    
+    private cz.muni.pa165.banking.account.query.dto.TransactionType convertType(TransactionType type) {
+        return cz.muni.pa165.banking.account.query.dto.TransactionType.valueOf(type.name());
     }
 }
-- 
GitLab