feat: product_attribute list with prices
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// JWTClaims represents the JWT claims
|
||||
@@ -436,7 +437,7 @@ func (s *AuthService) RevokeAllRefreshTokens(userID uint) {
|
||||
// GetUserByID retrieves a user by ID
|
||||
func (s *AuthService) GetUserByID(userID uint) (*model.Customer, error) {
|
||||
var user model.Customer
|
||||
if err := s.db.Preload("Role.Permissions").First(&user, userID).Error; err != nil {
|
||||
if err := s.db.Preload("Role.Permissions").Preload(clause.Associations).First(&user, userID).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, responseErrors.ErrUserNotFound
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package productService
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/db"
|
||||
"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 {
|
||||
@@ -29,6 +32,64 @@ func (s *ProductService) GetJSON(p_id_product, p_id_lang, p_id_customer, b2b_id_
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (s *ProductService) Find(id_lang uint, p find.Paging, filters *filters.FiltersList) (find.Found[model.ProductInList], error) {
|
||||
return s.productsRepo.Find(id_lang, p, filters)
|
||||
func (s *ProductService) Find(id_lang uint, p find.Paging, filters *filters.FiltersList, customer *model.Customer, quantity int, shopID uint) (*find.Found[model.ProductInList], error) {
|
||||
if customer == nil || customer.Country == nil {
|
||||
return nil, errors.New("customer is nil or is missing fields")
|
||||
}
|
||||
// customer.ID, customer.CountryID, uint(customer.Country.CurrencyID), quantity, shopID
|
||||
|
||||
found, err := s.productsRepo.Find(id_lang, p, filters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range found.Items {
|
||||
if found.Items[i].VariantsNumber <= 0 {
|
||||
err := s.PopulateProductPrice(&found.Items[i], customer, quantity, shopID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found, err
|
||||
}
|
||||
|
||||
func (s *ProductService) PopulateProductPrice(product *model.ProductInList, targetCustomer *model.Customer, quantity int, shopID uint) error {
|
||||
row := db.Get().Raw(
|
||||
"CALL get_product_price(?, ?, ?, ?, ?)",
|
||||
product.ProductID,
|
||||
shopID,
|
||||
targetCustomer.ID,
|
||||
targetCustomer.CountryID,
|
||||
quantity,
|
||||
).Row()
|
||||
|
||||
var (
|
||||
id uint
|
||||
base float64
|
||||
excl float64
|
||||
incl float64
|
||||
tax float64
|
||||
)
|
||||
|
||||
err := row.Scan(&id, &base, &excl, &incl, &tax)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product.PriceTaxExcl = excl
|
||||
product.PriceTaxIncl = incl
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (s *ProductService) GetProductAttributes(
|
||||
langID uint,
|
||||
productID uint,
|
||||
shopID uint,
|
||||
customerID uint,
|
||||
countryID uint,
|
||||
quantity uint,
|
||||
) ([]view.ProductAttribute, error) {
|
||||
return s.productsRepo.GetProductVariants(langID, productID, shopID, customerID, countryID, quantity)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user