api for health score toggle

This commit is contained in:
2026-10-05 12:07:49 +05:30
parent 97f277f424
commit fc2caffcd1
7 changed files with 358 additions and 1 deletions

View File

@@ -24,6 +24,8 @@ type ProductRepository interface {
GetProductStocks(tenantID, locationID string) ([]models.Productstocks, error)
CreateProductStock(stocks []models.Productstock) error
UpdateProductStatus(productIDs []int, status string) error
// SetShowHealthScore turns one product's health score on or off for one shop.
SetShowHealthScore(tenantID, productID int, show bool) error
SyncProductLocationStatus(refs []models.ProductLocationRef) error
EnsureProductLocation(refs []models.ProductLocationRef) error
UpdateProduct(product models.Products) error
@@ -1725,3 +1727,30 @@ func (r *productRepository) UpdateProductPricing(productid int, retailprice, pro
"taxpercent": taxpercent,
}).Error
}
// SetShowHealthScore turns one product's health score on or off for one shop.
//
// `Update` with a single column, deliberately, and not `Updates` with a struct.
// GORM's struct update SKIPS zero values, so `showhealthscore: false` would be
// silently dropped — the flag could be switched on and never off again, which is
// the exact failure a merchant would report as "it does not save".
//
// Scoped by tenant as well as product. `middleware.WebAuth` already refuses a
// request naming a tenant the session does not own, so this is the second lock
// rather than the first — but a write that changes what a shopper sees should
// not rest on one check being correctly mounted.
func (r *productRepository) SetShowHealthScore(tenantID, productID int, show bool) error {
result := r.db.Table("products").
Where("productid = ? AND tenantid = ?", productID, tenantID).
Update("showhealthscore", show)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
// Either no such product, or one belonging to another business. Both
// are the same answer to the caller, and neither should look like it
// worked.
return fmt.Errorf("product %d was not found for this business", productID)
}
return nil
}