Contacting support for the delivery service (Node.js)

In this use case, the user contacts the delivery support against an active or completed delivery. As a response, the user is provided the phone number or email to the customer support or sometimes redirected to a chat window.

Support

Code snippets

Client calls the BAP server to trigger support:

    router.post("/delivery/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": "delivery",
        "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-06-23T08:22:44.059Z"
    },
    "message": {
        "ref_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 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}/delivery/support`, method: "POST", headers, data: supportRequestBody }); 
    }

BAP receives protocol on_support

/*
Example Response JSON:
{
    "context": {
        "domain": "delivery",
        "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-06-23T08:24:26.519Z"
    },
    "message": {
        "phone": "+919898989898",
        "email": "[email protected]",
        "uri": "http://support.bpp.com?order_id=order_id"
    }
}
*/
    // Auth middleware authenticates the digital signature of the incoming request
    router.post("/delivery/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));
        }
    };