Adding a pharmaceutical item to the cart and receiving an updated quote (Node.js)

In this use case, the user wants to add an item to his cart. This usually results in the pharmacy blocking that item in his inventory for a limited period of time at af fixed price. Sometimes, the pharmacy may also apply a discount on the total cart value. In some other cases, the pharmacy might even add promotional items to the cart at zero price.

Select

Code snippets

Client calls the BAP server to trigger select:

    router.post("/health_pharmacy/add_items", addItems);

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

    async function generateSelectRequest(clientRequestBody) {
/*
Example Request JSON :
{
    "context": {
        "domain": "nic2004:52311",
        "country": "IND",
        "city": "std:080",
        "action": "select",
        "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": {
        "order": {
            "items": [
                {
                    "id": "vit_c",
                    "quantity": {
                        "count": 1
                    }
                },
                {
                    "id": "a_500",
                    "quantity": {
                        "count": 1
                    }
                }
            ]
        }
    }
}
*/
        //The below code generates the above example JSON.
        const transactionId = _.get(body, "transactionId");
        // Returns the Context including MessageId
        const context = createContext(transactionId);
        // Construct Header
        const headers = constructAuthHeader(); // Auth Header
        const selectRequestBody = {
            context,
            message:  {
                // Construct from the request
            }
        };
        //call protocol select
        const response = await callSelect(selectRequestBody);
        res
        .status(200)
        .send({ ...response.data, messageId: context["message_id"] });
    }

BAP server calls protocol select to the network


    async function callSelect(selectRequestBody) {
        // 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/select`, method: "POST", headers, data: selectRequestBody });
    }

BAP receives protocol on_select

/*
Example Response JSON:
{
    "context": {
        "domain": "local_retail",
        "country": "IND",
        "city": "std:080",
        "action": "on_select",
        "core_version": "0.9.2",
        "bap_id": "https://mock_bap.com/",
        "bap_uri": "https://mock_bap.com/beckn/",
        "transaction_id": "12837192379",
        "message_id": "12387193123",
        "timestamp": "2021-03-23T10:00:40.065Z"
    },
    "message": {
        "order": {
            "items": [
                {
                    "id": "vit_c",
                    "price" : {
                        "currency": "INR",
                        "value": "500"
                    },
                    "quantity": {
                        "selected": {
                            "count": 1
                        }
                    }
                },
                {
                    "id": "a_500",
                    "price" : {
                        "currency": "INR",
                        "value": "500"
                    },
                    "quantity": {
                        "selected": {
                            "count": 1
                        }
                    }
                }
            ],
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "1100"
                },
                "breakup": [
                    {
                        "title": "Vitamin C",
                        "price": {
                            "currency": "INR",
                            "value": "500"
                        }
                    }, 
                   {
                        "title": "Antibiotics",
                        "price": {
                            "currency": "INR",
                            "value": "500"
                        }
                    },
                    {
                        "title": "Delivery Charge",
                        "price": {
                            "currency": "INR",
                            "value": "100"
                        }
                    }
                ],
                "ttl": "P1H"
            }
        }
    }
}
*/
    // Auth middleware authenticates the digital signature of the incoming request
    router.post("/health_pharmacy/on_select", auth, onSelect);
    async function onSelect({ body }, res) {
        // Save the response to Database
        await saveToDb(body);
    };

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