feat: order action per status change
This commit is contained in:
@@ -3,17 +3,19 @@ package ordersRepo
|
||||
import (
|
||||
"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"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
||||
)
|
||||
|
||||
type UIOrdersRepo interface {
|
||||
UserHasOrder(user_id uint, order_id uint) (bool, error)
|
||||
Get(orderId uint) (*model.CustomerOrder, 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, originalUserId uint) (*model.CustomerOrder, error)
|
||||
ChangeOrderAddress(order_id uint, country_id uint, address_info string) error
|
||||
ChangeOrderStatus(order_id uint, status string) error
|
||||
ChangeOrderStatus(orderId uint, newStatus enums.OrderStatus, userId uint) error
|
||||
GetOrderStatus(orderID uint) (enums.OrderStatus, error)
|
||||
}
|
||||
|
||||
type OrdersRepo struct{}
|
||||
@@ -35,6 +37,18 @@ func (repo *OrdersRepo) UserHasOrder(user_id uint, order_id uint) (bool, error)
|
||||
return amt >= 1, err
|
||||
}
|
||||
|
||||
func (repo *OrdersRepo) Get(orderId uint) (*model.CustomerOrder, error) {
|
||||
var order model.CustomerOrder
|
||||
|
||||
err := db.Get().
|
||||
Model(&model.CustomerOrder{}).
|
||||
Preload("Products").
|
||||
Where("order_id = ?", orderId).
|
||||
First(&order).Error
|
||||
|
||||
return &order, err
|
||||
}
|
||||
|
||||
func (repo *OrdersRepo) Find(user_id uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.CustomerOrder], error) {
|
||||
var list []model.CustomerOrder
|
||||
var total int64
|
||||
@@ -69,13 +83,13 @@ 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, originalUserId uint) (*model.CustomerOrder, error) {
|
||||
order := model.CustomerOrder{
|
||||
UserID: cart.UserID,
|
||||
Name: name,
|
||||
CountryID: country_id,
|
||||
AddressString: address_info,
|
||||
Status: constdata.NEW_ORDER_STATUS,
|
||||
Status: enums.OrderStatusPending,
|
||||
Products: make([]model.OrderProduct, 0, len(cart.Products)),
|
||||
}
|
||||
|
||||
@@ -86,8 +100,30 @@ func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, cou
|
||||
Amount: product.Amount,
|
||||
})
|
||||
}
|
||||
tx := db.Get().Begin()
|
||||
err := tx.Create(&order).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
history := model.OrderStatusHistory{
|
||||
OrderId: order.OrderID,
|
||||
OldStatus: nil,
|
||||
NewStatus: enums.OrderStatusPending,
|
||||
UserId: originalUserId,
|
||||
}
|
||||
|
||||
return db.DB.Create(&order).Error
|
||||
err = tx.Create(&history).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
err = tx.Commit().Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &order, nil
|
||||
}
|
||||
|
||||
func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, address_info string) error {
|
||||
@@ -101,10 +137,48 @@ func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, addre
|
||||
Error
|
||||
}
|
||||
|
||||
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).
|
||||
Error
|
||||
func (repo *OrdersRepo) ChangeOrderStatus(orderID uint, newStatus enums.OrderStatus, userId uint) error {
|
||||
tx := db.Get().Begin()
|
||||
|
||||
var currentStatus enums.OrderStatus
|
||||
err := tx.Table("b2b_customer_orders").
|
||||
Select("status").
|
||||
Where("order_id = ?", orderID).
|
||||
Scan(¤tStatus).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Table("b2b_customer_orders").
|
||||
Where("order_id = ?", orderID).
|
||||
Update("status", string(newStatus)).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
history := model.OrderStatusHistory{
|
||||
OrderId: orderID,
|
||||
OldStatus: ¤tStatus,
|
||||
NewStatus: newStatus,
|
||||
UserId: userId,
|
||||
}
|
||||
|
||||
err = tx.Create(&history).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
func (repo *OrdersRepo) GetOrderStatus(orderID uint) (enums.OrderStatus, error) {
|
||||
var status enums.OrderStatus
|
||||
err := db.DB.Table("b2b_customer_orders").
|
||||
Select("status").
|
||||
Where("order_id = ?", orderID).
|
||||
Scan(&status).Error
|
||||
return status, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user