@startuml
'Import Sequence diagram style
autonumber
skinparam LegendBackgroundColor #white
'diagram title'
title PB-778 - UC1: GET onboarding required T&Cs

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
This use case will present a change on an already existing endpoint.

This endpoint identifies what Terms and Confitions should be signed by an applicant in order to progress on the onboarding flow and
create a Next account.

Currently Next provides a single Term and Condition related to service packages,regardless which one was chosen by the applicant.

From now on, Next will have specific Term and Confitions for each service package.

This use case will present the changes that should be made on the **onboarding** step to retrieve all required Terms and Conditions.
end legend

'a list of the actors - type actor|participant|database|queue to get template'
actor "Next Applicant" as UI
box " Core Next"
participant "Core Next" as Next
participant "Next Database" as DB
end box
'participant "CoreBank" as CB

note left of UI #plum
**Endpoint:**
GET /termsAndConditions

**Current implementation:**
Retrieve all Terms and Conditions required to complete the onboarding flow.
Currently Next has a single T&C¹ for all service packages.

**Required implementation:**
Change current implementation on how to retrieve service pack T&C.
The new implementation should identify which service pack was chosen by the applicant²
and retrieve the specific T&C for this service pack.

**No changes on endpoint request and response**

**ENSURE BACKWARDS COMPATIBILITY IS MAINTAINED**
end note

note left of Next #white
-- \t**Glossary:**\t --
¹T&C - Stands for "Terms and Conditions", documents that should be signed by a
person when acquiring a product, working as a proof that this person agreed with product's usage
conditions.

²Applicant - During the onboarding process, a person who is trying to open an account on
Next is called "applicant". It means that this person hasn't finished the onboarding process yet.

When onboarding is completed, and this applicant account is created, this person is considered
a "customer".
end note



UI->Next: GET /termsAndConditions
activate Next
note right of Next
**Note:**
Use existing methodology for all other Terms and Conditions that are not
related to service packages(i.e.: credit products, cards, and others)
end note


note right of Next #orange
**Note to developers:**
T&C for service package are currently retrieved within the "onboarding T&C group type".
This should be changed since there will no longer be a single T&C for this kind of product.

From now on, coreNext should identify what was the selected service package and retrieve the specifc T&C
related to it.
end note
group #CCEEFF **NEW** - Get required Service Package T&C

  Next->DB: Identify selected service package and retrieve related T&C
  activate DB

  note right of DB #coral
  **Clarification:**
  On onboarding, is the proposal created as "active" already, or does starts as
  "pending" like change plan flow?

  **Answer**: On the onboarding flow the proposal register is created as Active.
  end note

  note right of DB
1. Retrieve from proposal products table the service package.
  This proposal product should be related to the current **active proposal** for the given customer.

*Current **active** proposal is identified by "OFFERS_MAINTAINED_UID" = 'Y'.

2. On PROPOSAL_PRODUCT table, look for service package:
    - "Service packages" products are identified by productType=1. To find out
    which service package was selected, check "productCode".
  end note

  note right of DB
3. Retrieve the Term and Condition from "Service_Package" TC_GROUP from TERM_AND_CONDITION tabl that is linked to the
  service package code mapped via "EXTERNAL_CORE_PRODUCT_ID" on PRODUCT_TYPE_DEFINITION table,
  also checking if this product has "service_package" in **PRODUCT_TYPE** table.
  end note

  note right of DB #orange
  **Note to Developers**
  A few scripts will be necessary to populate the following tables:

4. Add on "PRODUCTS" table one register for each service package, and link the
  TERMS_AND_CONDITIONS register to each service package product.

  Insert all service packages on **PRODUCTS table**

|= PRODUCT_NAME |= PRODUCT_CD |= FUND_TC_REQUIRED_IND |
| Cesta Next|NEXT_SERVICE_PACKAGE| N |
| Serviços Essenciais|ESSENCIAL_SERVICE_PACKAGE| N|
| Pacote Padronizado de Serviços I | DEFAULT_SERVICE_PACKAGE_1 | N |
| Pacote Padronizado de Serviços II | DEFAULT_SERVICE_PACKAGE_2 | N |
| Pacote Padronizado de Serviços III | DEFAULT_SERVICE_PACKAGE_3 | N |
| Pacote Padronizado de Serviços IV | DEFAULT_SERVICE_PACKAGE_4 | N |

5. Create a new product type on **PRODUCT_TYPE** table to identify this new scenario:

|= PRODUCT_TYPE |= DESCRIPTION|
| 3 |ServicePackage|
   -> Update comments on this column to add "3 = PROPOSAL"


6. Insert the association between Product and Product_type on **PRODUCT_TYPE_DEFINITION** table:
 -In this table should be linked the product_uid just created, the product_type_uid
 just created and the "EXTERNAL_CORE_PRODUCT_ID" should correspond to the
 productCode of each service package, as follows:

|=Service Package |= EXTERNAL_CORE_PRODUCT_ID|
| Cesta Next| 7044|
| Serviços Essenciais | 4025 |
| Pacote Padronizado de Serviços I| 4056|
| Pacote Padronizado de Serviços II| 5680|
| Pacote Padronizado de Serviços III| 5681|
| Pacote Padronizado de Serviços IV| 5682|
*These code were confirmed with DS team.

7. Insert new terms and conditions on **TERMS_AND_CONDITIONS** table

|=PRODUCT_UID|=TC_GROUP|=TC_\nSIGNATURE|=DOCUMENT_\nTYPE_UID|=SORT_ORDER|=Version|
|product_uid related to \n external_core_id 4025| Service_Package | 2 | 20 | 2 |higher version +1|
|product_uid related to \n external_core_id 4056| Service_Package | 2 | 20 | 2 |higher version +1|
|product_uid related to \n external_core_id 5680| Service_Package | 2 | 20 | 2 |higher version +1|
|product_uid related to \n external_core_id 5681| Service_Package | 2 | 20 | 2 |higher version +1|
|product_uid related to \n external_core_id 5682| Service_Package | 2 | 20 | 2 |higher version +1|
*Despite adding 6 new Terms and Conditions, all of them represent one same thing:
A service package term and condition. In this case, we will maintain the "document_type" as it is,
and also maintain the version control, since it can be considered at a new version of the old
service package Term and Condition, and for this new scenario, we will identify which T&C should
be presented by the "product".

8. Link all products created with the new product_type

9. Update original Term and Condition group to "DEPRECATED"
-This should be done to guarantee thar the old T&C won't be retrieved when looking for all onboarding group T&Cs
  end note

  note right of Next #aqua
  **Assumption:**
  We are assuming that:
  # There will be no changes on those T&Cs signature type when comparing to the old version
  of this term and condition;
  # The same sort order should be maintained;
  # Old document type (code to identify the term and condition) should no longer be used;
  # New document types should be created in order to guarantee that there will be no
  conflicts;
  # These product codes will not change;
  # Service Package terms and Conditions are only "consumed" on onboarding and
   change plan flows. No other componet/journey handles these terms and conditions;
  end note

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

  # Can we use the product code returned by coreBank as "EXTERNAL_CORE_PRODUCT_ID"
   to map the service packages?


  **Answer:** Yes, It makes sense to use this code.

  # Is it possible that the applicant stop when retrieving and signing, or even
  right after signing the T&Cs, but before completing the onboarding flow?
   What is current onboarding behaviour in this scenario? What if it happens during
   Terms and conditions change? (edge case)


  **Answer:** Is is possible (due to an issue, for example), but if this happens,
  onboarding flow forces the applicant to go back in the onboarding flow and select
  the account products again. With this, Next will retrieve again the required T&C, and
  at this moment, the new T&Cs will be retrieved.

  # For those who already signed the "old generic version" of service package T&C,
  does next forces the applicant to sign the "new version" of this T&C again, even
  if this applicant is not changing plan?

  **Answer:** No, the only moment this T&C are handled is when an applicant is making the onboarding
  or changing a plan. Besides that, Next will not force the applicant to sign a new version
  od any onboaeding T&C.
  end note

  DB-->Next: selected service package T&C
  deactivate DB

end group

Next->UI:List of required T&C for onboarding
deactivate Next


note left of Next
No change on endpoint's response
end note



@enduml
