@startuml


partition "data-based check" {
    "Check based on local database" -down-> "Is addon_match set to true?"
    --> if "" then
        -down->[no] "Is the order in status PAID or PENDING\nand is the position not canceled?"
    else
        -right->[yes] "Build a list that includes the position\nas well as all its add-ons"
        -down-> "Filter list for products that are part of the check-in list"
        --> if "" then
            -down->[one found] Proceed with the matching position
            --> "Is the order in status PAID or PENDING\nand is the position not canceled?"
        else
            --> if "" then
                -right->[none found] "Return error PRODUCT  "
            else
                -down->[multiple found] Return error AMBIGUOUS
            endif
        endif
    endif

    "Is the order in status PAID or PENDING\nand is the position not canceled?" --> if "" then
        -right->[no] "Return error CANCELED"
    else
        -down->[yes] "Is one or more block set on the ticket?"
        --> if "" then
            -right->[yes] "Return error BLOCKED"
        else
            -down->[no] "Is the order in status PENDING and not yet approved?"
            --> if "" then
                -right->[yes] "Return error UNAPPROVED"
            else
                -down->[no] "If this is not an exit, is the valid_from/valid_until\nconstraint on the ticket fulfilled?"
                --> if "" then
                    -right->[no] "Return error INVALID_TIME"
                else
                    -down->[yes] "Is the product part of the check-in list?"
                    --> if "" then
                        -right->[no] "Return error PRODUCT"
                    else
                        -down->[yes] "Is the subevent part of the check-in list?"
                        --> if "" then
                            -right->[no] "Return error INVALID"
                            note bottom: TODO\ninconsistent\nwith online\ncheck
                        else
                            -down->[yes] "Is the order in status PAID?"
                            --> if "" then
                                -right->[no] "Is Order.valid_if_pending set?"
                                --> if "" then
                                    -->[yes] "Is this an entry or exit?"
                                else
                                    -->[no] "Does the check-in list include pending orders?"
                                    --> if "" then
                                        -->[no] "Return error UNPAID "
                                    else
                                        -->[yes] "Is ignore_unpaid set on the request?\n(Has the operator confirmed\nthe checkin?)"
                                        --> if "" then
                                            -->[no] "Return error UNPAID "
                                        else
                                            -->[yes] "Is this an entry or exit?"
                                        endif
                                    endif
                                endif
                            else
                                -down->[yes] "Is this an entry or exit?"
                            endif
                        endif
                    endif
                endif
            endif
        endif
    endif

    "Is this an entry or exit?" --> if "" then
        -right->[entry] Evaluate custom logic (rules)
        --> if "" then
            -right->[error] "Return error RULES"
        else
            -down->[ok] "Are all required questions answered?"
            --> if "" then
                -right->[no] "Return error INCOMPLETE"
            else
                -down->[yes] "Does the check-in list allow multi-entry?"
            endif
        endif
    else
        -->[exit] "Return OK "
    endif

    "Does the check-in list allow multi-entry?" --> if "" then
        -right->[yes] "Return OK"
    else
        -down->[no] "Is this the first checkin\nfor this ticket on this list?"
        --> if "" then
            -right->[yes] "Return OK"
        else
            -down->[no] "Are all previous checkins\nfor this ticket on this list exits?"
            --> if "" then
                -right->[yes] "Return OK"
            else
                -down->[no] "Does the check-in list\n allow entry after exit\nand is the last checkin\nan exit?"
                --> if "" then
                    -right->[yes] "Return OK"
                else
                    -down->[no] "Return error ALREADY_REDEEMED"
                endif
            endif
        endif
    endif
}

partition "dataless check" {
    "Check based on secret content" --> "Does the secret decode with\nany supported scheme\nand has a valid signature?"

    --> if "" then
        -down->[yes] "Is the ticket secret on the revocation list?"
        --> if "" then
            -right->[yes] "Return error REVOKED"
        else
            -down->[yes] "Is the ticket secret on the block list?"
            --> if "" then
                -right->[yes] "Return error BLOCKED "
            else
                -down->[yes] "If this is not an exit, is the valid_from/valid_until\nconstraint on the ticket fulfilled? "
                --> if "" then
                    -right->[no] "Return error INVALID_TIME "
                else
                    -down->[no] "Is the product part of the check-in list? "
                    --> if "" then
                        -right->[no] "Return error PRODUCT "
                    else
                        -down->[yes] "Is the subevent part of the check-in list? "
                        --> if "" then
                            -right->[no] "Return error INVALID  "
                            note bottom: TODO\ninconsistent\nwith online\ncheck
                        else
                          --> "Is this an entry or exit? "
                        endif
                    endif
                endif
            endif
        endif
    else
        -right>[no] "Return error INVALID "
    endif

    "Is this an entry or exit? " --> if "" then
        -right->[entry] "Evaluate custom logic (rules) "
        --> if "" then
            -right->[error] "Return error RULES "
        else
            -down->[ok] "Are all required questions answered? "
            --> if "" then
                -right->[no] "Return error INCOMPLETE "
            else
                -down->[yes] "Does the check-in list allow multi-entry? "
            endif
        endif
    else
        -->[exit] " Return OK "
    endif

    "Does the check-in list allow multi-entry? " --> if "" then
        -right->[yes] "  Return OK  "
    else
        -down->[no] "Are any locally queued checkins for\nthis ticket of this list known?"
        --> if "" then
            -right->[no] "  Return OK  "
        else
            -down->[yes] "Are all locally queued checkins\nfor this ticket on this list exits? "
            --> if "" then
                -right->[yes] "  Return OK  "
            else
                -down->[no] "Does the check-in list\n allow entry after exit\nand is the last locally\nqueued checkin\nan exit? "
                --> if "" then
                    -right->[yes] "  Return OK  "
                else
                    -down->[no] "Return error ALREADY_REDEEMED "
                endif
            endif
        endif
    endif
}

(*) --> "Check if order position with\nscanned ticket secret exists"
--> if "" then
    -down->[yes] "Check based on local database"
else
    -->[no] "Check based on secret content"
endif

@enduml
