From 82a020d40943b283e9f463611a2841df47c3e101 Mon Sep 17 00:00:00 2001 From: Raja Nabeel Date: Fri, 17 May 2024 00:36:53 +0500 Subject: [PATCH] Commit customer onboarding product based account opening add account currency commit --- src/main/java/com/mfsys/uco/UCOURI.java | 2 + .../java/com/mfsys/uco/constants/UCOURI.java | 1 + .../uco/controller/TransactionController.java | 11 +++ .../mfsys/uco/controller/UserController.java | 8 +- .../mfsys/uco/dto/AddAccountRequestModel.java | 18 ++++ .../uco/dto/EvaluatedCurrencyReponse.java | 17 ++++ .../uco/dto/webclientdto/AccountDetail.java | 1 + .../com/mfsys/uco/model/CustomerProfile.java | 3 + .../mfsys/uco/service/TransactionService.java | 35 +++++++ .../mfsys/uco/service/UcoAccountService.java | 92 ++++++++++++++++--- .../uco/service/WebClientDepositService.java | 8 +- 11 files changed, 183 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/mfsys/uco/dto/AddAccountRequestModel.java create mode 100644 src/main/java/com/mfsys/uco/dto/EvaluatedCurrencyReponse.java diff --git a/src/main/java/com/mfsys/uco/UCOURI.java b/src/main/java/com/mfsys/uco/UCOURI.java index 1c04e22..0033c9b 100644 --- a/src/main/java/com/mfsys/uco/UCOURI.java +++ b/src/main/java/com/mfsys/uco/UCOURI.java @@ -3,6 +3,7 @@ package com.mfsys.uco; public interface UCOURI { String VIEW_BALANCE = "/user/viewBalance"; String ONBOARD_CUSTOMER = "/auth/user/authenticate/onboardCutomer"; + String ADD_UCO_CUSTOMER_ACCOUNT = "/createUcoAccount"; String FETCH_DEPOSITACCOUNTS = "/depositAccounts"; String FETCH_ACCOUNT_STATEMENT = "/accountStatement"; String FETCH_ACCOUNT_INQUIRY = "/accountInquiry"; @@ -16,6 +17,7 @@ public interface UCOURI { String GET_DR_TRANSACTION_PIN = "/sendDrTranOtpAndVerifyTranPin"; String RESEND_GET_DR_TRANSACTION_PIN = "/resendDrTranOtp"; String SUBMIT_DR_TRANSACTION = "/submitDrTransaction"; + String CURRENCY_EVALUATION = "/getEvaluatedCurrency"; String SUBMIT_CR_TRANSACTION = "/submitCrTransaction"; String PENDING_CR_TRANSACTION = "/fetchPendingCredittransaction"; String ACCOUNT_STATEMENT = "/fetchDepositAccountStatement"; diff --git a/src/main/java/com/mfsys/uco/constants/UCOURI.java b/src/main/java/com/mfsys/uco/constants/UCOURI.java index 7d0124c..a2551c2 100644 --- a/src/main/java/com/mfsys/uco/constants/UCOURI.java +++ b/src/main/java/com/mfsys/uco/constants/UCOURI.java @@ -5,5 +5,6 @@ public interface UCOURI { String CUSTOMER_ONBOARDING = "/crm/onboarding/digital/customer"; String GET_CMP_UCOACCOUNTS = "/deposit/getUcoAccounts"; String FETCH_EXCHANGE_RATE = "/deposit/uco/fetchExchangeRate"; + String UCO_CUSTOMER_ACCOUNT = "/deposit/onboarding/digital/UcoCustomerAccount"; } diff --git a/src/main/java/com/mfsys/uco/controller/TransactionController.java b/src/main/java/com/mfsys/uco/controller/TransactionController.java index cbcdfc8..b48d981 100644 --- a/src/main/java/com/mfsys/uco/controller/TransactionController.java +++ b/src/main/java/com/mfsys/uco/controller/TransactionController.java @@ -3,6 +3,7 @@ package com.mfsys.uco.controller; import com.mfsys.uco.UCOURI; import com.mfsys.uco.dto.CashInTransactionRequest; import com.mfsys.uco.dto.CashOutTransactionRequest; +import com.mfsys.uco.dto.EvaluatedCurrencyReponse; import com.mfsys.uco.dto.Transaction.TransactionOtpRequestModel; import com.mfsys.uco.dto.TransactionPinResponseModel; import com.mfsys.uco.model.TransactionTrail; @@ -52,5 +53,15 @@ public class TransactionController { @RequestParam String mbmBkmsnumber) { return transactionService.fetchDepositAccountStatement(porOrgacode, mbmBkmsnumber); } + + + @GetMapping(UCOURI.CURRENCY_EVALUATION) + public EvaluatedCurrencyReponse getEvaluatedCurrency( + @RequestParam String porOrgacode, + @RequestParam String baseCurrency, + @RequestParam String targetCurrency, + @RequestParam double sgtGntramtfc) { + return transactionService.getEvaluatedCurrency(porOrgacode, baseCurrency,targetCurrency,sgtGntramtfc); + } } diff --git a/src/main/java/com/mfsys/uco/controller/UserController.java b/src/main/java/com/mfsys/uco/controller/UserController.java index 4c1de0a..d43040c 100644 --- a/src/main/java/com/mfsys/uco/controller/UserController.java +++ b/src/main/java/com/mfsys/uco/controller/UserController.java @@ -56,7 +56,7 @@ public class UserController { @RequestParam String porOrgacode, @RequestParam String cmpCustcode, @RequestParam String pctCstycode) { - return List.of(ucoAccountService.fetchdepositAccountFromCiihive(porOrgacode, cmpCustcode)); + return ucoAccountService.fetchdepositAccountFromCiihive(porOrgacode, cmpCustcode); } @GetMapping(UCOURI.FETCH_ACCOUNT_STATEMENT) @@ -172,4 +172,10 @@ public class UserController { notificationService.sendOtp(otpRequest); return ResponseEntity.ok("OTP sent"); } + + @PostMapping(UCOURI.ADD_UCO_CUSTOMER_ACCOUNT) + public ResponseEntity addUcoCustomerAccount(@RequestBody AddAccountRequestModel addAccountRequestModel) { + ucoAccountService.addUcoAccount(addAccountRequestModel); + return ResponseEntity.ok(HttpStatus.OK); + } } diff --git a/src/main/java/com/mfsys/uco/dto/AddAccountRequestModel.java b/src/main/java/com/mfsys/uco/dto/AddAccountRequestModel.java new file mode 100644 index 0000000..930c25c --- /dev/null +++ b/src/main/java/com/mfsys/uco/dto/AddAccountRequestModel.java @@ -0,0 +1,18 @@ +package com.mfsys.uco.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class AddAccountRequestModel { + private String porOrgacode; + private String email; + private String title; + private String dmpProdcode; + private String channelCode; +} diff --git a/src/main/java/com/mfsys/uco/dto/EvaluatedCurrencyReponse.java b/src/main/java/com/mfsys/uco/dto/EvaluatedCurrencyReponse.java new file mode 100644 index 0000000..078a828 --- /dev/null +++ b/src/main/java/com/mfsys/uco/dto/EvaluatedCurrencyReponse.java @@ -0,0 +1,17 @@ +package com.mfsys.uco.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class EvaluatedCurrencyReponse { + private String pcrCurrcode; + private double sgtGntramtfc; +} diff --git a/src/main/java/com/mfsys/uco/dto/webclientdto/AccountDetail.java b/src/main/java/com/mfsys/uco/dto/webclientdto/AccountDetail.java index d047909..42ab9f6 100644 --- a/src/main/java/com/mfsys/uco/dto/webclientdto/AccountDetail.java +++ b/src/main/java/com/mfsys/uco/dto/webclientdto/AccountDetail.java @@ -20,6 +20,7 @@ public class AccountDetail { protected String dmpProddesc; protected String plcLocadesc; protected String pcrCurrcode; + protected String pcrCurrshort; protected String pcrCurrdesc; protected String cmpCustcode; protected boolean mbmBkmsclosed; diff --git a/src/main/java/com/mfsys/uco/model/CustomerProfile.java b/src/main/java/com/mfsys/uco/model/CustomerProfile.java index 6485d16..a5fb145 100644 --- a/src/main/java/com/mfsys/uco/model/CustomerProfile.java +++ b/src/main/java/com/mfsys/uco/model/CustomerProfile.java @@ -51,4 +51,7 @@ public class CustomerProfile { private String cmpName; @Column(name = "CMP_USERNAME", nullable = true, columnDefinition = FieldNameLength.CODE_50) private String cmpUserName; + @Column(name = "CMP_ADDRESS", nullable = true, columnDefinition = FieldNameLength.DESCRIPTION_LONG) + private String cmpAddress; + } diff --git a/src/main/java/com/mfsys/uco/service/TransactionService.java b/src/main/java/com/mfsys/uco/service/TransactionService.java index d1c8d92..296b8b6 100644 --- a/src/main/java/com/mfsys/uco/service/TransactionService.java +++ b/src/main/java/com/mfsys/uco/service/TransactionService.java @@ -27,6 +27,7 @@ public class TransactionService { private final NotificationService notificationService; private final TransactionPinService transactionPinService; private final TransactionTrailRepository transactionTrailRepository; + private final UcoAccountService ucoAccountService; private final WebClientDepositService webClientDepositService; public TransactionPinResponseModel sendOtpAndValidateTranPin(TransactionOtpRequestModel transactionOtpRequestModel, boolean isResendOtp) { @@ -136,4 +137,38 @@ public class TransactionService { } throw new RuntimeException("may day"); } + + public EvaluatedCurrencyReponse getEvaluatedCurrency(String porOrgacode, String baseCurrencyCode, String targetCurrencyCode, double sgtGntramtfc) { + List exchangeRateList = (List) ucoAccountService.fetchExchangeRate(porOrgacode); + Map exchangeRates = new HashMap<>(); + for (Map rateEntry : exchangeRateList) { + String currencyCode = (String) rateEntry.get("pcrCurrcode"); + Double rate = (Double) rateEntry.get("perEratrateact"); + exchangeRates.put(currencyCode, rate); + } + + double convertedAmount = convertCurrency(baseCurrencyCode, targetCurrencyCode, sgtGntramtfc, exchangeRates); + + return EvaluatedCurrencyReponse.builder().pcrCurrcode(targetCurrencyCode).sgtGntramtfc(convertedAmount).build(); + } + + public double convertCurrency(String baseCurrencyCode, String targetCurrencyCode, double amount, Map exchangeRates) { + if (baseCurrencyCode.equals("default")) { + return exchangeRates.get(targetCurrencyCode) * amount; + } else if (targetCurrencyCode.equals("default")) { + return exchangeRates.get(baseCurrencyCode) * amount; + } + if (baseCurrencyCode.equals(targetCurrencyCode)) { + return amount; + } + if (!exchangeRates.containsKey(baseCurrencyCode) || !exchangeRates.containsKey(targetCurrencyCode)) { + throw new IllegalArgumentException("Unsupported currency code"); + } + + double amountInPKR = amount * exchangeRates.get(baseCurrencyCode); + double amountInTargetCurrency = amountInPKR / exchangeRates.get(targetCurrencyCode); + + return amountInTargetCurrency; + } + } diff --git a/src/main/java/com/mfsys/uco/service/UcoAccountService.java b/src/main/java/com/mfsys/uco/service/UcoAccountService.java index 5fa5190..3a6ec0f 100644 --- a/src/main/java/com/mfsys/uco/service/UcoAccountService.java +++ b/src/main/java/com/mfsys/uco/service/UcoAccountService.java @@ -1,9 +1,11 @@ package com.mfsys.uco.service; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.mfsys.uco.constants.UCOConstants; import com.mfsys.uco.constants.UCOURI; import com.mfsys.uco.dto.AccountInquiryResponse; +import com.mfsys.uco.dto.AddAccountRequestModel; import com.mfsys.uco.dto.SignupStep3RequestModel; import com.mfsys.uco.dto.webclientdto.AccountDetail; import com.mfsys.uco.exception.AccountDoesntExistsException; @@ -16,15 +18,11 @@ import com.mfsys.uco.repository.UCOAccountRepository; import org.springframework.stereotype.Service; import java.time.LocalDate; -import java.util.List; -import java.util.Map; -import java.util.Objects; - - +import java.util.*; @Service public class UcoAccountService { - + ObjectMapper objectMapper; private final CustomerProfileRepository customerProfileRepository; private final UCOAccountRepository ucoAccountRepository; private final CustomerProfileService customerProfileService; @@ -32,12 +30,14 @@ public class UcoAccountService { private final WebClientDepositService webClientDeposit; private final WebClientCrmService webClientCrmService; - public UcoAccountService(CustomerProfileRepository customerProfileRepository, UCOAccountRepository ucoAccountRepository, CustomerProfileService customerProfileService, WebClientDepositService webClientDeposit, WebClientCrmService webClientCrmService) { + public UcoAccountService(CustomerProfileRepository customerProfileRepository, UCOAccountRepository ucoAccountRepository, CustomerProfileService customerProfileService, WebClientDepositService webClientDeposit, WebClientCrmService webClientCrmService + ,ObjectMapper objectMapper) { this.customerProfileRepository = customerProfileRepository; this.ucoAccountRepository = ucoAccountRepository; this.customerProfileService = customerProfileService; this.webClientDeposit = webClientDeposit; this.webClientCrmService = webClientCrmService; + this.objectMapper = objectMapper; } public AccountInquiryResponse fetchAccountTitile(String porOrgacode, String acntTypeCode, String acntTypeValue) { @@ -70,7 +70,6 @@ public class UcoAccountService { if (Objects.nonNull(customerProfileService.fetchCustcodeBasedOnEmail(signupStep3RequestModel.getPorOrgacode(), signupStep3RequestModel.getEmail()))) { throw new UserAlreadyRegisteredException(null); } - String porOrgacode = signupStep3RequestModel.getPorOrgacode(); Map cmpCustcodeReturn = (Map) webClientCrmService.onboardCustomer(Map.of("CMP_FULLNAME", signupStep3RequestModel.getName() @@ -80,7 +79,7 @@ public class UcoAccountService { String cmpCustcode = String.valueOf(cmpCustcodeReturn.get("cmpCustcode")); System.out.println(cmpCustcode); - AccountDetail accountDetail = fetchdepositAccountFromCiihive(porOrgacode, cmpCustcode); + AccountDetail accountDetail = fetchdepositAccountFromCiihive(porOrgacode, cmpCustcode).get(0); CustomerProfile customerProfile = CustomerProfile.builder().cmpCustcode(accountDetail.getCmpCustcode()).cmpEmail(signupStep3RequestModel.getEmail()) .cmpName(signupStep3RequestModel.getName()).cmpIsKycVerified(signupStep3RequestModel.isKycAdded()) .pitIdencode(signupStep3RequestModel.getIdentificationType()).pitIdenvalue(signupStep3RequestModel.getIdentificationNumber()) @@ -104,12 +103,13 @@ public class UcoAccountService { } - public AccountDetail fetchdepositAccountFromCiihive(String porOrgacode, String cmpCustcode) { + public List fetchdepositAccountFromCiihive(String porOrgacode, String cmpCustcode) { String url = UCOURI.GET_CMP_UCOACCOUNTS + "?porOrgacode=" + porOrgacode + "&cmpCustcode=" + cmpCustcode; List map = (List) webClientDeposit.getCmpUcoAccounts(url, porOrgacode); ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.convertValue(map.get(0), AccountDetail.class); + return objectMapper.convertValue(map, objectMapper.getTypeFactory().constructCollectionType(List.class, AccountDetail.class)); + } @@ -117,5 +117,75 @@ public class UcoAccountService { String url = UCOURI.FETCH_EXCHANGE_RATE + "?porOrgacode=" + porOrgacode; return webClientDeposit.fetchExchangeRate(url, porOrgacode); } + + public void addUcoAccount(AddAccountRequestModel addAccountRequestModel){ + String porOrgacode = addAccountRequestModel.getPorOrgacode(); + CustomerProfile customerProfile = customerProfileRepository.findCustomerProfileByCmpEmailAndPorOrgacode(porOrgacode,addAccountRequestModel.getEmail()); + String accountNumber = webClientDeposit.createUcoAccount(JsonToString(Map.of("payload",preparePayloadForAccount(customerProfile,addAccountRequestModel.getTitle()), + "uniqueConstraints",List.of(List.of(String.valueOf(addAccountRequestModel.getDmpProdcode()))))),UCOURI.UCO_CUSTOMER_ACCOUNT,porOrgacode); + saveCustomerAccountDetails(porOrgacode,customerProfile.getCmpCustcode(),accountNumber); + } + +public void saveCustomerAccountDetails(String porOrgacode, String cmpCustcode,String accountNumber){ + fetchdepositAccountFromCiihive(porOrgacode, cmpCustcode).stream().forEach(k -> { + if(k.getMbmBkmsnumber().equals(accountNumber)){ + UcoAccount ucoAccount = UcoAccount.builder() + .id(new AccountId(k.getPorOrgacode(), k.getMbmBkmsnumber())) // Set the AccountId, assuming a method exists to create or retrieve it + .dmpProdcode(k.getDmpProdcode()) + .mbmBkmstitle(k.getMbmBkmstitle()) + .pcrCurrdesc(k.getPcrCurrdesc()) + .cmpCustcode(k.getCmpCustcode()) + .pcrCurrcode(k.getPcrCurrcode()) + .mbmBkmsclosed(k.isMbmBkmsclosed()) + .mbmBkmsopendate(LocalDate.now()) + .sgtLasttrandate(LocalDate.now()) + .build(); + ucoAccountRepository.save(ucoAccount); + } + }); +} + public String preparePayloadForAccount(CustomerProfile customerProfile,String title){ + Map jsonMap = new HashMap<>(); + jsonMap.put("SUS_USERCODE", "01"); + jsonMap.put("CMP_CUSTCODE",customerProfile.getCmpCustcode()); + jsonMap.put("CMP_FULLNAME", title); + jsonMap.put("PLC_LOCACODE", "2003"); + jsonMap.put("workFlowStage", "BN_WF_CP_AUTHORIZATION"); + jsonMap.put("PCT_CSTYCODE", "I"); + jsonMap.put("POR_ORGACODE", customerProfile.getPorOrgacode()); + List> workFlowLog = new ArrayList<>(); + Map logEntry = new HashMap<>(); + logEntry.put("susUsercode", "01"); + logEntry.put("formId", "BN_WF_CP_AUTHORIZATION"); + workFlowLog.add(logEntry); + jsonMap.put("workFlowLog", workFlowLog); + + List> bnCsItIdentifier = new ArrayList<>(); + Map identifierEntry = new HashMap<>(); + identifierEntry.put("CIT_IDENVALUE", customerProfile.getPitIdenvalue()); + identifierEntry.put("PIT_PRIMARYIDENCODE", true); + identifierEntry.put("PIT_IDENCODE", customerProfile.getPitIdencode()); + bnCsItIdentifier.add(identifierEntry); + jsonMap.put("BN_CS_IT_IDENTIFIER", bnCsItIdentifier); + jsonMap.put("@_CREATEUSER", "01"); + + List> bnCsAdAddress = new ArrayList<>(); + Map addressEntry = new HashMap<>(); + addressEntry.put("PAD_ADRSCORRESPONDENCE", true); + addressEntry.put("PAD_ADRSMOBPHONE", customerProfile.getPadAdrsmobphone()); + bnCsAdAddress.add(addressEntry); + jsonMap.put("BN_CS_AD_ADDRESS", customerProfile.getCmpAddress()); + Map createdDateMap = new HashMap<>(); + createdDateMap.put("$date", new Date()); + jsonMap.put("@_CREATEDATE", createdDateMap); + return JsonToString(jsonMap); + } + public String JsonToString(Object jsonMap){ + try { + return this.objectMapper.writeValueAsString(jsonMap); + } catch (JsonProcessingException var3) { + throw new RuntimeException(var3); + } + } } diff --git a/src/main/java/com/mfsys/uco/service/WebClientDepositService.java b/src/main/java/com/mfsys/uco/service/WebClientDepositService.java index f35e6cc..c38cbd9 100644 --- a/src/main/java/com/mfsys/uco/service/WebClientDepositService.java +++ b/src/main/java/com/mfsys/uco/service/WebClientDepositService.java @@ -46,7 +46,13 @@ public class WebClientDepositService { porOrgaCode); } - + public String createUcoAccount(Object payload, String url, String porOrgaCode) { + return handleResponse(webClientDeposit.post().uri(url).bodyValue(payload).accept(MediaType.APPLICATION_JSON) + .header("SUS_USERCODE", porOrgaCode) + .header("POR_ORGACODE", porOrgaCode).retrieve() + .toEntity(String.class), + porOrgaCode); + } public Object fetchExchangeRate(String url, String porOrgacode) { return handleResponse(webClientDeposit.get().uri(url).accept(MediaType.APPLICATION_JSON) .header("SUS_USERCODE", porOrgacode)