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

'diagram title'
title UC14 - Create a goal

'a list of the actors and participants'
actor "Next Customer" as UI
box "Next Platform"
participant "Core Next" as Next
database "Next Database" as DB
end box
participant "Bradesco" as FDOS

'a brief description of the diagram'
legend top
This use case is for creating a Goal 2.0. It will require a new Core Next endpoint to guarantee
backwards compatibility. The implementation is similar to the existing POST /goals endpoint, with
some additiona input, logic, and outputs.

The current Goals 1.0 POST /goals endpoint would:
1. Create a goal record
2. Update Terms_And_Conditions_Accept table with the Goal_UID

The new Goals 2.0 POST /goals/goalsAndSignature endpoint will conduct the following within a database transaction:
1. Create a goal
2. Create a goal subscription plan
3. Accept all necessary terms and conditions for the creation of this goal (at least 1, maxiumum 3)
4. Perform all defensive checks required
end legend


note right of UI #orange
**Note to Developers**
This UC describes the changes needed to the
existing POST /goals logic. However, we are
building this as a NEW ENDPOINT.
end note

UI->Next: **HTTP**\nPOST /goals/goalsAndSignature
activate Next
note right of UI
UI calls Core Next to create a goal 2.0.
It would pass it in existing input parameters
in POST /goals plus a few additional.

Current input parameters:
"estimatedEndDate": string - GOAL.GOAL_ESTIMATED_END_DT,
"goalType": integer - GOAL.GOAL_TYPE_UID,
"name": string - GOAL.GOAL_NAME,
"recommendedMonthlyAmount": number - GOAL.RECOMENDED_MONTHLY_AMT and
GOAL_SUBSCRIPTION_PLAN.MONTHLY_AMOUNT
"targetAmount": number - GOAL.GOAL_TARGET_AMT,

Remove from input:
"termsAndCondAcceptUid": integer

Additional input parameteres:
1. "fundId": integer - GOAL.EXTERNAL_CORE_PRODUCT_ID (mandatory)
2. "depositDay": integer - GOAL_SUBSCRIPTION_PLAN.DEPOSIT_DAY (mandatory)
3. "GOAL_termsAndCondUid": integers - Terms_And_Conditions.GOAL_termsAndCondUid (mandatory)
4. "RefusalOfAdvice_termsAndCondUid": integers - Terms_And_Conditions.GOAL_termsAndCondUid (optional)
5. "Fund_termsAndCondUid": integers - Terms_And_Conditions.GOAL_termsAndCondUid (optional)

Note:
Before (PB-055), when the client creates a goal, the app suggested a monthyAmount, and the Core Next
stored that information in the GOAL_SUBSCRIPTION_PLAN.MONTHLY_AMOUNT. From now on, we will create a goal
tied to a goal subscription plan, so don't make sense use this field anymore. Core Next needs to
store recommendedMonthlyAmount both in GOAL table and GOAL_SUBSCRIPTION_PLAN table.
end note


  GROUP Wrap in transaction

    opt App passes Fund_termsAndCondUid
      ref over Next
      1. Use existing methodology to create
      a Terms_And_Conditions_Accept record
      for the Fund T&C with the Customer_UID.
      end ref
      note right of Next #coral
      **Clarification**
      Is it ok for a customer to have multiple
      Fund T&Cs signed? Do we need a defensive
      check to ensure we are not inserting multiple
      or is that not necessary?

      **Answer**
      Yes, it is ok for a customer have multiple
      Fund T&Cs signed. Core Next don't need to
      perform a defensive check for this.
      end note
      note right of Next #Coral
      **Clarification**
      There is also a GOAL_UID in the Terms_And_Conditions_Accept
      table. Do we need to update this so that the Terms_And_
      Conditions_Accept is also tied to a Goal? Or is that not necessary.

      **Answer**
      There's no need update de GOAL_UID in the Terms_And_Conditions_Accept
      table because the T&C is tied to the customer, not to the goal.

      end note
    end opt

    opt App passes RefusalOfAdvice_termsAndCondUid
      ref over Next
      2. Use existing methodology to create
      a Terms_And_Conditions_Accept record
      for the Refusal of Advice T&C with the
      Customer_UID.
      end ref
      note right of Next #orange
      **Note to developers**
      Please follow the logic in existing Core
      Next endpoint POST /investments/refusalOfAdvice/signature
      to accept the Refusal of Advice T&C.

      Make sure that we are calling APIC to let them know
      that the customer signed the T&C.
      end note
      note right of Next #red
      **Assumption**
      We will need to make an APIC call to let
      it know that the customer has signed a
      Refusal of Advice T&C. APIC will update
      the system so that when we make APIC investor
      profile status checks, their status code
      will come back with a 5 (no investor profile,
      accepted valid Refusal of Advice T&C).
      end note
      note right of Next #coral
      **Clarification**
      Is it ok for a customer to have multiple
      Refusal of Advice T&Cs signed? Do we need
      a defensive check to ensure we are not
      inserting multiple  or is that not necessary?

      **Answer**
      Yes, it is ok for a customer have multiple
      Refusal of Advice T&Cs signed. Core Next don't
      need to perform a defensive check for this.
      end note
      note right of Next #Coral
      **Clarification**
      There is also a GOAL_UID in the Terms_And_Conditions_Accept
      table. Do we need to update this so that the Terms_And_
      Conditions_Accept is also tied to a Goal? Or is that not necessary.

      **Answer**
      There's no need update de GOAL_UID in the Terms_And_Conditions_Accept
      table because the T&C is tied to the customer, not to the goal.

      end note
    end opt

    ref over Next
      Do UC14.2 Create goal defensive checks
    end ref
    note right of Next
      We then perform the defensive checks
      to ensure we can actually create a goal
      and the subscription plan.
    end note

  alt Defensive checks pass

    ref over Next
    3. Use existing methodology to insert
    one record into GOALS table.
    end ref

    ref over Next
    4. Use existing methodology to
    insert one record into
    GOAL_SUBSCRIPTION_PLAN table.
    end ref

    ref over Next
    3. Use existing methodology to
    sign the Terms_And_Conditions_Accept
    Goal T&C record AND include the new
    GOAL_UID in the insert.
    end ref
    note right of Next #orange
    Previously on insert GET /goals/termsAndConditions,
    we would not have had a GOAL_UID on
    end note

    note right of Next
    If all defensive checks pass, then Core Next will:
    1. Create new GOAL record
    2. Create new GOAL_SUBSCRIPTION_PLAN record
    3. Update Terms_And_Conditions_Accept record
      with GOAL_UID
    4. Update Terms_And_Conditions_Accept record
        for Fund T&C with CUSTOMER_UID (if required)
    5. Update Terms_And_Conditions_Accept record
        for Refusal of Advice T&C with CUSTOMER_UID
        (if required)
    Note that all of this will be wrapped in a database
    transaction. Therefore, if any part of this fails,
    we will roll back all inserts or updates.
    end note
  end GROUP

  alt Transaction completed successfully
    Next --> UI: Response
    note right of UI
    Once the goal / goal subscription
    plan is inserted, we return the
    following:
      Goal response {
        estimatedEndDate (string),
        goalTypeId (integer),
        goalTypeName (string),
        name (string),
        partnerProductId (string, optional),
        recommendedMonthlyAmount (number),
        shared (boolean, optional),
        status (string) = ['ONGOING', 'COMPLETED', 'CANCELED', 'TO_CANCEL', 'ACHIEVED', 'HIDDEN'],
        statusDate (string),
        system (System Response, optional),
        targetAmount (number),
        transaction (integer),
        uniqueID (integer)
      }
      System Response {
        code (string, optional),
        messages (Array[string], optional)
      }
      end note
    else Transaction failed
      Next --> UI: Error
      note right of UI #pink
      If goal and goal subscription
      plan insertion fails, we will
      return the HTTP status code
      with the following message in
      portuguese:
        **Ops**
        Deu error na hora de criar seu
        Objetivo. Tenta de novo.
      end note
    end alt
else Any defensive check fails
  Next --> UI: Error
  deactivate Next
  note right of UI #pink
  If any of the defensive checks failed,
  we will return an error to indicate
  what caused the failure.
  end note
end note

@enduml
