Merge branch 'main' of ssh://git.ma-al.com:8822/goc_daniel/b2b into order-actions
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package orderService
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/actions/orderStatusActions"
|
||||
@@ -10,8 +9,11 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/repos/cartsRepo"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/repos/ordersRepo"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/repos/productsRepo"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/addressesService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/emailService"
|
||||
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||
"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/utils/responseErrors"
|
||||
@@ -20,6 +22,7 @@ import (
|
||||
type OrderService struct {
|
||||
ordersRepo ordersRepo.UIOrdersRepo
|
||||
cartsRepo cartsRepo.UICartsRepo
|
||||
productsRepo productsRepo.UIProductsRepo
|
||||
addressesService *addressesService.AddressesService
|
||||
emailService *emailService.EmailService
|
||||
actionRegistry *orderStatusActions.ActionRegistry
|
||||
@@ -29,6 +32,7 @@ func New() *OrderService {
|
||||
return &OrderService{
|
||||
ordersRepo: ordersRepo.New(),
|
||||
cartsRepo: cartsRepo.New(),
|
||||
productsRepo: productsRepo.New(),
|
||||
addressesService: addressesService.New(),
|
||||
emailService: emailService.NewEmailService(),
|
||||
actionRegistry: &orderStatusActions.GlobalRegistry,
|
||||
@@ -62,9 +66,12 @@ func (s *OrderService) Find(user *model.Customer, p find.Paging, filt *filters.F
|
||||
|
||||
for i := 0; i < len(list.Items); i++ {
|
||||
address_unparsed, err := s.addressesService.ValidateAddressJson(list.Items[i].AddressString, list.Items[i].CountryID)
|
||||
// log such errors
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
logger.Warn("failed to validate address",
|
||||
"service", "orderService",
|
||||
"order_id", list.Items[i].OrderID,
|
||||
"error", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
list.Items[i].AddressUnparsed = &address_unparsed
|
||||
@@ -99,8 +106,10 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
||||
name = *cart.Name
|
||||
}
|
||||
|
||||
base_price, tax_incl, tax_excl, err := s.getOrderTotalPrice(user_id, cart_id, country_id)
|
||||
|
||||
// all checks passed
|
||||
order, err := s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, originalUserId)
|
||||
order, err := s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, originalUserId, base_price, tax_incl, tax_excl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -109,8 +118,12 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
||||
// if no error is returned, remove the cart. This should be smooth
|
||||
err = s.cartsRepo.RemoveCart(user_id, cart_id)
|
||||
if err != nil {
|
||||
// Log error but don't fail placing order
|
||||
_ = err
|
||||
logger.Warn("failed to remove cart after order placement",
|
||||
"service", "orderService",
|
||||
"user_id", user_id,
|
||||
"cart_id", cart_id,
|
||||
"error", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
return s.ChangeOrderStatus(user_id, order.OrderID, enums.OrderStatusPending)
|
||||
@@ -167,3 +180,27 @@ func (s *OrderService) ChangeOrderStatus(userId, orderId uint, newStatus enums.O
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *OrderService) getOrderTotalPrice(user_id uint, cart_id uint, country_id uint) (float64, float64, float64, error) {
|
||||
cart, err := s.cartsRepo.RetrieveCart(user_id, cart_id)
|
||||
if err != nil {
|
||||
return 0.0, 0.0, 0.0, err
|
||||
}
|
||||
|
||||
base_price := 0.0
|
||||
tax_incl := 0.0
|
||||
tax_excl := 0.0
|
||||
|
||||
for _, product := range cart.Products {
|
||||
prices, err := s.productsRepo.GetPrice(product.ProductID, product.ProductAttributeID, constdata.SHOP_ID, user_id, country_id, product.Amount)
|
||||
if err != nil {
|
||||
return 0.0, 0.0, 0.0, err
|
||||
}
|
||||
|
||||
base_price += prices.Base
|
||||
tax_incl += prices.FinalTaxIncl
|
||||
tax_excl += prices.FinalTaxExcl
|
||||
}
|
||||
|
||||
return base_price, tax_incl, tax_excl, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user