changes with app variant

This commit is contained in:
2026-08-28 13:02:03 +05:30
parent aef105979a
commit c31f3aca27
4 changed files with 165 additions and 15 deletions

View File

@@ -26,7 +26,7 @@ type ProductService interface {
GetLocationProductSummary(tenantID, locationID int) ([]models.ProductSummary, error)
GetSaleTemplate(tenantID, locationID int) (*models.SaleTemplate, error)
FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID int, keyword, productStatus, approve string, pageno, pagesize int) ([]models.Tenantproducts, error)
GetProductByVariant(tenantid, variantid, locationid int) ([]models.Products, error)
GetProductByVariant(tenantid, variantid, locationid, productid int) ([]models.Products, error)
GetProductsBySubcategory(params models.ProductFilter) (map[string]interface{}, error)
UpdateProductLocation(input models.Productlocations) error
CreateProductLocation(input []models.Productlocations) error
@@ -169,19 +169,21 @@ func (s *productService) FetchFilteredProducts(categoryID, subcategoryID, produc
return s.repo.FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID, keyword, productStatus, approve, pageno, pagesize)
}
func (s *productService) GetProductByVariant(tenantid, variantid, locationid int) ([]models.Products, error) {
func (s *productService) GetProductByVariant(tenantid, variantid, locationid, productid int) ([]models.Products, error) {
var data []models.Products
result, err := s.repo.GetProductByVariant(tenantid, variantid, locationid)
result, err := s.repo.GetProductByVariant(tenantid, variantid, locationid, productid)
if err != nil {
return nil, err
}
data = result
return data, nil
// Same rule as the browse list: you can only order what the shop has.
//
// Applied here too so the two screens cannot disagree. Without it a variant
// group would offer every size on the order sheet while the list that led
// there showed only the stocked ones, and a shopper could pick a size the
// branch has none of. Unscoped calls are left alone — see InStockOnly.
return InStockOnly(result, locationid), nil
}

View File

@@ -353,3 +353,97 @@ func TestTheBrowseEndpointOffersOnlyStockedProducts(t *testing.T) {
t.Errorf("offered product %d, want 7086 — the only one Peelamedu has", offered[0].Productid)
}
}
/*
The ordering screen's rule: a product with sizes offers the whole family, a
product without offers itself.
The repository decides that from the product's own `variants` column, so these
tests drive the service and assert on what the app receives.
*/
type variantRepo struct {
fakeProductRepo
gotVariantID int
gotProductID int
rows []models.Products
}
func (v *variantRepo) GetProductByVariant(tenantid, variantid, locationid, productid int) ([]models.Products, error) {
v.gotVariantID = variantid
v.gotProductID = productid
return v.rows, nil
}
// The case that did not work at all: an ungrouped product. Every product for
// Suriya Store is `variants = 0`, so the old signature had no way to name one
// and the order screen received an empty list.
func TestOrderingAnUngroupedProductAsksByProductId(t *testing.T) {
repo := &variantRepo{rows: []models.Products{
{Productid: 7086, Productname: "Cadbury Dairy Milk 100g", Productstock: 25},
}}
svc := NewProductService(repo, &fakeCatalogueService{})
got, err := svc.GetProductByVariant(1135, 0, 1170, 7086)
if err != nil {
t.Fatalf("GetProductByVariant: %v", err)
}
if repo.gotProductID != 7086 {
t.Errorf("productid %d never reached the repository", repo.gotProductID)
}
if len(got) != 1 || got[0].Productid != 7086 {
t.Fatalf("want just the tapped product, got %v", names(got))
}
}
// A product that does belong to a group still returns the whole group, so the
// shopper can pick a size.
func TestOrderingAGroupedProductOffersEveryVariant(t *testing.T) {
repo := &variantRepo{rows: []models.Products{
{Productid: 6995, Productname: "Strawberries 250g", Productstock: 4},
{Productid: 6996, Productname: "Strawberries 500g", Productstock: 9},
}}
svc := NewProductService(repo, &fakeCatalogueService{})
got, err := svc.GetProductByVariant(1087, 44, 1097, 0)
if err != nil {
t.Fatalf("GetProductByVariant: %v", err)
}
if repo.gotVariantID != 44 {
t.Errorf("variantid %d never reached the repository", repo.gotVariantID)
}
if len(got) != 2 {
t.Fatalf("want both sizes offered, got %v", names(got))
}
}
// The order screen must not offer a size the branch has none of — the list that
// led there does not show it, and the two must agree.
func TestAVariantWithNoStockIsNotOfferedForOrder(t *testing.T) {
repo := &variantRepo{rows: []models.Products{
{Productid: 6995, Productname: "Strawberries 250g", Productstock: 0},
{Productid: 6996, Productname: "Strawberries 500g", Productstock: 9},
}}
svc := NewProductService(repo, &fakeCatalogueService{})
got, err := svc.GetProductByVariant(1087, 44, 1097, 0)
if err != nil {
t.Fatalf("GetProductByVariant: %v", err)
}
if len(got) != 1 || got[0].Productid != 6996 {
t.Fatalf("want only the stocked size, got %v", names(got))
}
}
// Oversold is not stock. Strawberries at Ragul Stores really did read -262.
func TestAnOversoldVariantIsNotOfferedForOrder(t *testing.T) {
repo := &variantRepo{rows: []models.Products{
{Productid: 6995, Productname: "Strawberries", Productstock: -262},
}}
svc := NewProductService(repo, &fakeCatalogueService{})
got, _ := svc.GetProductByVariant(1087, 44, 1097, 0)
if len(got) != 0 {
t.Errorf("an oversold product was offered for order")
}
}