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

'diagram title'
title PB-202: UC24.1 Process MQ message

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
Process to analyse daily MQ messages and sending out notifications to customers who have fallen behind on their debt payments.
DTS - Debt Service
NS - Notification Service
end legend

partition Bradesco #lightblue {
  start
  :Bradesco inserts an individual Next customer (debtor) record in an MQ;

}
partition Next #lightgrey {

    partition DTS {

    while (Whilst MQ contains records)
          :DTS fetches customer record from MQ;
          :We do a number of validations;

              if (CPF (CPF) belongs to a Next customer) then (yes)
                :Is a Next customer
                CUSTOMER_UID identified by making a RESTful API call;
                note
                **Note:**
                The service providing the RESTful API call will be determined later
                end note
                if (agency (AGENCIA) and account number (CONTA) match Next customer?) then (yes)
                  :Agency and account number are correct;
                  if (number of days of delay (QDIA-ATRSO-CONTR) > 0) then (yes)
                    :delay period is valid;
                    :Record is correct and shall be processed;

                      #limegreen:Desired behaviour is to send appropriate notification;
                      :Identify the correct BUSINESS_EVENT_UID;
                      :Send a notification request to the Notification Service (NS);
                      :POST /next-notification/v1/X
                      where X is the channel and can be: push or in-app or sms
                      the request body would include things like (different for push/in-app/sms):
                      priority=Normal
                      cpf = CPF (for /push) from from RESTful API call (replaced CUSTOMER_MATERIALISED_VIEW table)
                      phoneNumber = PHONE_NUMBER (for /sms) from from RESTful API call (replaced CUSTOMER_MATERIALISED_VIEW table)
                      customerId = CUSTOMER_UID from RESTful API call (replaced CUSTOMER_MATERIALISED_VIEW table)
                      business_event_id = BUSINESS_EVENT_UID (indicates appropriate debt message)
                      context = null
                      firstStartDateTime = START_TIME timestamp calculated from DEBT_BUSINESS_PARAMETER table (e.g. Monday date and hours)
                      firstEndDateTime = END_TIME timestamp calculated from DEBT_BUSINESS_PARAMETER table
                      secondStartDateTime = START_TIME timestamp calculated from DEBT_BUSINESS_PARAMETER table (e.g. Tuesday date and hours)
                      secondEndDateTime = END_TIME timestamp calculated from DEBT_BUSINESS_PARAMETER table;
                      partition NS {
                        :HTTP POST received;
                        :Persist notification request on internal queue;
                        :Return: 202 Accepted;
                      }
                      :Persistence in NS confirmed;
                      :Done with this customer, move to next;

                  else (no)
                    :delay period is not valid;
                    :Log invalid number of days error with CPF;
                    #coral:Stop processing this record and move on to the next MQ message;
                  endif
                else (no)
                  :Agency and account number are not correct;
                  :Log agency and account number error with CPF;
                  #coral:Stop processing this record and move on to the next MQ message;
                endif
              else (no)
                :Is not a Next customer;
                :Log not Next customer error with CPF;
                note
                **Note:**
                We will log the error in a file similar to what we are doing for Notification Service.
                No API required to read this error log.
                end note
                #coral:Stop processing this record and move on to the next MQ message;
              endif
        endwhile (all MQ records processed)

    :All records in MQ have processed;
stop
    }
}

@enduml
a
