Tracking a delivery order (Node.js)

In this use case, the user requests for a live delivery tracking link that can be rendered on the application.

Track

Code snippets

Client calls the BAP server to trigger track:

    router.post("/delivery/track_order", trackOrder);

    async function trackOrder({ body }, res) {
        try {
            //  .. Validate the client request before below function
            await generateTrackRequest(body)
        } catch (error) {
            res.status(500).send(httpResponse("NACK", error));
        }
    };

BAP server generates the protocol request body


    // Code to generate the protocol request body i.e. function generateTrackRequest() specified above

    async function generateTrackRequest(body) {
/*
Example Request JSON :
{
    "context": {
        "domain": "delivery",
        "country": "IND",
        "city": "std:080",
        "action": "track",
        "core_version": "0.9.2",
        "bap_id": "https://mock_bap.com/",
        "bap_uri": "https://mock_bap.com/beckn/",
        "transaction_id": "1239890342",
        "message_id": "123793824",
        "timestamp": "2021-06-23T08:29:27.933Z"
    },
    "message": {
        "order_id": "order_1"
    }
}
*/
        //The below code generates the above example JSON.
        const transactionId = _.get(body, "transactionId");
        // Returns the Context including MessageId
        const context = createContext(transactionId);
        const trackRequestBody = {
            context,
            message:  {
                // Construct from the request
            }
        };
        //call protocol track
        const response = await callTrack(trackRequestBody);
        res
        .status(200)
        .send({ ...response.data, messageId: context["message_id"] });
    }

BAP server calls protocol track to the network

    async function callTrack(trackRequestBody) {
        // It lookups the registry for BG OR BPP
        let uri = lookup();
        // Construct Header
        const headers = constructAuthHeader(); // Auth Header with digital Signature
        return axios({ url: `${uri}/delivery/status`, method: "POST", headers, data: trackRequestBody });
    }

BAP receives protocol on_track

/*
Example Response JSON:
{
    "context": {
        "domain": "delivery",
        "country": "IND",
        "city": "std:080",
        "action": "on_track",
        "core_version": "0.9.2",
        "bap_id": "https://mock_bap.com/",
        "bap_uri": "https://mock_bap.com/beckn/",
        "transaction_id": "1239890342",
        "message_id": "123793824",
        "timestamp": "2021-06-23T08:31:04.045Z"
    },
    "message": {
        "tracking": {
            "url": "https://track.bpp.com?order_id=order_1",
            "status": "active"
        }
    }
}
*/
    // Auth middleware authenticates the digital signature of the incoming request
    router.post("/delivery/on_track", auth, onStatus);
    async function onStatus({ body }, res) {
        // Save the response to Database
        await saveToDb(body);
    };

Client polls BAP to get the on_track results

    // Endpoint for the client to poll the search data based on the message id
    async function getMessageById(req) {
        try {
            const messageId = _.get(req, "messageId");
            // Get the data using message Id
            const response = await getData(messageId);
            res.status(200).send(httpResponse('ACK', "", response));
        } catch(error) {
            res.status(500).send(httpResponse("NACK", error));
        }
    };