@startuml

entity "Mojaloop Adapter" as dfsp
note over dfsp
    * Participant Bank A
    * Participant Bank B
end note

box Central-Ledger #FFFFF9
    participant "**API**" as cl_api
    participant "__api__ / Handler" as cl_handler
    participant "__domain__ / Service" as cl_domain
    participant "__models__ / Facade" as cl_facade

    note over cl_api
        REST API deployed in NodeJS
    end note

    note over cl_handler
        JSON request decoded and
        sent for processing via a handler
    end note

    note over cl_domain
        Domain folder where the service
        functions are exposed.
    end note

    note over cl_facade
        Facade for storing/retrieving data from
        MySQL and TigerBeetle
    end note
end box

box Storage #F9FFF9
	control "TigerBeetle Client" as tb_client
	collections "TigerBeetle Cluster" as tb_nodes
	database "central-ledger" as cl_db
	database "Redis" as redis_cache
end box

autonumber
group Create Transfer (2-Phase)
    dfsp -> cl_api : //JSON// \n**(transfer data)**
    cl_api -> cl_handler : ///jmeter/transfers/prepare// \nHandler invoked
    cl_handler -> cl_domain : Service layer invoked
    cl_domain -> cl_facade : Domain to facade
    cl_facade -> cl_db : Perform the following validations against the database:\n*//validateFspiopSourceMatchesPayer//\n*//validateParticipantByName//\n*//validatePositionAccountByNameAndCurrency//\n*//validateParticipantByName//\n*//validateAmount//\n*//validateConditionAndExpiration//\n*//validateDifferentDfsp//
    cl_facade -> redis_cache : Lookup participant data by \n//name//, //account type// (POSITION) and //currency//.
    autonumber stop
    cl_facade --> cl_db : If the participant data is not available in Redis, a database lookup is performed
    cl_facade --> redis_cache : Data looked up is now stored in Redis.
    autonumber resume
    cl_facade -> tb_client : Pending //Transfer// requested via the CL-TB interface.
    tb_client -> tb_nodes : //transfer// Distributed to nodes\n via the state machine.
    tb_nodes -> tb_nodes : Replication via VSR.
    tb_client --> cl_facade : //transfer// create error **(none)**
    cl_facade --> cl_domain : Return result
    cl_domain --> cl_handler : Return result
    cl_handler --> cl_api : Return result
    cl_api --> dfsp : Return result with //**transferId**//
    == Asynchronous Database Activities START ==
    cl_facade -> cl_db : **[ASYNC 1.1]**\n*//transferDuplicateCheck// DB record created.
    cl_facade -> cl_db : **[ASYNC 1.2]**\n*//transfer//, \n*//transferParticipant//, \n*//ilpPacket// \n*//transferStateChange// \n*//transferExtension//\nDatabase records created.
    cl_db <--> cl_facade : **[ASYNC 1.3]**\n Database transaction committed.
    == Asynchronous Database Activities STOP ==

||70||

    == Transfer Fulfilment ==
    dfsp -> cl_api : //JSON// \n(fulfil transfer data with //**transferId**//)
    cl_api -> cl_handler : ///jmeter/transfers/fulfil// \nHandler invoked.
    cl_handler -> cl_domain : Service layer invoked \n//handlePayeeResponse//
    cl_domain -> cl_facade : Domain to facade \n//savePayeeTransferResponse//
    cl_facade -> cl_db : New Database transaction for fulfillment.
    cl_facade -> cl_db : Obtain //**settlementWindowId**// for window where state is **OPEN**.
    cl_facade -> tb_client : //Transfer// posting requested via the CL-TB interface.
    tb_client -> tb_nodes : //transfer// Distributed to nodes\n via the state machine.
    tb_nodes -> tb_nodes : Replication via VSR.
    tb_client --> cl_facade : //transfer// posting create error **(none)**.
    cl_facade -> cl_db : Create \n*//transferFulfilment// \n*//transferStateChange// \nDatabase records.
    cl_facade -> cl_db : Database transaction committed.
    cl_facade --> cl_domain : Return result.
    cl_domain --> cl_handler : Return result.
    cl_handler --> cl_api : Return result.
    cl_api --> dfsp : Return result.
end

@enduml
