Document Purpose: Technical & functional proposal for team review and approval before implementation.
Scope: Automated B2C pincode pricing derived from dealer inventory prices (±20pincode range).
System Risk Level: Low (zero changes to existing product/pincode tables, zero customer-frontend changes, instant rollback toggle).
Today, every product variant in CKart has two layers of pricing entered manually by the Admin/Brand team:
product_variants (price, b2bPrice, mrp).product_pincodes (amount, b2bPrice, mrp).When onboarding or collaborating with dealers in an area, dealers quote different prices for the same products. Currently, the operations team manually collects these prices, calculates the average offline, and overwrites the product/pincode prices in the database.
We want to automate this end-to-end:
dealer_inventories).P, automatically look at all qualifying dealers located within [P - 20, P + 20] and calculate the average of their entered prices for each product variant.P.[P - 20, P + 20] has entered a price for a product, seamlessly fall back to CKart's prior admin-entered price (product_pincodes first, then product_variants).Pricing touches product listing, search, product details, best-prices, cart totals, taxes, coupons, and order checkout. The design must:
| # | Rule / Area | Agreed Behavior |
|---|---|---|
| 1 | Customer Segment | B2C price only. B2B pricing (b2bPrice and /b2b/* endpoints) remains 100% untouched. |
| 2 | Pincode Range | ±20 numeric pincode range ([customerPincode - 20, customerPincode + 20]). Configurable via constant/env. (Existing ±10 stock/delivery radius remains untouched.) |
| 3 | Stock Dependency | Independent of stock. Once a dealer adds a product with a price in active inventory (status = true), their price counts even if currentStock = 0. |
| 4 | Dealer Eligibility | Only dealers with dealers.status = 'Approved', active warehouse address (dealer_addresses.status = 1), active inventory row (dealer_inventories.status = true), and a valid entered price (price IS NOT NULL AND price > 0). |
| 5 | CKart's Own Dealer | Included for now (ckartDealer = true counts in the average), controlled by a single config flag (INCLUDE_CKART_DEALER = true) so it can be toggled anytime without rewriting code. |
| 6 | Warehouses per Dealer | Currently 1 warehouse per dealer. |
| 7 | Price Guardrails | Enforced on dealer input (blocks accidental typos / unrealistic outliers before saving). |
| 8 | MRP & Tax | mrp continues to come from product_pincodes / product_variants. Dealer-entered price is GST-inclusive (matching existing product_variants.price and product_pincodes.amount). |
product_pincodesAt first glance, overwriting product_pincodes whenever a dealer saves a price might look simpler. However, auditing the codebase revealed 4 critical risks with that approach:
P affects every pincode in [P - 20, P + 20] (41 pincodes per variant per dealer). Materializing those rows would bloat product_pincodes massively with auto-generated and non-existent pincodes.src/services/admin/productService.js (line 708) and src/services/brand/productService.js (line 583) load all ProductPincode rows into the product edit form. When an admin clicks Save, the backend runs ProductPincode.destroy({ where: { productVariantId } }) and recreates only what the form submitted — which would flood the admin UI and wipe out dealer averages.GET /shared/product/prices: src/services/common/productService.js (line 784) currently queries a pincode range (±10). Inserting 41 synthetic rows per dealer would cause duplicate entries per variant to be returned.Conclusion: Leave product_variants and product_pincodes 100% untouched as CKart's safe admin baseline, store dealer prices only on dealer_inventories, and compute the effective B2C price at read time through one central resolver service.
Think of customer B2C pricing as 3 simple layers checked top-to-bottom:
src/services/common/pincodePriceResolver.js) owns the entire rule. Changing ±20, toggling INCLUDE_CKART_DEALER, or turning off the feature happens in one place.product_pincodes and product_variants are never overwritten.A key concern is avoiding any new response field or object (such as dealer_avg_prices) that would force changes across mobile and web frontends.
With this plan, ZERO new keys are added to any customer API response. The backend injects the averaged price in-memory directly into the exact fields (amount and price) that frontends already read today.
GET /shared/product/prices?pincode=110050Before (Current API Response):
[
{
"productVariantId": 101,
"pincode": "110050",
"amount": "410.00",
"mrp": "480.00",
"b2bPrice": "395.00"
}
]
After (When Dealer Average = ₹400.00):
[
{
"productVariantId": 101,
"pincode": "110050",
"amount": "400.00",
"mrp": "480.00",
"b2bPrice": "395.00"
}
]
product_pincodes row for variant 101 at 110050, if dealers within 110030–110070 entered prices, this API returns an entry in the exact same format ({ productVariantId: 101, pincode: "110050", amount: "400.00", mrp: "480.00", b2bPrice: null }), so the frontend's existing lookup automatically displays ₹400.00.b2bPrice and mrp remain completely untouched.| Application / Panel | Changes Required? | Notes |
|---|---|---|
| Customer Mobile App (current & older versions) | None (0 changes) | Continues reading amount / price as today |
| Customer Web App | None (0 changes) | Continues reading amount / price as today |
B2B App / Portal (/b2b/*) |
None (0 changes) | Completely untouched |
| Admin Panel & Brand Panel | None (0 changes) | product_variants & product_pincodes CRUD untouched |
| Dealer App | UI Input Only | Add "Price" input on Add/Edit Inventory screen (older Dealer App builds won't break because price is optional) |
Assume one product in the database:
productVariantId = 101)product_variants): price = ₹420, mrp = ₹480product_pincodes): For pincode 110050 → amount = ₹410, mrp = ₹480, b2bPrice = ₹395Suppose the following dealers have this product in dealer_inventories:
| Dealer | Warehouse Pincode | Dealer Status | Stock | Entered Price | Eligible? |
|---|---|---|---|---|---|
| Dealer A | 110045 |
Approved | 500 | ₹390 |
Yes |
| Dealer B | 110060 |
Approved | 0 | ₹410 |
Yes (counts regardless of stock) |
| Dealer C (CKart) | 110052 |
Approved | 200 | ₹400 |
Yes (INCLUDE_CKART_DEALER = true) |
| Dealer D | 110055 |
Approved | 100 | (NULL / blank) | No (no price entered) |
| Dealer E | 110050 |
Blocked | 100 | ₹350 |
No (dealer not Approved) |
| Dealer F | 110090 |
Approved | 300 | ₹430 |
Only if within customer's ±20 |
B2C Customer at Pincode 110050
110030 to 110070 (110050 ± 20).₹390), Dealer B (₹410), Dealer C (₹400).
(Dealer D has no price; Dealer E is Blocked; Dealer F at 110090 is outside range.)(390 + 410 + 400) / 3 = ₹400.00 (MRP stays ₹480)./shared/product/prices, /shared/product/best-prices, Cart, and Checkout.B2C Customer at Pincode 110075
110055 to 110095 (110075 ± 20).₹410 at 110060) and Dealer F (₹430 at 110090).(410 + 430) / 2 = ₹420.00.B2C Customer at Pincode 400001 (No dealers nearby)
399981 to 400021 → 0 dealers found.product_pincodes for 400001 → none found.product_variants.price = ₹420.00.All Dealers Near 110050 Remove the Product (or Clear Their Price)
110030–110070 → 0 dealers with price.product_pincodes for 110050 → finds original Admin entry.₹410.00 (zero manual cleanup needed).Dealer Enters a Mistake / Outlier Price
₹40 (forgot a zero) or ₹550 (above MRP ₹480).B2B Customer at Pincode 110050
b2bPrice = ₹395.00 from product_pincodes / product_variants.Create a Sequelize migration in src/migrations/:
dealer_inventories table:price: DECIMAL(10, 2), allowNull: true, defaultValue: null.src/models/dealerInventoryModel.js to declare price.dealer_addresses table:idx_dealer_addresses_pincode_status on (pincode, status) for fast ±20 range lookups.product_variants & product_pincodes tables:Files to update:
src/services/dealer/inventorySerivce.jssrc/controllers/dealer/inventoryController.jssrc/routes/dealerRoutes/dealerInventoryRoutes.jssrc/validations/dealer/index.jsChanges:
POST /dealer/inventory/addProductToInventoryprice in request body.price against the guardrail (if provided).price on DealerInventory.POST /dealer/inventory/updateProductStockInInventoryprice alongside currentStock so the Dealer App can update both in one call if desired.POST /dealer/inventory/updateProductPriceInInventory (New Endpoint)null) the dealer's price for a productVariantId independently of stock.getInventoryByDealerId, getInventoryByProductVariantId)price in returned DealerInventory rows.ProductVariant (price, mrp) before saving DealerInventory.price.price must be a valid number > 0.price <= variant.mrp (cannot exceed MRP).price >= variant.price * MIN_PRICE_RATIO such as 0.5 or 0.7 of admin base price).cache:productPrices:*cache:dealerInventory:*cache:productList:*, cache:userProductList:*, cache:productDetail:* (if WebStore is included)src/services/common/pincodePriceResolver.js)Create a new service with 3 configuration toggles (readable from .env with safe defaults):
const ENABLE_DEALER_AVG_PRICING = process.env.ENABLE_DEALER_AVG_PRICING !== "false"; // default: true
const DEALER_PRICE_PINCODE_RANGE = parseInt(process.env.DEALER_PRICE_PINCODE_RANGE || "20", 10);
const INCLUDE_CKART_DEALER = process.env.INCLUDE_CKART_DEALER !== "false"; // default: true
SELECT
di.productVariantId,
ROUND(AVG(di.price), 2) AS avgPrice
FROM dealer_inventories di
INNER JOIN dealer_addresses da ON da.id = di.dealerAddressId
INNER JOIN dealers d ON d.id = di.dealerId
WHERE di.status = true
AND di.price IS NOT NULL
AND di.price > 0
AND da.status = 1
AND da.pincode REGEXP '^[0-9]{6}