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

'diagram title'
title UC5 - Create Broadcast Message

'a list of the actors and participants'
actor "SM" as SM

box "Core PNO"
  participant "PO" as PO
  database "PNO DB" as pnodb
end box
box "Next Platform"
    participant "Core Next" as NEXT
    database "Next Database" as nextdb
end box

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
    This diagrams explains how the System Management will include new "broadcast"
    messages (IN_APP messages that will be sent to all users) into Core Next
end legend
note right of SM #plum
**Endpoint**
POST /notification/inApp/broadcast

**Current implementation:**
N/A

**Required implementation:**
Create a "broadcast" message (a message that will be sent to all Customers)
related to the business entity that generated the message. In this case,
the business entity generating messages is a Mimo without eligibility rules,
that is, a HubOffer.

Steps to create a broadcast message:
> Prior to the execution of this endpoint, a SQL script will create a
""BUSINESS_FUNCTION"" specifically to send Broadcast Messages to a certain
Business Entity. For this PB, the ""BUSINESS_FUNCTION"" to be created is
**""BROADCAST_MESSAGES_MIMO""**

# Create a ""BUSINESS_EVENT_TYPE"" with a standardized ""BUSINESS_EVENT_NAME"" and
a ""BUSINESS_FUNCTION_UID"" (both will be provided as parameters of the endpoint)
    i.e. ""BUSINESS_EVENT_TYPE.BUSINESS_EVENT_NAME = BROADCAST_MIMO_16"",
    in which "16" is the ID of the Mimo
    ""BUSINESS_EVENT_TYPE.BUSINESS_FUNCTION_UID = "" ID OF ""BUSINESS_FUNCTION""
    ""BROADCAST_MESSAGES_MIMO"" (created by script).

# With the ID of the newly created ""BUSINESS_EVENT_TYPE"", create a
""BUS_EVENT_NOTIFICATION"", saving the subject and text of the Broadcast Message
that will be sent to all Customers. Also save the ""EXPIRY_TIME"" of the message
and a new flag indicating that this is a Broadcast Message.

Outputs:
* ID and Name of the created BUSINESS_EVENT_TYPE
* ID of the created BUS_EVENT_NOTIFICATION
* Subject and Content of the created BUS_EVENT_NOTIFICATION
* Value of FlagBroadcastMessage and EXPIRY_TIME of the created BUS_EVENT_NOTIFICATION
* ID and Name of the BUSINESS_FUNCTION in which this Broadcast Message was
associated

State changes:
* Insert into BUSINESS_EVENT_TYPE
* Insert into BUS_EVENT_NOTIFICATION
end note

'save huboffer
SM -> SM: Save HubOffer
activate SM
    group #transparent NEW HUBOFFER
        SM -> PO: ""POST /huboffer""
        activate PO
            PO -> pnodb: Save ""HUBOFFER"" info
            activate pnodb
                PO <-- pnodb: New hubOfferId
                deactivate pnodb
            SM <-- PO: Return hubOfferId
        deactivate PO

        group #transparent If should send broadcast message to all customers
            group #transparent Proposal to get the default expiry time
                rnote right of SM #aqua
                **Assumption:**
                This is a sugestion of approach to retrieve the default expiry time for a Broadcast Message.
                But, ultimately, this is up to the Bradesco's team to define how System Management will
                handle this.
                Another possibility is to hardcode the number of days that a Broadcast Message should expire,
                or maybe give to the Manager the possibility to choose whatever date he wants.

                The goal is to have a expiry **DATE** in which this Broadcast Message will expire.
                end note
                SM -> NEXT: ""GET /applicationParameters?parameterName=BROADCAST INAPP DEFAULT EXPIRY TIME""
                activate NEXT
                    NEXT -> nextdb: ""SELECT PARAMETER_VALUE""\n""  FROM APPLICATION_PARAMETER""\n"" WHERE PARAMETER_NAME=parameterName""
                    activate nextdb
                    NEXT <- nextdb: return ""PARAMETER_VALUE"" (the default expiry time in days)
                    deactivate nextdb
                    SM <- NEXT: return expiryTimeInDays
                deactivate NEXT
                SM -> SM: Calculate MESSAGE_EXPIRY_DATE (TODAY + expiryTimeInDays)
            end group

            SM -> NEXT: ""POST /v1/notification/broadcast"" - create a new broadcast message
            activate NEXT
                note right of SM
                    Parameters:
                    ""param { ""
                    ""  eventTypeName: "BROADCAST_MIMO_" + ""hubOfferId"",""
                    ""  businessFunctionId: 99,"" // hardcoded -> id of business function ""BROADCAST_MESSAGES_MIMO""
                    ""  subject: "Subject of the Message",""
                    ""  content: "Content of the Message",""
                    ""  expiryTime: "2018-09-30T00:00:00.000Z","" // value of MESSAGE_EXPIRY_DATE
                    ""  notificationTypeId: 2,"" // hardcoded -> 2 = INAPP
                    ""  notificationRecipientTypeId: 1,"" // hardcoded -> 1 = CUSTOMER
                    ""} ""
                end note

                note right of SM
                Considering the separation of concerns of the microservices architecture,
                the Notification Service / Core Next shouldn't calculate the Expiry Time of a
                IN_APP_MESSAGE.
                The Expiry Time is related to the business logic and needs of the application
                calling the Notification Service, so the Notification Service should not be
                aware of these rules. Its only responsability is to comply with the given
                Expiry Time.
                end note

                NEXT -> NEXT: Validate input data
                activate NEXT
                    note right of NEXT
                        Validations:
                        * param.notificationTypeId **MUST BE ""2""**.
                            - Only InApp Broadcasts are supported now
                        * Check if the following mandatory fields are filled:
                            - eventTypeName
                            - businessFunctionId
                            - content
                            - notificationTypeId
                            - notificationRecipientTypeId
                    end note
                deactivate NEXT

                NEXT -> nextdb: Create a ""BUSINESS_EVENT_TYPE""
                activate nextdb
                    note right of NEXT #aqua
                        **Assumption:**
                        Core Next will not check whether the business function id is valid
                        or not, and will not check if the business function provided is
                        related to Broadcast Messages.
                    end note
                    note right NEXT
                        BUSINESS_EVENT_NAME **MUST BE** unique
                    end note
                    note right NEXT
                        ""INSERT INTO NEXTBANK.BUSINESS_EVENT_TYPE""
                        ""           (BUSINESS_EVENT_TYPE_UID""
                        ""           ,BUSINESS_EVENT_NAME""
                        ""           ,BUSINESS_FUNCTION_UID)""
                        ""    VALUES (NEXTBANK.SQ_BUSINESS_EVENT_TYPE.NEXTVAL""
                        ""           ,""param.eventTypeName
                        ""           ,""param.businessFunctionId"")""
                    end note
                    NEXT <-- nextdb: ""BUSINESS_EVENT_TYPE_UID""
                deactivate nextdb

                NEXT -> nextdb: Create a ""BUS_EVENT_NOTIFICATION""
                activate nextdb
                    note right NEXT
                        ""INSERT INTO NEXTBANK.BUS_EVENT_NOTIFICATION""
                        ""           (BUS_EVENT_NOTIFICATION_UID""
                        ""           ,BUSINESS_EVENT_TYPE_UID""
                        ""           ,NOTIFICATION_TYPE_ID""
                        ""           ,NOTIFICATION_RECIPIENT_TYPE_ID""
                        ""           ,TEMPLATE_SUBJECT""
                        ""           ,TEMPLATE_TEXT""
                        ""           ,ENABLED_IND""
                        ""           ,EXPIRY_TIME"" (new)
                        ""           ,BROADCAST_MESSAGE_IND"" (new)
                        ""           ,BROADCAST_MESSAGE_SENT_IND)"" (new)
                        ""    VALUES (NEXTBANK.SQ_BUS_EVENT_NOTIFICATION.NEXTVAL""
                        ""           ,"" value returned from previous insert
                        ""           ,"" param.notificationTypeId
                        ""           ,"" param.notificationRecipientTypeId
                        ""           ,"" param.subject
                        ""           ,"" param.content
                        ""           ,'Y'"" // default to enabled
                        ""           ,"" param.expiryTime
                        ""           ,'Y'"" // always True in this scenario
                        ""           ,'N')"" // always False when creating a Message
                    end note
                    note right of NEXT #aqua
                    **Assumption:**
                    Core Next will not check if the values of notificationRecipientTypeId and
                    notificationRecipientTypeId are valid and related to Broadcast Messages.

                    **Only** ""BUS_EVENT_NOTIFICATION"" with ""NOTIFICATION_TYPE_UID"" = 2,
                    ""NOTIFICATION_RECIPIENT_TYPE_ID"" = 1 and ""BROADCAST_MESSAGE_IND"" = "Y"
                    will be sent to all Customers.
                    end note
                    NEXT <-- nextdb: ""BUS_EVENT_NOTIFICATION_UID""
                deactivate nextdb



                group #transparent if BusinessEvent AND BusEventNotification was successfully created
                    note right of SM: success, new broadcast message created
                    note right of SM: HTTP success code: 201
                    SM <-- NEXT: Return BUS_EVENT_NOTIFICATION_UID
                end group

                group #transparent else (new businessEventName was not unique)
                    note right of SM: Failed, record not created
                    note right of SM
                        HTTP error code: 409
                        Error message: Business Event Name already exists
                    end note
                    SM <-- NEXT: responseCode=error
                end group

                group #transparent else input(s) missing, extra or incorrect value/datatype
                    note right of SM: Failed, new record not added
                    note right of SM
                        HTTP error code: 400
                        Error message: Bad request
                    end note
                    SM <-- NEXT: responseCode=error
                end group

            deactivate NEXT
        end group

    end group

deactivate SM


@enduml
