@startuml
    title PUSHBROOM - Query Push Token API
    autonumber "<color:red><b>00 "
    skinparam backgroundColor #FFFFFF
    skinparam sequence {
        TitleFontSize 20
        ActorFontSize 12
        ParticipantFontSize 12
        ParticipantPadding 40
        ArrowFontSize 12
    }
    
    actor  "        User       " as USR
    participant "  PUSHBROOM Microservice " as BRASS_PBS
    database "   PUSH_DB.PUSH_BROOM   " as PUSHBROOM_DB

    USR -> BRASS_PBS: Invoke GET /pbs/v1/push-token API
    note right of USR: Query params - <i>trans_id=test_pbs_search&networkMsisdn=msisdn</i>

    == Traffic Validation ==

    alt If request IP is greylisted or not whitelisted
        BRASS_PBS -> USR:     <b> 401 Unauthorized</b>
        note left of BRASS_PBS: { \n     "error_code": "Unauthorized" \n     "error_description": "Invalid Authorization header"  \n }
    end

    == Validate Request ==

    alt If body does not contain trans_id OR any one of fcmId / networkMsisdn / uuid / email
        BRASS_PBS -> USR:     <b> 400 Bad Request</b>
        note left of BRASS_PBS: { \n     "error_code": "invalid_request" \n     "error_description": "Request does not have mandatory Parameters"  \n }
    end

    == Query the database ==

    BRASS_PBS <--> PUSHBROOM_DB:    Create connection to database
    BRASS_PBS -> PUSHBROOM_DB: Execute SELECT query based on the reqesut params
    note right of BRASS_PBS: <i>SELECT * FROM PUSH_BROOM WHERE MSISDN=networkMsisdn ... order by LAST_UPDT_TIME desc</i>

    alt If database returns no error
        PUSHBROOM_DB -> BRASS_PBS: Returns query response containing DB rows
        note left of PUSHBROOM_DB: [{ FCM_TOKEN: '', MSISDN:'' }]
        BRASS_PBS -->x PUSHBROOM_DB: Release the connection
        BRASS_PBS -> USR: <b> 200 OK </b>
        note left of BRASS_PBS: { \n     "status": 200, \n     "resultCount": 1,  \n      "results": [{ \n           "FCM_TOKEN" \n           "MSISDN", \n            ... \n      }]  \n }
    else If any database error occurs
        PUSHBROOM_DB -> BRASS_PBS: Returns error
        note left of PUSHBROOM_DB: { "code": "error code", "sqlMessage": "error message" }
        BRASS_PBS -->x PUSHBROOM_DB: Release the connection
        BRASS_PBS -> USR:     <b> 400 Bad Request</b>
        note left of BRASS_PBS: { \n     "error_code": "<i>db error code</i>" \n     "error_description": "<i>error message</i>"  \n }
    end
@enduml