@startuml
scale 0.55
autonumber
'diagram title'
title UC1 - Get Eligibility to Survey

'a brief description of the diagram'
legend top
-- \t**Abstract:**\t --
It verifies if the customer is eligible to a survey in the app and it returns the survey customer should respond.
end legend

actor "Next App" as App
box "Survey"
    participant "Partnerships & Offers" as PartnershipsAndOffers
    database "P&O DB" as PartnershipsAndOffersDB
end box

note right of App #plum
    **Endpoint**
    ""GET /v1/surveys/eligibility/{journeyId}""

    **Current implementation:**
    N/A (New endpoint)

    **Required implementation:**
    The system should retrieve information about whether or not the system should display a survey dialog.
    If the customer is eligible to a survey, then the system should retrieve the survey which has the minimum endDate from the list of surveys the customer is eligible to respond.

    Input:
    * ""SurveyEligibilityRequest(""
      ""   customerId,""
      ""   journey""
      "")""

    Outputs:
    * ""SurveyEligibilityResponse(""
      ""   displaySurveyDialog,""
      ""   firstResponse,""
      ""   survey {""
        ""      surveyId,""
        ""      startDate,""
        ""      endDate,""
        ""      active,""
        ""      name,""
        ""      description,""
        ""      headerText,""
        ""      footerText,""
        ""      journeyId,""
        ""      journeyName,""
        ""      questions [{""
        ""          questionId,""
        ""          name,""
        ""          description,""
        ""          type,""
        ""          answers [{""
        ""            answerId,""
        ""            answerName""
        ""          }]""
        ""      }]""
        ""   }""
        "")""
      * responseCode

    Note: the system will return just 1 survey which has the minimum endDate.

end note

note right of App #orange
    **Assumptions**
    The Analytics will insert data into the tables:
      journey
      answer
      question
      survey
      survey_eligible_customer
end note

note right of App #plum
    **Rule for displaying the survey dialog**
    The App should display the dialog when displaySurveyDialog = true.
    The displaySurveyDialog will contain true just when the customer is accessing the screen for the 1st or 3rd time.
end note

'interactions'
App -> PartnershipsAndOffers: **HTTP**\nGET /v1/surveys/eligibility\nGet the next survey the customer is eligible to respond
rnote right of App
""{""
""  "customerId": Long""
""  "journey": String""
""}""
end note
activate PartnershipsAndOffers
    rnote right of PartnershipsAndOffers
        **Steps that will be taken in this endpoint:**
        * Validate eligibility form request
        * Retrieve eligibility information and survey form from P&O Database according to the required implementation
    end note

    group Validate question form request
        rnote right of PartnershipsAndOffers
            **Validations:**
            * All fields are mandatory
            * Customer exists
        end note

        PartnershipsAndOffers -> PartnershipsAndOffersDB: **Retrieve Surveys Assigned to the Customer**\nThe data returned by the script below will be used to return the surveys system should display.\n\nScript suggested:\n\nselect *\nfrom survey_eligible_customer sec\ninner join survey s on s.survey_id = sec.survey_uid\ninner join journey c on c.journey_uid = s.journey_id\ninner join survey s on s.survey_id = sec.survey_uid\ninner join survey_question sq on sq.survey_id = s.survey_id\ninner join question q on q.question_id = sq.question_id\ninner join question_answer qa on qa.question_id = q.question_id\ninner join answer a on a.answer_id = qa.answer_id\nwhere survey_eligible_customer.customer_uid = <customerId>\nand survey.start_dt <= today\nand survey.end_dt >= today\nand survey.active = true\nand journey.journey_uid = <journeyId>\norder by survey.end_dt asc
        activate PartnershipsAndOffersDB
            PartnershipsAndOffersDB -> PartnershipsAndOffers: ""One survey: the survey that has the minimum endDate""
        deactivate PartnershipsAndOffersDB

        PartnershipsAndOffers -> PartnershipsAndOffersDB: **Add information about the first response**\nIf there is no register for the customer and the survey\n\tthen the response should contain firstResponse = true\nelse firstResponse = false.\n\nSuggested script:\nselect count(*)\nfrom customer_response cr\nwhere customer_uid = <customerId>\nand survey_id = <surveyId>
        activate PartnershipsAndOffersDB
            PartnershipsAndOffersDB -> PartnershipsAndOffers: ""Response with flag firstResponse""
        deactivate PartnershipsAndOffersDB

        PartnershipsAndOffers -> PartnershipsAndOffersDB: **Add information about the disposal of survey dialog**\nIf the customer never accessed the survey (no register on customer_response) or accessed the survey for third time (number_of_accesses is currently 2),\n\tthen the system should return displaySurveyDialog = true\nelse displaySurveyDialog = false.\n\nSuggested script:\n\nselect number_of_accesses\nfrom customer_response cr\nwhere customer_uid = <customerId>\nand survey_id = <surveyId>\nand upper(answered) = 'N'\nand customer_id NOT IN(\n\t\tselect customer_id\n\t\tfrom customer_response\n\t\twhere customer_id = <customerId>\n\t\tand survey_id = <surveyId>\n\t\tand upper(answered) = 'Y')
        activate PartnershipsAndOffersDB
            PartnershipsAndOffersDB -> PartnershipsAndOffers: ""Response with flag displaySurveyDialog""
        deactivate PartnershipsAndOffersDB

        alt #transparent A mandatory field is not filled
            PartnershipsAndOffers -> App: Error - Bad Request
            rnote left of PartnershipsAndOffers
                HTTP Status:
                400
            end note
            else customer does not exist
                PartnershipsAndOffers -> App: Error - Bad Request
                note left of PartnershipsAndOffers
                    **Note:**
                    PartnershipsAndOffers will NOT get eligibility information if the customer does not exist.
                end note
            else journey does not exist
                PartnershipsAndOffers -> App: Error - Bad Request
                note left of PartnershipsAndOffers
                    **Note:**
                    PartnershipsAndOffers will NOT get eligibility information if the journey does not exist.
                end note
        end alt
    end group
    PartnershipsAndOffers -> App: The survey that has the minimum endDate
    rnote left of PartnershipsAndOffers
        HTTP Status
        200 - Success
    end note

deactivate PartnershipsAndOffers

center footer \n\n\n\n\n\n\n\n\nCapco - Confidential\n%date[HH:MM '-' dd.MM.yyyy]%\n%filename%

@enduml
