@startuml

autonumber

skinparam LegendBackgroundColor #white
title PB-520 Phase 2 UC18 - Get Product Investment Detail

actor "customer" as UI

box "Next Plataform"
  participant "Core Next" as Next
  database "Next DB" as DB
end box

participant "Core Banking" as Bradesco

legend top
-- \t **Abstract** \t --
This is a new endpoint that will retrieve details of investments made to a specific fund product.
This information will be used to populate the Investment Overview screen IN_A2_BA2_01.B, EF page 29.

The endpoint will:
•	Return all scheduled investments for this Product
•	Return recurring investment details (if there is one)
•	Return Product Investment information including total invested, total yield, total available for withdrawal, etc.

This endpoint will **not** be used to retrieve savings account information. For this, the app will redirect the customer to an already existing savings account flow.
end legend

UI -> Next: **HTTP** \n GET /v1/investments/investmentDetails{fundId}
activate Next

note right of UI
  **Note:**
  App layer will call this new endpoint to
  retrieve the details about an specific
  investment product.

  Request parameter:
  fundId = EXTERNAL_CORE_PRODUCT_ID
end note

Next -> DB: **SQL** \n select  prod.PRODUCT_NAME, prod.WHEN_IT_YIELDS, prod.INCOME_VALUE, prod.INCOME_DESCRIPTION, prod.ADMINISTRATIVE_FEE \n recurInvest.DEPOSIT_DAY, recurInvest.AMOUNT, recurInvest.ENABLE_IND\n from PRODUCT prod \n inner join INVESTMENT invest on invest.PRODUCT_UID = prod.PRODUCT_UID \n left join RECURRING_INVESTMENT recurInvest on recurInvest.INVESTMENT_UID = invest.INVESTMENT_UID  \n inner join PRODUCT_TYPE_DEFINITION prodTypeDef on prodTypeDef.PRODUCT_UID = prod.PRODUCT_UID, prod.ALTERNATE_CORE_ID \n inner join PRODUCT_TYPE prodType on prodType.PRODUCT_TYPE_UID = prodTypeDef.PRODUCT_TYPE_UID \n where prod.PRODUCT_UID = productID and invest.customer_uid = customerId\n and prodType.PRODUCT_TYPE= 2 \n order by recurInvest.CREATE_DT desc \n limit 1

activate DB

  DB --> Next: Product details

deactivate DB

  alt if SQL query return is not empty


    Next -> DB: **SQL** \n Select cbat.TRANSACTION_DT, cbat.AMOUNT \n from CUSTOMER_BANK_ACCOUNT_TRANS cbat \n inner join INVESTMENT invest on  invest.INVESTMENT_UID = cbat.INVESTMENT_UID \n where invest.PRODUCT_UID = productID and invest.customer_uid = customerId and cbat.TRANSACTION_TYPE_ID in (6,7) \n and cbat.TRANSACTION_DT > current_date \n order by cbat.TRANSACTION_DT asc

    note left of DB #orange
      **Note for developers**
      for TRANSACTION_TYPE_ID it was considered:
       6 - Investment Deposit
       7 - Investment Withdrawal

       codes may change between environments.
       Please double check these.
    end note

    activate DB

    DB--> Next: List of scheduled investments

    deactivate DB



      alt If product sent is CDB


        Next -> Bradesco: **HTTP** \n  YMIV GET /investimentos/produtos/saldo
        activate Bradesco


          note left of Bradesco
            Retrieve CDB Balance

            saldoDisponivelResgate = 178.99
            saldoLivre = 178.99;

          "saldoDisponivelResgate" should also be sent to the app
            to inform balance not considering deposits made on the current day.
            App will decide  which field will be displayed as "balance".
          end note

          Bradesco --> Next: Response

        deactivate Bradesco

        Next -> Bradesco: CAPN GET /investimentos/cdb/extrato/

        activate Bradesco

          note left of Bradesco
          **Technical Note**
          Retrieve investment information

          {

           private Double totalAplicado = null;
           private Double valorImpostoDeRenda = null;
           private Double valorIof = null;
           private Double valorPrincipal = null;
           private Double valorRendimentoBruto = null;
          }
          end note


          note left of Bradesco #coral
            **Clarification:**
            This endpoint only provides information for the last three months.
            There's a chance that in the app screen, all yield information
            summed won't match the current balance (An investment older than
              3 months with no withdrawal will have abalace greater than
              last 3-months deposits and yielded amounts summed up).

              **Answer:**
              Leandro is aware and this was already
              explained to Bradesco's business team. We will probably
              add a disclaimer at this point, on the app side, to
              let he customer know that the information presented
              considers the last three months. No need to change
              anything on coreNext side here.
          end note

          note right  of Next #orange
            **Note to developers**
              Add new field 'withdrawalPeriod' on PRODUCT table to store  "Prazo de Resgate"
          end note

            note right of Next #coral
              **Question** to Leandro
              Will we keep EndDate(vencimento) field, since each
              CDB deposit has its own end date?

              **Answer** This field will no longer be presented.
            end note

          Bradesco --> Next: Response

        deactivate Bradesco

        note right of Next
          **Assumption**
          If any of coreBanking calls fail,
          we'll return an error to the app layer.
          end note

          Next --> UI: Response


        note right of UI
        {
          "fundFinancialInfo": {
            "incomeTax": 0,
            "iofTax": 0,
            "totalBalance": 0,
            "balanceAvailableForWithdraw" : 0;
            "totalDeposited": 0,
            "totalYielded": 0,

          },
            "fundGroupDetails": {
            "fundGroupCode": 0,
            "fundGroupDescription": "String"
          },
          "productInfo"{
          "fundId": 0,
          "fundName": "string",
          "whenItYields": "string"
          "incomeValue": "string",
          "incomeDescription": "string",
          "withdrawalPeriod": "string",
          "administrativeFee":"string"
          }

          "fundRecurringInvestmentDetails": {
            "recurringInvesmentAmout": 0,
            "recurringInvestmentDay": 0,
            "recurringInvestmentEnabled": true
          },

          "fundScheduledInvestmentDeposits": [
            {
              "scheduledAmount": 0,
              "scheduledDate": "2019-01-21"
            }
          ],
          "system": {
            "code": "string",
            "messages": [
              "string"
            ]
          }
        }
        end note


    else if product is not CDB
      note left of Next #coral
      **Note:**
      So far we are only considering CDB for this endpoint.
      This should be reviewed once a new product is added
      into the investments plataform.
      end note
      Next --> UI: Error 400

    end alt


else If SQL query result on step 2 is empty



  Next --> UI: Error 400

  note right of UI #coral
  **Note:**
  In this case, either the customer has no investments
  to the given product or the product
  is not an investment product
  end note
  deactivate Next
end alt

@enduml
