@startuml
'diagram parameters'
autonumber
'skinparam dpi 300
skinparam LegendBackgroundColor #white

'diagram title'
title UC6 - Contract a installment credit simulated

'a list of the actors and participants'
actor "Credit Customer" as UI

box "Credit Platform"
participant "Core Credit" as CS
database "Credit Database" as DB
end box

box "Core Next Platform" #lime
participant "Core Next" as Next
database "Next Database" as NEXTDB
end box

participant "Core Bradesco" as BR

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
This endpoint is responsible to contract a installment credit simulated.
end legend
note right of UI #aqua
**Assumption:**
During the simulation Core banking will return
a simulation ID that need to be passed in case
of hiring
end note

ref over UI, CS, DB, BR
Please see UC 4 & 5 -  to check how the simulation ID is returned by core banking
end ref

note right of UI #coral
**Clarification:**
Credit hiring is available 24x7?

**Answer**
Yes.
end note

note right of CS #coral
**Clarification:**
Credit Description need to be validated against
Prohibited words list?

**Answer**
Yes. The description must be
validated on Prohibited words list.
end note


note left of CS
**Note:**

Data expected by the endpoint:

"""codPessoaConta"           ==> From UC1   ""
"""flagSeguro"               ==> From UC1   ""
"""flagSeguroElegivel"       ==> From UC5   ""
"""numSimulacao"             ==> From UC5   ""
"""produtoEmprestimo"        ==> Value 1    ""
"""seqNegocioConta"          ==> From UC1   ""
"""tipoConta"                ==> Value 00   ""
"""tipoNegocioConta"         ==> From UC1   ""
"""Descrição crédito"        ==> From App   ""

end note


UI->CS: POST credit/installment/contracts/
activate CS


note right of CS
**Note:**

Credit Microservice will have a replica of PROHIBITED_WORD table
to check if Customer Credit description contains any prohibited
word. The logic to do that is the same used in Core Next to
validate Vaquinha or Goal name.

end note

note right of CS #coral
**Clarification:**

Is it necessary a contigency mechanism in case of any problem
with the replica mechanism that syncronize the table with Core Next?

Needs Credit Microservice to call a new endpoint of Core Next to
check forbidden words as a contingency?

end note

CS->DB: get list of prohibited words
activate DB
DB->DB: SELECT WORD FROM PROHIBITED_WORD
DB-->CS: return list of prohibited words
deactivate DB

alt if access to PROHIBITED_WORD is ok obtaining a list of words

  CS->CS: Check if Credit description constains a prohibited word

else Check with CoreBanking if Credit description contains prohibited word
  CS -> Next: CheckForbiddenword(credit_description)
  activate Next
  Next --> CS: Return
  deactivate Next
end alt

alt If Valid description

CS->BR: POST /creditoParcelado/efetivacao
activate BR
BR-->CS: Response


alt Credit hired




note right of CS #coral
**Clarification:**

Is the new Core Banking API going to return the
expected fields for screen EM_A1_09.A ?
-Credit description
-Credit amount
-Installment Number
-Installment amount
-Payment method
-Credit Value requested
-Credit amount
-Contract date&time
-Payment day

**Answer**
Yes, corebanking endpoint is going to provide this info.

end note

note right of CS #coral
**Clarification:**

Is core banking able to return the necessary data
to generate the PDF receipt (EM_A1_09.B) after
credit hiring?

This data need to be available for another endpoint
responsible to generate the PDF receipt

**Answer**

Credit details is not available 24x7 right after the credit hiring.

To allow the customer to get the PDF contract, the response of POST /creditoParcelado
need to be stored  to allow the PDF generation.



end note

deactivate BR
CS->CS: Generate CREDIT_AUTHENTICATION_CODE
CS->DB: Save Credit details
activate DB
DB-->DB: **SQL**\n INSERT (CUSTOMER_UID, CREDIT_CORE_ID, CREDIT_DESCRIPTION, CREDIT_AMOUNT_REQUIRED, \n IOF_VALUE, INSURANCE_VALUE, CREDIT_AMOUNT, CREDIT_INSTALLMENT_NUMBER, INSTALLMENT_VALUE, \n CREDIT_FIRST_DAY_PAYMENT, CREDIT_LAST_DAY_PAYMENT, INTEREST_RATE_MONTH, \n INTEREST_RATE_YEAR, RATES_VALUE, PAYMENT_MODE, OPERATION_FLOW_TOTAL_AMOUNT, \n OPERATION_FLOW_VALUE_RELEASED, AUTHORIZES_PAYMENTS_IOF, \n AUTHORIZES_PAYMENTS_INSURANCE, AUTHORIZES_PAYMENTS_FEES, \n  AUTHORIZES_PAYMENTS_TOTAL_AMOUNG_AUTHORIZED, \n AUTHORIZES_PAYMENTS_CET_MONTH, \n AUTHORIZES_PAYMENTS_CET_YEAR, CREDIT_AUTHENTICATION_CODE, \n CREATE_DT, CREATE_USER_UID, CREATE_USER_FUNCTION_NAME, UPDATE_DT,\n UPDATE_USER_UID, UPDATE_FUNCTION_NAME) INTO INSTALLMENT_CREDIT
DB-->CS: New row
deactivate DB

note right of CS #coral
**Clarification:**

Is Core Banking able to return the list of
installments credit to be payed right after
the credit hiring?

The installment list is mandatory to support the
notification to the client that a near installment
is going to be charged.

**Answer**

the list of installment credits will be provided in the
response of the POST /creditoParcelado

end note


loop For each installment returned in the list
  CS->DB: Store installment list
  activate DB
  DB-->DB: **SQL**\n INSERT (CREDIT_UID, INSTALLMENT_DATE, INSTALLMENT_VALUE, INSTALLMENT_NOTIFIED=0) INTO CREDIT_INSTALLMENTS
  DB-->CS: Response
  deactivate DB
end loop
CS-->UI: Response
note right of CS
**Note:**

Response will have these fields to support screens EM_A1_09.A and EM_A1_09.B:
-CREDIT_UID
-Credit description
-Credit amount
-Installment Number
-Installment amount
-Payment method
-Credit Value requested
-Credit amount
-Contract date&time
-Payment day
-CREDIT_AUTHENTICATION_CODE

end note

note right of CS #coral
**Clarification:**

Confirm how to return these fields:
- Payment method
- Payment day


Confirmed with DS in a meeting on 2019-01-02:
- Payment method will be a hardcode text "Débito en conta"
- Payment day will be the day number of dtVencPrimeiraParcela

end note

note right of CS #coral
**Clarification:**

Core banking is returning more fields than expected for screens EM_A1_09.A and EM_A1_09.B.

Do we need to return all data?

Yes

end note

else
  CS --> UI: Error message

  note right of CS
  **Note:**

  Return error message returned by core banking.

  end note

end alt

else Forbidden word
  CS-->UI: Error message: Forbidden word used in Description
  deactivate CS

  note right of CS
  **Note:**

  Text message defined by DS is:
  "Palavra proibida encontrada no nome do Crédito"

  end note

end alt

@enduml
