@startuml
'diagram parameters'
'skinparam dpi 300
skinparam LegendBackgroundColor #white
'diagram title'
title PB-362: Candidate flow for avoiding POST calls for credit assessment
'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
<color: orange><b>API calls</b></color>
<color: lightblue><b>DB updates/inserts</b></color>
<color: lightgreen><b>Successful outcome</b></color>
For this to work we need to store 4 things:
* date of last POST to the credit assessment API (last_credit_assessment_POST_date)
* whether this last POST was for student or non-student check (last_credit_assessment_type)
* approved amount for student credit (student_credit)
* approved amount for non-student credit (non_student_credit)
With these four bits of information we can use the logical flow detailed below in order to avoid calling the POST API more than once per day for each of the types of credit assessment for a given customer.
All of this (note and diagram) applies PER CUSTOMER, and where the data is stored (i.e. which table) doesn't matter, so long as there is no impact to any other PB or functionality, and no ambiguety in the use / meaning of the fields.
end legend
start
while (student_credit == null\nOR\nnon_student_credit == null)
  if (student_credit == null?) then (yes)
    :student_credit outcome is not known;
    if (last_credit_assessment_POST_date == today()?) then (yes)
      :There is a valid credit assessment, but is it the right type?;
      if (last_credit_assessment_type == "student"?) then (yes)
        :Yes, a student assessment was already requested today;
        #orange:Call GET to retrieve outcome;
        if (GET provides credit outcome?) then (yes)
          :We have our student credit assessment outcome;
          #lightblue:Update student_credit = {outcome};
          #lightgreen:We will not have to call for student credit assessment again;
        else (no)
          :The assessment isn't ready, we'll have to try again later;
        endif
      else (no)
        :No, therefore the last assessment must have been for non-student;
        if (non_student_credit == null?) then (yes)
          :We still need to resolve non_student_credit so move forward;
          :We'll resolve the non-student credit outcome first (below)
          and then do the student one on the next loop around so we avoid
          calling the API more than once for each;
        else (no)
          :We have already resolved non_student_credit;
          :We want to resolve student_credit now;
          :We need to request assessment for student credit;
          #orange:Make POST call for student credit assessment;
          #lightblue:Update last_credit_assessment_POST_date = today();
          #lightblue:Update last_credit_assessment_type = "student";
          :Now we wait for outcome whilst we find out by calling the GET;
          :It is not possible to trigger this call twice in one day
          so we will never call the POST more than once per day for
          student assessment;
        endif
      endif
    else (no)
      :Our last request for credit assessment is out of
      date or was not made;
      :We need to request assessment for student credit;
      #orange:Make POST call for student credit assessment;
      #lightblue:Update last_credit_assessment_POST_date = today();
      #lightblue:Update last_credit_assessment_type = "student";
      :Now we wait for outcome whilst we find out by calling the GET;
      :It is not possible to trigger this call twice in one day
      so we will never call the POST more than once per day for
      student assessment;
    endif
  else (no)
    :student_credit outcome is known already;
    #lightgreen:No need to do anything more for student credit,
    we can move on;
  endif
  if (non_student_credit == null?) then (yes)
    :non_student_credit outcome is not known;
    if (last_credit_assessment_POST_date == today()?) then (yes)
      :There is a valid credit assessment, but is it the right type?;
      if (last_credit_assessment_type == "non_student"?) then (yes)
        :Yes, a non-student assessment was already requested today;
        #orange:Call GET to retrieve outcome;
        if (GET provides credit outcome?) then (yes)
          :We have our non-student credit assessment outcome;
          #lightblue:Update non_student_credit = {outcome};
          #lightgreen:We will not have to call for non-student credit assessment again;
        else (no)
         :The assessment isn't ready, we'll have to try again later;
        endif
      else (no)
        :No, therefore the last assessment must have been for student;
        if (student_credit == null?) then (yes)
          :We still need to resolve student_credit so move forward;
          :We'll resolve the non-student credit outcome first on the next
          loop around, and then the non-student afterwards, so we avoid
          calling the API more than once for each;
        else (no)
          :We have already resolved student_credit;
          :We want to resolve non_student_credit now;
          :We need to request assessment for non-student credit;
          #orange:Make POST call for non-student credit assessment;
          #lightblue:Update last_credit_assessment_POST_date = today();
          #lightblue:Update last_credit_assessment_type = "non-student";
          :Now we wait for outcome whilst we find out by calling the GET;
          :It is not possible to trigger this call twice in one day
          so we will never call the POST more than once per day for
          non-student assessment;
        endif
      endif
    else (no)
      :Our last request for credit assessment is out of
      date or was not made;
      :We need to request assessment for non-student credit;
      #orange:Make POST call for non-student credit assessment;
      #lightblue:Update last_credit_assessment_POST_date = today();
      #lightblue:Update last_credit_assessment_type = "non-student";
      :Now we wait for outcome whilst we find out by calling the GET;
      :It is not possible to trigger this call twice in one day
      so we will never call the POST more than once per day for
      non-student assessment;
    endif
  else (no)
    :non_student_credit outcome is known;
    #lightgreen:No need to do anything more for non-student credit,
    we can move on;
  endif
endwhile (student_credit != null\nAND\nnon_student_credit != null\ni.e. flow goes through green paths all the way)
#lightgreen:Both credit outcomes are known, no API calls to make;
stop
@enduml
