Contacting delivery support for an order (Node.js)

In this use case, the user wants to contact the customer support due to an issue with the delivery of an order. Here the pharmacy returns the support info of the delivery service provider.

Support

Code snippets

Client calls the BAP server to trigger support:

    router.post("/health_pharmacy/contact_delivery", contactDelivery);

    async function contactDelivery({ body }, res) {
        try {
            //  .. Validate the client request before below function
            await generateSupportRequest(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 generateSupportRequest() specified above

    async function generateSupportRequest(body) {
/*
Example Request JSON :
{
    "context": {
        "domain": "nic2004:52311",
        "country": "IND",
        "city": "std:080",
        "action": "support",
        "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-03-23T10:00:40.065Z"
    },
    "message": {
        "ref_id": "fulfillment_1"
    }
}
*/
        //The below code generates the above example JSON.
        const transactionId = _.get(body, "transactionId");
        // Returns the Context including MessageId
        const context = createContext(transactionId);
        const supportRequestBody = {
            context,
            message:  {
                // Construct from the request
            }
        };
        //call protocol support
        const response = await callSupport(supportRequestBody);
        res
        .status(200)
        .send({ ...response.data, messageId: context["message_id"] });
    }

BAP server calls protocol support to the network

    async function callSupport(supportRequestBody) {
        // 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}/health_pharmacy/support`, method: "POST", headers, data: supportRequestBody }); 
    }

BAP receives protocol on_support

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:52311",
        "country": "IND",
        "city": "std:080",
        "action": "on_support",
        "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-03-23T10:00:40.065Z"
    },
    "message": {
        "phone": "+919898989898",
        "email": "[email protected]",
        "uri": "http://support.bpp.com?fulfillment_id=1"
    }
}
*/
    // Auth middleware authenticates the digital signature of the incoming request
    router.post("/health_pharmacy/on_support", auth, onSupport);
    async function onSupport({ body }, res) {
        // Save the response to Database
        await saveToDb(body);
    };

Client polls BAP to get the on_support 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));
        }
    };