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

'diagram title'
title PB-428 Phase 4 UC4 - Create goal 2.0

'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 "Core Bradesco" as BR

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
This is an existing endpoint for the creation of a goal, a subscription plan and acceptance of T&Cs required. Currently, it will allow for the acceptance of three T&Cs:
1. Goal T&C (always)
2. Fund T&C (if required)
3. Refusal of Advice (if required)

With this change, a customer will also be able to accept the new Absence of Profile T&C when creating a goal. See changes to this endpoint in **LIGHT BLUE** notes.
end legend


UI->Next: **HTTP**\nPOST /v1/goals/goalsAndSignature
activate Next

note right of UI
UI calls existing Core Next endpoint to create
a goal 2.0.

It will pass the following parameters:
CreateGoalAndSignatureRequest {
  depositDay (integer): Deposit day for the subscription plan ,
  estimatedEndDate (string),
  fundId (integer): EXTERNAL_CORE_PRODUCT_ID, located on PRODUCT table ,
  goalType (integer): Type of goal selected by User ,
  monthlyAmount (number): Montlhly amount ,
  name (string): Description of goal by User Must have a minimum length of 1 and a maximum length of 50. ,
  signatureList (Array[GoalSignature]): Terms and conditions signature list. The GOAL T&C is always mandatory ,
  targetAmount (number): Total amount that is goal target
}
GoalSignature {
  termsAndConditionUid (integer, optional): ID related to table TERMS_AND_CONDITION
}
end note
note right of Next #lightblue
The signatureList array might
contain the new Absence of Profile
T&C when the App calls Core next.
end note

ref over Next
Do Sprint 1 UC10 - Get Goal T&Cs
end ref
note right of Next
First we will do Sprint 1 UC10 - Get Goal T&Cs.
This will return the Goal T&Cs that the customer
needs to sign in order to create a goal.
end note

opt Sprint 1 UC10 returns a Refusal of Advice T&C and input parameter signatureList does not contain a Refusal of Advice T&C ID
  Next --> UI: Error
  note right of UI
  If Sprint 1 UC10 tells us the customer
  needs to sign a Refusal of Advice T&C
  and the signatureList does not contain
  a Refusal of Advice T&C ID, we will return
  an error to the App.
  end note
end opt

opt Sprint 1 UC10 returns a Fund T&C and input parameter signatureList does not contain a Fund T&C ID
  Next --> UI: Error
  note right of UI
  If Sprint 1 UC10 tells us the customer
  needs to sign a Fund T&C and the
  signatureList does not contain a Fund
  ID, we will return an error to the App.
  end note
end opt


opt Sprint 1 UC10 returns an Absence of Profile T&C and input parameter signatureList does not contain an Absence of Profile T&C ID
  Next --> UI: Error
  note right of UI #lightblue
  If Sprint 1 UC10 tells us the customer
  needs to sign an Absence Of Profile T&C
  and the signatureList does not contain
  an Absence of Profile T&C ID, we will
  return an error to the App.
  end note
end opt

ref over Next
Do Sprint 1 UC1 - Get minimum values for available funds
end ref
note right of Next
Next Core Next should check if the installment
amount is valid. It will get the minimum values
for the selected fund by using Sprint 1 UC1 -
Get minimum values for available funds.

The monthly installment amount is valid if both
are true:
  1. <= Goal Target
  2. >= Minimum initial investment amount
end note

opt installment amount is not valid
  Next --> UI: Error
  note right of UI
  If the installment amount is not valid,
  we will return an error to the App.
  end note
end opt

note right of Next
If all above checks have passed and Core
Next does not return an error to the App,
we will create all necessary Terms_And_
Conditions_Accept records, the goal record,
and the GOAL_SUBSCRIPTION_PLAN record
all in one database transaction. If any
database insertions fail, we will roll back.

1. Create Terms_And_Conditions_Accept record
    for Fund T&C with CUSTOMER_UID (if required)
2. Create Terms_And_Conditions_Accept record
    for Refusal of Advice T&C with CUSTOMER_UID
    (if required)
3. Create new GOAL record
4. Create new GOAL_SUBSCRIPTION_PLAN record
5. Create Terms_And_Conditions_Accept record
  with GOAL_UID
end note

GROUP Wrap in transaction

  opt App passes a Absence of Profile T&C ID in the signatureList
    ref over Next
    1. Use existing methodology to create
    a Terms_And_Conditions_Accept record
    for the Absence of Profile T&C with
    the Customer_UID.
    end ref
    note right of Next #lightblue
    **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.
    end note
  end opt

  opt App passes a Fund T&C ID in the signatureList
    ref over Next
    2. 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 a Refusal of Advice T&C ID in the signatureList
    ref over Next
    3. 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
  4. Use existing methodology to insert
  one record into GOALS table.
  end ref

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

  ref over Next
  6. 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
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),
      MonthlyAmount (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



@enduml
