---
# System prepended metadata

title: JioMart Buy-Again Widget

---

# JioMart Buy-Again Widget

[https://rilcloud.sharepoint.com/:b:/r/sites/JMTelOS/Shared%20Documents/Product%20Docs/Personalization%20Workstreams.pptx.pdf?csf=1&web=1&e=hA1BhW](https://)

## BuyAgain API

> This API will not be opened to internet. It will be consumed by jio mart api

* **End point URL** : https://<host-name>/buy_again?rr_id=<customer_rr_id>&l1_category=<l1_category_id>
* **Method** : GET
* **Request Body** : None
* **Request Headers** : None
* **Authentication None** : None
* **Response** :
    
```json
{
  "result": "success",
  "data": {
    [
     {
        "sku" : "HADF2344358",
        "score" : 0.651
     },
     {
         "sku" : "WBDF1394352",
         "score" : 0.643
     }
    ]
  }
}
```
* **Error Code** : 404 if either rr_id no present
* **Optional Parameter** : Category is optional, if not present it will be assumed that it is for home page

### Notes for Buy Again cold start
1. For first hit (before first order) for rr_id x category id : Nothing will return
2. For second hit (after first order) for rr_id x category id : last order product will be returned with score 1
3. For third hit (after second order) for rr_id x category id : last two order product will be returned with score 1
4. From third onwards score will be calculated and return.
5. Limit number of products to 20
    
## Knowledge Onboarding

### Schema

1. Product
2. Category
3. Customer
4. Brand

#### Lower environment data link:
Priti - upload to sharepoint and put link here.


## Transaction Events

### 1. Order Completed Event

Schema

Payload for Order Completed 
```
{
  "external_ids": [
    {
      "type": "rr_id",
      "id": "9aee539a-c22b-48b3-a069-f573e5c0ae2e"
    }
  ],
  "application_ids": [
    {
      "type": "jiomart_id",
      "id": "1615343"
    }
  ],
  "type": "track",
  "event": "Order Completed",
  "properties": {
    "order_id": "3j2h3j4gh-2323b2b3",
    "cart_total": 353,
    "items": 3,
    "shipping": 10,
    "tax": 0,
    "order_total": 363,
    "currency": "INR",
    "products": [
      {
        "product_id": "52efg-7f",
        "sku": "AMM-500g",
        "product_name": "Amul Mithai Mate 500 gm",
        "l1_category": "Groceries",
        "l2_category": "Dairy & Bakery",
        "l3_category": "Dairy",
        "brand": "Amul",
        "variant": "500 gm",
        "quantity": 2,
        "price": 116,
        "value": 232,
        "currency": "INR",
        "position": 1,
        "url": "https://www.jiomart.com/p/groceries/nestle-milkmaid-sweetened-condensed-milk-400-g-tin/490001537",
        "img_url": "https://www.jiomart.com/images/product/original/490010311/amul-taaza-homogenised-toned-milk-1-l-tetra-pak-product-images-o490010311-p490010311-0-202203152257.jpg?im=Resize=(420,420)"
      },
      {
        "product_id": "52efg-7g",
        "sku": "NMM-500g",
        "product_name": "Nestle Milkmaid 500 gm",
        "l1_category": "Groceries",
        "l2_category": "Dairy & Bakery",
        "l3_category": "Dairy",
        "brand": "Nestle",
        "variant": "500 gm",
        "quantity": 1,
        "price": 121,
        "value": 121,
        "currency": "INR",
        "position": 2,
        "url": "https://www.jiomart.com/p/groceries/nestle-milkmaid-sweetened-condensed-milk-400-g-tin/490001537",
        "img_url": "https://www.jiomart.com/images/product/original/490010311/amul-taaza-homogenised-toned-milk-1-l-tetra-pak-product-images-o490010311-p490010311-0-202203152257.jpg?im=Resize=(420,420)"
      }
    ]
  }
}
```

### 2. Product Added Event

Schema -

### 3. Product Clicked Event

Schema -


## TelOS DAG

### 1. Order Completed Event DAG -

1. Diagram:
![WhatsApp Image 2024-01-30 at 13.33.27](https://hackmd.io/_uploads/rJfwQ289T.jpg)

2. Each lambda function schema:

3. Each Delta function schema:

3. Features 
    Property Features - 
        
        /retail/feature/customer_sku/order_total_count/daily
        
        /retail/feature/customer_sku/last_purchase_date/daily
            𝜏_𝑛𝑒𝑤(𝑐,𝑝) = latest_order_date

    Derived Features -
            
        /retail/feature/customer_sku/last_purchase_lag/daily
            ℓ_𝑛𝑒𝑤(𝑐,𝑝) = 𝜏_𝑛𝑒𝑤(𝑐,𝑝) − 𝜏_𝑜𝑙𝑑(𝑐,𝑝)
            
        /retail/feature/customer_sku/lag_order_count/daily
            𝑛_𝑛𝑒𝑤(𝑐,𝑝) = 𝑛_𝑜𝑙𝑑(𝑐,𝑝) + 1
            
        /retail/feature/customer_sku/order_mean/daily
            𝜇_𝑛𝑒𝑤(𝑐,𝑝) = ((𝜇_𝑜𝑙𝑑(𝑐,𝑝) × 𝑛_𝑜𝑙𝑑(𝑐,𝑝)) + ℓ_𝑛𝑒𝑤(𝑐,𝑝)) / (𝑛_𝑛𝑒𝑤 (𝑐,𝑝) )
            
        /retail/feature/customer_sku/order_variance/daily
            v_𝑛𝑒𝑤(𝑐,𝑝) = (([v_𝑜𝑙𝑑(𝑐,𝑝) + (𝜇_𝑜𝑙𝑑(𝑐,𝑝) − 𝜇_𝑛𝑒𝑤(𝑐,𝑝))^2 ] × 𝑛_𝑜𝑙𝑑(𝑐,𝑝)) + (ℓ_𝑛𝑒𝑤(𝑐,𝑝) − 𝜇_𝑛𝑒𝑤(𝑐,𝑝))^2) / (𝑛_𝑛𝑒𝑤(𝑐,𝑝))
 
    

4. Inferences
    Heuristic Inference -
        
        Scheduled daily -
            /retail/inference/customer_sku/buy_again_score
                𝑦_𝑡(𝑝,𝑐) = 1 / (1 + exp⁡(−(ℓ(𝑐,𝑝)−𝜇(𝑝,𝑐)+√(𝑣(𝑐,𝑝)))))
            /retail/inference/customer_sku/buy_again_last_updated - ???
        
6. Decisions


# API Contract

## Knowladge API
    Product
        Create
        Update
        Delete
    Category
        Create
        Update
        Delete
    ProductCategoryMapping
        Create
        Update
        Delete
    
    

## Output API

**1. BuyAgain API**

    Introduction -
        This API is used for fetching product list with their score
        for customer to show them in BuyAgain widget. 

    Base URL -
        https://api.jiomart.com/telos/

    Endpoints -
        Buy-again
            egress/buy-again?customer_id=<rr-id>&l1_catogery=<id>
    
    Description -
        Parameter - l1_catogery is optional. 
            If l1_catogery is not present product list accross categories will be returned.
            If l1_catogery is present product list for the given categorie will be returned.

    HTTP Method - GET

    Request -
        Headers -
            Content-Type: application/json         
        Body -
            NA
    Response -
        Success (HTTP Status Code: 200)
            {
              "result": "success",
              "data": {
                [
                 {
                    "sku" : "HADF2344358",
                    "score" : 0.651
                 },
                 {
                     "sku" : "WBDF1394352",
                     "score" : 0.643
                 }
                ]
              }
            }
            
        Error (HTTP Status Code: 404)
            {
              "result": "error",
              "error": {
                  "code": "SKU_NOT_FOUND"
              }
            }
        Error (HTTP Status Code: 500)
            {
              "result": "error",
              "error": {
                  "code": "INTERNAL_SERVER_ERROR"
              }
            }

#### Open Questions:
Reduce - How to close the window if order is coming next day morning?
    Publish dummy message for all windowTyps on all running instance of all reduce

Data Resolver - Add validation if all required features and inference for the timestamp given

Change in HeuristicSchema for dependendent feature schema - define TimeCriteriaInstanceSchema and add validation in dataresolver

TBD - Shrimant
API - we need api gateway (Ask shrimant)
        In front of api gateway - loadbalancer/internet gateway




