diff --git a/app/delivery/web/api/restricted/orders.go b/app/delivery/web/api/restricted/orders.go index 186599c..652d9d2 100644 --- a/app/delivery/web/api/restricted/orders.go +++ b/app/delivery/web/api/restricted/orders.go @@ -66,6 +66,11 @@ var columnMappingListOrders map[string]string = map[string]string{ "name": "b2b_customer_orders.name", "country_id": "b2b_customer_orders.country_id", "status": "b2b_customer_orders.status", + "base_price": "b2b_customer_orders.base_price", + "tax_incl": "b2b_customer_orders.tax_incl", + "tax_excl": "b2b_customer_orders.tax_excl", + "created_at": "b2b_customer_orders.created_at", + "updated_at": "b2b_customer_orders.updated_at", } func (h *OrdersHandler) PlaceNewOrder(c fiber.Ctx) error { diff --git a/app/model/order.go b/app/model/order.go index a6556b2..93fcd96 100644 --- a/app/model/order.go +++ b/app/model/order.go @@ -1,5 +1,7 @@ package model +import "time" + type CustomerOrder struct { OrderID uint `gorm:"column:order_id;primaryKey;autoIncrement" json:"order_id"` UserID uint `gorm:"column:user_id;not null;index" json:"user_id"` @@ -8,6 +10,11 @@ type CustomerOrder struct { AddressString string `gorm:"column:address_string;not null" json:"address_string"` AddressUnparsed *AddressUnparsed `gorm:"-" json:"address_unparsed"` Status string `gorm:"column:status;size:50;not null" json:"status"` + BasePrice float64 `gorm:"column:base_price;type:decimal(10,2);not null" json:"base_price"` + TaxIncl float64 `gorm:"column:tax_incl;type:decimal(10,2);not null" json:"tax_incl"` + TaxExcl float64 `gorm:"column:tax_excl;type:decimal(10,2);not null" json:"tax_excl"` + CreatedAt time.Time `gorm:"column:created_at;not null" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at;not null" json:"updated_at"` Products []OrderProduct `gorm:"foreignKey:OrderID;references:OrderID" json:"products"` } diff --git a/app/repos/ordersRepo/ordersRepo.go b/app/repos/ordersRepo/ordersRepo.go index c3b8158..97569e0 100644 --- a/app/repos/ordersRepo/ordersRepo.go +++ b/app/repos/ordersRepo/ordersRepo.go @@ -1,6 +1,8 @@ package ordersRepo import ( + "time" + "git.ma-al.com/goc_daniel/b2b/app/db" "git.ma-al.com/goc_daniel/b2b/app/model" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" @@ -11,7 +13,7 @@ import ( type UIOrdersRepo interface { UserHasOrder(user_id uint, order_id uint) (bool, error) Find(user_id uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.CustomerOrder], error) - PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string) error + PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string, base_price float64, tax_incl float64, tax_excl float64) error ChangeOrderAddress(order_id uint, country_id uint, address_info string) error ChangeOrderStatus(order_id uint, status string) error } @@ -69,7 +71,7 @@ func (repo *OrdersRepo) Find(user_id uint, p find.Paging, filt *filters.FiltersL }, nil } -func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string) error { +func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string, base_price float64, tax_incl float64, tax_excl float64) error { order := model.CustomerOrder{ UserID: cart.UserID, Name: name, @@ -87,6 +89,12 @@ func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, cou }) } + order.CreatedAt = time.Now() + order.UpdatedAt = time.Now() + order.BasePrice = base_price + order.TaxIncl = tax_incl + order.TaxExcl = tax_excl + return db.DB.Create(&order).Error } @@ -97,6 +105,7 @@ func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, addre Updates(map[string]interface{}{ "country_id": country_id, "address_string": address_info, + "updated_at": time.Now(), }). Error } @@ -105,6 +114,9 @@ func (repo *OrdersRepo) ChangeOrderStatus(order_id uint, status string) error { return db.DB. Table("b2b_customer_orders"). Where("order_id = ?", order_id). - Update("status", status). + Updates(map[string]interface{}{ + "status": status, + "updated_at": time.Now(), + }). Error } diff --git a/app/service/authService/auth.go b/app/service/authService/auth.go index 8f8c63a..af971d5 100644 --- a/app/service/authService/auth.go +++ b/app/service/authService/auth.go @@ -99,6 +99,7 @@ func (s *AuthService) Login(req *model.LoginRequest) (*model.AuthResponse, strin user.LangID = *req.LangID } + user.Country = nil s.db.Save(&user) // Generate access token (JWT) @@ -210,6 +211,7 @@ func (s *AuthService) CompleteRegistration(req *model.CompleteRegistrationReques user.EmailVerificationToken = "" user.EmailVerificationExpires = nil + user.Country = nil if err := s.db.Save(&user).Error; err != nil { return nil, "", fmt.Errorf("failed to update user: %w", err) } @@ -278,6 +280,7 @@ func (s *AuthService) RequestPasswordReset(emailAddr string) error { user.PasswordResetToken = token user.PasswordResetExpires = &expiresAt user.LastPasswordResetRequest = &now + user.Country = nil if err := s.db.Save(&user).Error; err != nil { return fmt.Errorf("failed to save reset token: %w", err) } @@ -328,6 +331,7 @@ func (s *AuthService) ResetPassword(token, newPassword string) error { user.PasswordResetToken = "" user.PasswordResetExpires = nil + user.Country = nil if err := s.db.Save(&user).Error; err != nil { return fmt.Errorf("failed to update password: %w", err) } @@ -539,6 +543,7 @@ func (s *AuthService) UpdateJWTToken(user *model.Customer) (string, error) { } // Save the updated user + user.Country = nil if err := s.db.Save(user).Error; err != nil { return "", fmt.Errorf("database error: %w", err) } diff --git a/app/service/authService/google_oauth.go b/app/service/authService/google_oauth.go index d517c6d..9bcd5ce 100644 --- a/app/service/authService/google_oauth.go +++ b/app/service/authService/google_oauth.go @@ -83,6 +83,7 @@ func (s *AuthService) HandleGoogleCallback(code string) (*model.AuthResponse, st // Update last login now := time.Now() user.LastLoginAt = &now + user.Country = nil s.db.Save(user) // Generate access token (JWT) diff --git a/app/service/orderService/orderService.go b/app/service/orderService/orderService.go index 5e1a5ca..a78b17e 100644 --- a/app/service/orderService/orderService.go +++ b/app/service/orderService/orderService.go @@ -8,8 +8,10 @@ import ( "git.ma-al.com/goc_daniel/b2b/app/model" "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/query/filters" "git.ma-al.com/goc_daniel/b2b/app/utils/query/find" "git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors" @@ -18,6 +20,7 @@ import ( type OrderService struct { ordersRepo ordersRepo.UIOrdersRepo cartsRepo cartsRepo.UICartsRepo + productsRepo productsRepo.UIProductsRepo addressesService *addressesService.AddressesService emailService *emailService.EmailService } @@ -26,6 +29,7 @@ func New() *OrderService { return &OrderService{ ordersRepo: ordersRepo.New(), cartsRepo: cartsRepo.New(), + productsRepo: productsRepo.New(), addressesService: addressesService.New(), emailService: emailService.NewEmailService(), } @@ -82,8 +86,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 - err = s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info) + err = s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, base_price, tax_incl, tax_excl) if err != nil { return err } @@ -143,3 +149,27 @@ func (s *OrderService) ChangeOrderStatus(user *model.Customer, order_id uint, st return s.ordersRepo.ChangeOrderStatus(order_id, status) } + +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 +} diff --git a/bruno/b2b_daniel/auth/update-choice.yml b/bruno/b2b_daniel/auth/update-choice.yml index 0a511b0..ba9b348 100644 --- a/bruno/b2b_daniel/auth/update-choice.yml +++ b/bruno/b2b_daniel/auth/update-choice.yml @@ -5,15 +5,14 @@ info: http: method: POST - url: http://localhost:3000/api/v1/public/auth/update-choice?lang_id=0&country_id=1 + url: http://localhost:3000/api/v1/public/auth/update-choice?lang_id=1&country_id=1 params: - name: lang_id - value: "0" + value: "1" type: query - name: country_id value: "1" type: query - auth: inherit settings: encodeUrl: true diff --git a/bruno/b2b_daniel/carts/add-product-to-cart.yml b/bruno/b2b_daniel/carts/add-product-to-cart.yml index 9337ab5..d2ac0f2 100644 --- a/bruno/b2b_daniel/carts/add-product-to-cart.yml +++ b/bruno/b2b_daniel/carts/add-product-to-cart.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: http://localhost:3000/api/v1/restricted/carts/add-product-to-cart?cart_id=1&product_id=51&product_attribute_id=1115&amount=1&set_amount=true + url: http://localhost:3000/api/v1/restricted/carts/add-product-to-cart?cart_id=1&product_id=51&product_attribute_id=1115&amount=2&set_amount=true params: - name: cart_id value: "1" @@ -17,7 +17,7 @@ http: value: "1115" type: query - name: amount - value: "1" + value: "2" type: query - name: set_amount value: "true" diff --git a/i18n/migrations/20260302163122_create_tables.sql b/i18n/migrations/20260302163122_create_tables.sql index 0c68535..50f06c8 100644 --- a/i18n/migrations/20260302163122_create_tables.sql +++ b/i18n/migrations/20260302163122_create_tables.sql @@ -251,6 +251,11 @@ CREATE TABLE IF NOT EXISTS b2b_customer_orders ( country_id BIGINT UNSIGNED NOT NULL, address_string TEXT NOT NULL, status VARCHAR(50) NOT NULL, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + base_price DECIMAL(10, 2) NOT NULL, + tax_incl DECIMAL(10, 2) NOT NULL, + tax_excl DECIMAL(10, 2) NOT NULL, CONSTRAINT fk_customer_orders_customers FOREIGN KEY (user_id) REFERENCES b2b_customers(id) ON DELETE NO ACTION ON UPDATE CASCADE, CONSTRAINT fk_customer_orders_countries FOREIGN KEY (country_id) REFERENCES b2b_countries(id) ON DELETE NO ACTION ON UPDATE CASCADE ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;