@startuml

autonumber

skinparam LegendBackgroundColor #white

title PB-520 Phase 2 UC17 - View Investment Summary

actor "Customer" as UI

box "Next Platform"
  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 provide a summary of all investments owned by the customer.
  This information will be used to populate the Investment Overview screen IN_A2_01.A, EF page 21
  Categories and products that don't have any investments won't be presented on this screen
end legend

UI -> Next:**HTTP** \n GET /v1/investments/investmentSummary
activate Next

note right of UI
App will make a call to this new endpoint
to retrieve a summary of all investments
made by the customer.
end note

Next -> DB: **SQL** select invest.FUND_ACCOUNT_ID, categ.PRODUCT_CATEGORY_TYPE, categ.DESCRIPTION,  \n prodTypeDef.EXTERNAL_CORE_PRODUCT_ID, prodGroup.CODE, prodGroup.DESCRIPTION \n prod.PRODUCT_NAME, prod.INCOME_VALUE, prod.INCOME_DESCRIPTION, prod.WHEN_IT_YIELDS \n  INVESTMENT invest \n inner join  PRODUCT prod on prod.PRODUCT_UID = invest.PRODUCT_UID \n inner join PRODUCT_CATEGORY categ on categ.PRODUCT_CATEGORY_UID = prod.PRODUCT_CATEGORY_UID  \n inner join PRODUCT_TYPE_DEFINITION\n prodTypeDef on prodTypeDef.PRODUCT_UID = prod.PRODUCT_UID \n inner join PRODUCT_TYPE prodType ON prodType.PRODUCT_TYPE_UID = prodTypeDef.PRODUCT_TYPE_UID \n inner join PRODUCT_GROUP prodGroup on prodGroup.PRODUCT_GROUP_UID = prod.PRODUCT__GROUP_UID \n where prod.ENABLE_IND = true and prodType.PRODUCT_TYPE = 2 and invest.CUSTOMER_UID = customerId
activate DB

note right of Next
Retrieves all products in which the customer has already invested and their category. The category will be used to  present the
percentual invested on each category.
end note

note right of Next #coral
**Clarification**
Is it possible that an investment product with balance
grater than zero is deactivated? What should happen?

**Answer**
So far it's not possible. but we should
retrieve the investments regardless the product
is able or not add "enable_ind" on response
because in the future this might be possible.
end note

note right of Next #orange
  **Note to developers:**
  "WHEN_IT_YIELDS" is a new field (Varchar2(50))
  that should be added on product table
end note
DB --> Next: List of investments created
deactivate DB

loop for each investment retrieved from Next database

note right of Next
For now there will be only one product: CDB. In the future there will be more products, and for
ech one of them it will be necessary to check balance and yield information
end note

opt if there's an investment created for CDB product

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

  note right of Next
  Retrieve CDB Balance
  **detalhar um pouco melhor**
  end note

  note right of Next #orange
  **Note to developers:**
  (January 23, 2019)
  "saldoLivre" will be presented as TotalBalance for the product "saldoDisponivelResgate" should also be sent to the app
  to inform balance not considering deposits made on the current day. This will be used by the app on other screen during
  investment flow
  Example:
    saldoDisponivelResgate = 178.99
    saldoLivre = 178.99;
  end note

  Bradesco --> Next: Response
  deactivate Bradesco

  note right of Next #coral
  **Clarification**
  Are we going to keep the deposited and yielded amounts?
  Currently it's only possible to retrieve
  this information for the last three months.

  **Answer**
  We will present the last three month information.
  end note

  Next -> Bradesco: **HTTP** \n CAPN GET /investimentos/cdb/extrato/
  activate  Bradesco

  note right of Bradesco
  **Note:**
  Retrieve CDB statement.
  This endpoint only returns information of the last 3 months.

  (find below only the fields that will be used by coreNext within Json structure)

  {
      "listaExtrato": [
          {
            "totalAplicado": 10672.99,
            "valorRendimentoBruto": 54089.06,
          }
      ]
  }

  end note
  Bradesco --> Next:Response
  deactivate Bradesco

end opt
end loop

group Savings account



    Next -> Bradesco: **HTTP** \n ENTR GET /consultas/contaPoupanca/saldos
    activate Bradesco

    note  right of Bradesco #lightblue
    **Note:**
    Retrieve savings account Balance from ENTR api

    (find below only the fields that will be used by coreNext within Json structure)

    {
      SaldosDiversos:[
                {
                 "identificacaoProduto": 983,
                  "saldo" = 543,75;
              }
            ]
    }

    (January 23, 2019)
      identificacaoProduto must be 983. This
      identifies the product as "savings account".
      If this product was not returned, it means that
      the account has no balance at the moment (baçance is zero).
    end note

    Bradesco--> Next: Response
    deactivate  Bradesco



    Next -> Bradesco: **HTTP** \n ENTR GET /consultas/contaPoupanca/extratos/demonstrativos
    activate  Bradesco

    note right of Bradesco
    **Note:**
What about "total deposit" (totalAplicado)? Do we have this for savings account?

    {
      "totalRendimentos": 0
    }

    end note

    Bradesco --> Next:Response


    group (To be Confirmed)

      Next -> Bradesco: ENTR GET /consultas/contaCorrente/extratos/periodo


          note right of Bradesco #red
        (January 23, 2019)
        Retrieve Total deposit to savings account
          **Question**
          the endpoint does not provide any information about
          savings account total deposit. Should we use another endpoint
          to retrieve all deposits made in a giver period of time and
          just summ all deposit amounts?
          (GET /consultas/contaCorrente/extratos/periodo)
          Should we also use the new endpoint to retrieve yield amount
          information?

          Leandro to confirm that.

          end note
      Bradesco --> Next:Response
    end group


    deactivate Bradesco
    end group

  opt if savings account balance = Zero and "totalRendimentos" = zero

       Next -> DB: Check for any deposits for savings account on CBAT
    activate DB
    DB --> Next: Count savings account deposit
    deactivate DB

    note right  of Bradesco #orange
    **Note to developers:**
    If there is a object returned for savings account
    but  it's amounts are zero, coreNext should check if
    there is any deposit for savings account
    on CBAT table, since there will be no records for savings account
    on investment table and it's values provided by
    coreBanking are not enought to guarantee that the custmer
    has already invested in savings account.

    If there's a deposit for savings account on CBAT
    then it should be added in the product list on the
    response, even if all amounts are zero.

       end note

    note right of DB #lightblue
      (January 23, 2019)
    **Question:** to Leandro:
    Can we assume that there is an existing investment
    for saving account if there was any investment for
    savings account even before PB-520?

    answer: Yes. We can assume this.

    end note
    end opt


note right of Next #orange
**Note to Developers:**
If any of coreBanking calls fail,
the product should be added in the
response, but in this case, all amounts
will be zero, lists and Strings will
be empty and the flag "IsProductAvailable"
should be "No".
end note




opt if any products has balance

  Next -> Next: Sum all products balance (totalInvestmentBalance), deposit (totalDeposit) and yield (totalYield) amounts retrieved on steps above to get the Total Balance of all investments

  Next -> Next: get the percentual invested per category
end opt

Next -> Next: Check if there are any products in which the customer has not invested yet

note right of Next #lightblue
**Note:**
(January 25, 2018)
  CoreNext should check if there are any
  investment products (including savings account)
  in which the customer has not invested yet.
  If any investment product is found, hasOtherProductsToInvest = true;
  If all investment products already have an investment
  tied to it, hasOtherProductsToInvest = False;

  This will be used by the app to enable/disable the button
  to make a new investment
end note


note left of Next #pink
**Assumption:**
if there are no investments
for the customer,return the amounts as zero,
strings and lists will be empty.
Question to Leandro: Should we add
a message on the "system"
list to identify that the customer
 has no investments?

 **Answer**: We can proceed that way, tough
 the message is not necessary.
end note

Next --> UI:Response
deactivate Next

note right of UI
**Note:**
{

  "system": {
    "code": "string",
    "messages": [
      "string"
    ]
  }


  "investmentSummary":  {
      "totalInvestmentBalance": "Double",
      "totalDeposit": "Double",
      "totalYield": "Double",
      "hasMoreProductsToInvest": "Boolean"
      "investedCategories": [
        {
          "productCcategoryType": "Integer", - PRODUCT_CATEGORY_TYPE(DB)
          "productCategoryDescription": "String",
          "investedPercentage": "Double"
        }
      ]

      "investedProducts": [
        {
          "productId": "Integer", (EXTERNAL_CORE_PRODUCT_ID)
          "productName": "String",
          "enable_ind": Boolean,
          "productCategoryDescription": "String",
          "productGroupDescription": "String",
          "balance": "Double",
          "balanceAvailableForWithdraw": "Double" (empty for savings account)
          "incomeValue": "String",
          "incomeDescription": "String",
          "whenItYields": "String",
          "totalYield": "Double",
          "IsProductAvailable": "Boolean",
          "productGroup": {
            "productGroupCode": "Integer", (new field on Next database)
            "productGroupDescription": "String" (new field on Next database)
          }
        }
      ]

    }
  }

end note

@enduml
