Files
backend_fiesta/services/cataloguePrice.go
2026-09-03 11:21:12 +05:30

52 lines
2.0 KiB
Go

package services
import (
"regexp"
"strconv"
"strings"
)
// The catalogue quotes a RANGE, as a string: "₹9-11", "₹75-95", sometimes a
// single "₹22". It is what a shopper would expect to pay across the shops the
// catalogue was built from, not a price any particular shop has set.
var cataloguePriceFigure = regexp.MustCompile(`[0-9]+(?:\.[0-9]+)?`)
// SuggestedPriceFrom reads a starting price out of a catalogue price range.
//
// ── Why import needs this at all ────────────────────────────────────────────
//
// Import deliberately wrote no price: "import is not pricing", the merchant sets
// it. That is right about who decides and wrong about what a shopper sees in the
// meantime — the product goes live at ₹0, which is not "unpriced" on a phone, it
// reads as free. Products uploaded by sheet carry their price and show it; the
// same product imported from the catalogue showed nothing.
//
// ── Which end of the range ──────────────────────────────────────────────────
//
// The LOW end. It is a suggestion the merchant will overwrite, and the two ways
// to be wrong are not equal: too low is a margin the shop notices and corrects,
// too high is a shopper who quietly decides the shop is expensive. The low end
// is also the number a catalogue range is usually anchored on.
//
// Returns 0 when there is no figure to read — 159 of the 672 catalogue rows
// measured on 2026-09-03 carry no range at all, and inventing a price for those
// would be worse than showing none.
func SuggestedPriceFrom(priceRange string) float64 {
found := cataloguePriceFigure.FindAllString(strings.TrimSpace(priceRange), -1)
if len(found) == 0 {
return 0
}
low := 0.0
for _, raw := range found {
value, err := strconv.ParseFloat(raw, 64)
if err != nil || value <= 0 {
continue
}
if low == 0 || value < low {
low = value
}
}
return low
}