package productService import ( "encoding/json" "errors" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/repos/productsRepo" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" "git.ma-al.com/goc_daniel/b2b/app/utils/query/filters" "git.ma-al.com/goc_daniel/b2b/app/utils/query/find" "git.ma-al.com/goc_daniel/b2b/app/view" ) type ProductService struct { productsRepo productsRepo.UIProductsRepo } func New() *ProductService { return &ProductService{ productsRepo: productsRepo.New(), } } func (s *ProductService) Get( p_id_product, p_id_lang, p_id_customer, b2b_id_country, p_quantity uint, ) (*json.RawMessage, error) { product, err := s.productsRepo.GetBase(p_id_product, constdata.SHOP_ID, p_id_lang) if err != nil { return nil, err } price, err := s.productsRepo.GetPrice(p_id_product, nil, constdata.SHOP_ID, p_id_customer, b2b_id_country, p_quantity) if err != nil { return nil, err } variants, err := s.productsRepo.GetVariants(p_id_product, constdata.SHOP_ID, p_id_lang, p_id_customer, b2b_id_country, p_quantity) if err != nil { return nil, err } result := view.ProductFull{ Product: product, Price: price, Variants: variants, } if len(variants) > 0 { result.Variants = variants } jsonBytes, err := json.Marshal(result) if err != nil { return nil, err } raw := json.RawMessage(jsonBytes) return &raw, nil } func (s *ProductService) Find( idLang uint, p find.Paging, filters *filters.FiltersList, customer *model.Customer, quantity uint, shopID uint, ) (*find.Found[model.ProductInList], error) { if customer == nil || customer.Country == nil { return nil, errors.New("customer is nil or missing fields") } found, err := s.productsRepo.Find(idLang, p, filters) if err != nil { return nil, err } // 1. collect simple products (no variants) simpleProductIndexes := make([]int, 0, len(found.Items)) for i := range found.Items { if found.Items[i].VariantsNumber <= 0 { simpleProductIndexes = append(simpleProductIndexes, i) } } // 2. resolve prices ONLY for simple products for _, i := range simpleProductIndexes { price, err := s.productsRepo.GetPrice( found.Items[i].ProductID, nil, shopID, customer.ID, customer.CountryID, quantity, ) if err != nil { return nil, err } found.Items[i].PriceTaxExcl = price.FinalTaxExcl found.Items[i].PriceTaxIncl = price.FinalTaxIncl } return found, nil } func (s *ProductService) GetProductAttributes( langID uint, productID uint, shopID uint, customerID uint, countryID uint, quantity uint, ) ([]view.ProductAttribute, error) { variants, err := s.productsRepo.GetVariants(productID, constdata.SHOP_ID, langID, customerID, countryID, quantity) if err != nil { return nil, err } return variants, nil }