order-actions #79
116
app/actions/orderStatusActions/examples.go
Normal file
116
app/actions/orderStatusActions/examples.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package orderStatusActions
|
||||||
|
|
||||||
|
// func init() {
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusConfirmed, ActionChain{
|
||||||
|
// SendOrderConfirmationEmail,
|
||||||
|
// NotifyInventorySystem,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusProcessing, ActionChain{
|
||||||
|
// NotifyWarehouse,
|
||||||
|
// ReserveInventory,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusShipped, ActionChain{
|
||||||
|
// NotifyWarehouseShipped,
|
||||||
|
// GenerateTrackingNumber,
|
||||||
|
// SendShippingNotificationEmail,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusDelivered, ActionChain{
|
||||||
|
// SendDeliveryConfirmationEmail,
|
||||||
|
// NotifyFulfillmentComplete,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusCancelled, ActionChain{
|
||||||
|
// SendCancellationEmail,
|
||||||
|
// ReleaseInventory,
|
||||||
|
// ProcessRefund,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusReturned, ActionChain{
|
||||||
|
// SendReturnConfirmationEmail,
|
||||||
|
// NotifyReturnsDepartment,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusRefunded, ActionChain{
|
||||||
|
// NotifyRefundProcessed,
|
||||||
|
// })
|
||||||
|
|
||||||
|
// GlobalRegistry.Register(enums.OrderStatusPending, ActionChain{})
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var SendOrderConfirmationEmail = WithID("send_order_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Sending order confirmation email for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyInventorySystem = WithID("notify_inventory_system", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying inventory system for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyWarehouse = WithID("notify_warehouse", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying warehouse for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var ReserveInventory = WithID("reserve_inventory", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Reserving inventory for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyWarehouseShipped = WithID("notify_warehouse_shipped", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying warehouse of shipment for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var GenerateTrackingNumber = WithID("generate_tracking_number", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Generating tracking number for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var SendShippingNotificationEmail = WithID("send_shipping_notification_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Sending shipping notification email for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var SendDeliveryConfirmationEmail = WithID("send_delivery_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Sending delivery confirmation email for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyFulfillmentComplete = WithID("notify_fulfillment_complete", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying fulfillment complete for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var SendCancellationEmail = WithID("send_cancellation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Sending cancellation email for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var ReleaseInventory = WithID("release_inventory", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Releasing inventory for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var ProcessRefund = WithID("process_refund", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Processing refund for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var SendReturnConfirmationEmail = WithID("send_return_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Sending return confirmation email for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyReturnsDepartment = WithID("notify_returns_department", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying returns department for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var NotifyRefundProcessed = WithID("notify_refund_processed", func(actionCtx ActionContext) ActionResult {
|
||||||
|
// log.Printf("Notifying refund processed for order %d", actionCtx.OrderId)
|
||||||
|
// return ActionResult{Err: nil}
|
||||||
|
// })
|
||||||
21
app/actions/orderStatusActions/pending.go
Normal file
21
app/actions/orderStatusActions/pending.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package orderStatusActions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var sendNewOrderEmail = WithID("send_new_order_email", func(actionCtx ActionContext) ActionResult {
|
||||||
|
|
||||||
|
if actionCtx.EmailService == nil {
|
||||||
|
return ActionResult{Err: fmt.Errorf("emailService not provided")}
|
||||||
|
}
|
||||||
|
return ActionResult{Err: actionCtx.EmailService.SendNewOrderPlacedNotification(*actionCtx.UserId)}
|
||||||
|
})
|
||||||
|
|
||||||
|
GlobalRegistry.Register(enums.OrderStatusPending, ActionChain{
|
||||||
|
sendNewOrderEmail,
|
||||||
|
})
|
||||||
|
}
|
||||||
88
app/actions/orderStatusActions/registry.go
Normal file
88
app/actions/orderStatusActions/registry.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package orderStatusActions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/service/emailService"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
var GlobalRegistry = make(ActionRegistry)
|
||||||
|
|
||||||
|
type ActionID string
|
||||||
|
|
||||||
|
type ActionContext struct {
|
||||||
|
Order *model.CustomerOrder
|
||||||
|
UserId *uint
|
||||||
|
EmailService *emailService.EmailService
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActionResult struct {
|
||||||
|
Err error
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderAction interface {
|
||||||
|
ID() ActionID
|
||||||
|
Execute(actionCtx ActionContext) ActionResult
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActionChain []OrderAction
|
||||||
|
|
||||||
|
func (c ActionChain) Execute(actionCtx ActionContext) []ActionResult {
|
||||||
|
results := make([]ActionResult, 0, len(c))
|
||||||
|
for _, action := range c {
|
||||||
|
result := action.Execute(actionCtx)
|
||||||
|
results = append(results, result)
|
||||||
|
if result.Err != nil {
|
||||||
|
logger.Debug("action failed",
|
||||||
|
"action_id", action.ID(),
|
||||||
|
"order", actionCtx.Order,
|
||||||
|
"user_id", actionCtx.UserId,
|
||||||
|
"error", result.Err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActionRegistry map[enums.OrderStatus]ActionChain
|
||||||
|
|
||||||
|
func (r ActionRegistry) Register(status enums.OrderStatus, chain ActionChain) {
|
||||||
|
r[status] = chain
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ActionRegistry) ExecuteForStatus(status enums.OrderStatus, actionCtx ActionContext) []ActionResult {
|
||||||
|
chain, exists := r[status]
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return chain.Execute(actionCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActionFunc func(actionCtx ActionContext) ActionResult
|
||||||
|
|
||||||
|
func (f ActionFunc) ID() ActionID {
|
||||||
|
return "anonymous"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f ActionFunc) Execute(actionCtx ActionContext) ActionResult {
|
||||||
|
return f(actionCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
type actionAdapter struct {
|
||||||
|
id ActionID
|
||||||
|
fn ActionFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *actionAdapter) ID() ActionID {
|
||||||
|
return a.id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *actionAdapter) Execute(actionCtx ActionContext) ActionResult {
|
||||||
|
return a.fn(actionCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithID(id ActionID, fn ActionFunc) OrderAction {
|
||||||
|
return &actionAdapter{id: id, fn: fn}
|
||||||
|
}
|
||||||
@@ -29,12 +29,12 @@ func NewCartsHandler() *CartsHandler {
|
|||||||
func CartsHandlerRoutes(r fiber.Router) fiber.Router {
|
func CartsHandlerRoutes(r fiber.Router) fiber.Router {
|
||||||
handler := NewCartsHandler()
|
handler := NewCartsHandler()
|
||||||
|
|
||||||
r.Get("/add-new-cart", handler.AddNewCart)
|
r.Post("/add-new-cart", handler.AddNewCart)
|
||||||
r.Delete("/remove-cart", handler.RemoveCart)
|
r.Delete("/remove-cart", handler.RemoveCart)
|
||||||
r.Get("/change-cart-name", handler.ChangeCartName)
|
r.Patch("/change-cart-name", handler.ChangeCartName)
|
||||||
r.Get("/retrieve-carts-info", handler.RetrieveCartsInfo)
|
r.Get("/retrieve-carts-info", handler.RetrieveCartsInfo)
|
||||||
r.Get("/retrieve-cart", handler.RetrieveCart)
|
r.Get("/retrieve-cart", handler.RetrieveCart)
|
||||||
r.Get("/add-product-to-cart", handler.AddProduct)
|
r.Post("/add-product-to-cart", handler.AddProduct)
|
||||||
r.Delete("/remove-product-from-cart", handler.RemoveProduct)
|
r.Delete("/remove-product-from-cart", handler.RemoveProduct)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ package restricted
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/service/orderService"
|
"git.ma-al.com/goc_daniel/b2b/app/service/orderService"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
||||||
@@ -32,7 +36,7 @@ func OrdersHandlerRoutes(r fiber.Router) fiber.Router {
|
|||||||
r.Get("/list", handler.ListOrders)
|
r.Get("/list", handler.ListOrders)
|
||||||
r.Post("/place-new-order", handler.PlaceNewOrder)
|
r.Post("/place-new-order", handler.PlaceNewOrder)
|
||||||
r.Post("/change-order-address", handler.ChangeOrderAddress)
|
r.Post("/change-order-address", handler.ChangeOrderAddress)
|
||||||
r.Get("/change-order-status", handler.ChangeOrderStatus)
|
r.Patch("/change-order-status", middleware.Require(perms.OrdersModifyAll), handler.ChangeOrderStatus)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@@ -109,7 +113,13 @@ func (h *OrdersHandler) PlaceNewOrder(c fiber.Ctx) error {
|
|||||||
|
|
||||||
name := c.Query("name")
|
name := c.Query("name")
|
||||||
|
|
||||||
err = h.ordersService.PlaceNewOrder(userID, uint(cart_id), name, uint(country_id), address_info)
|
originalUserId, ok := localeExtractor.GetOriginalUserID(c)
|
||||||
|
if !ok {
|
||||||
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
||||||
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.ordersService.PlaceNewOrder(userID, uint(cart_id), name, uint(country_id), address_info, originalUserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
logger.Error("failed to place order",
|
logger.Error("failed to place order",
|
||||||
@@ -172,22 +182,19 @@ func (h *OrdersHandler) ChangeOrderAddress(c fiber.Ctx) error {
|
|||||||
// we base permissions and user based on target user only.
|
// we base permissions and user based on target user only.
|
||||||
// TODO: well, permissions and all that.
|
// TODO: well, permissions and all that.
|
||||||
func (h *OrdersHandler) ChangeOrderStatus(c fiber.Ctx) error {
|
func (h *OrdersHandler) ChangeOrderStatus(c fiber.Ctx) error {
|
||||||
user, ok := localeExtractor.GetCustomer(c)
|
userId, ok := localeExtractor.GetUserID(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
||||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
||||||
}
|
}
|
||||||
|
|
||||||
order_id_attribute := c.Query("order_id")
|
order_id, err := strconv.Atoi(c.Query("order_id"))
|
||||||
order_id, err := strconv.Atoi(order_id_attribute)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
||||||
}
|
}
|
||||||
|
|
||||||
status := c.Query("status")
|
err = h.ordersService.ChangeOrderStatus(userId, uint(order_id), enums.OrderStatus(strings.ToUpper(c.Query("status"))))
|
||||||
|
|
||||||
err = h.ordersService.ChangeOrderStatus(user, uint(order_id), status)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
logger.Error("failed to change order status",
|
logger.Error("failed to change order status",
|
||||||
|
|||||||
16
app/model/enums/orderStatus.go
Normal file
16
app/model/enums/orderStatus.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package enums
|
||||||
|
|
||||||
|
type OrderStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
OrderStatusPending OrderStatus = "PENDING"
|
||||||
|
OrderStatusConfirmed OrderStatus = "CONFIRMED"
|
||||||
|
OrderStatusProcessing OrderStatus = "PROCESSING"
|
||||||
|
OrderStatusShipped OrderStatus = "SHIPPED"
|
||||||
|
OrderStatusOutForDelivery OrderStatus = "OUT_FOR_DELIVERY"
|
||||||
|
OrderStatusDelivered OrderStatus = "DELIVERED"
|
||||||
|
OrderStatusCancelled OrderStatus = "CANCELLED"
|
||||||
|
OrderStatusReturned OrderStatus = "RETURNED"
|
||||||
|
OrderStatusRefunded OrderStatus = "REFUNDED"
|
||||||
|
OrderStatusFailed OrderStatus = "FAILED"
|
||||||
|
)
|
||||||
@@ -1,21 +1,25 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||||
|
)
|
||||||
|
|
||||||
type CustomerOrder struct {
|
type CustomerOrder struct {
|
||||||
OrderID uint `gorm:"column:order_id;primaryKey;autoIncrement" json:"order_id"`
|
OrderID uint `gorm:"column:order_id;primaryKey;autoIncrement" json:"order_id"`
|
||||||
UserID uint `gorm:"column:user_id;not null;index" json:"user_id"`
|
UserID uint `gorm:"column:user_id;not null;index" json:"user_id"`
|
||||||
Name string `gorm:"column:name;not null" json:"name"`
|
Name string `gorm:"column:name;not null" json:"name"`
|
||||||
CountryID uint `gorm:"column:country_id;not null" json:"country_id"`
|
CountryID uint `gorm:"column:country_id;not null" json:"country_id"`
|
||||||
AddressString string `gorm:"column:address_string;not null" json:"address_string"`
|
AddressString string `gorm:"column:address_string;not null" json:"address_string"`
|
||||||
AddressUnparsed *AddressUnparsed `gorm:"-" json:"address_unparsed"`
|
AddressUnparsed *AddressUnparsed `gorm:"-" json:"address_unparsed"`
|
||||||
Status string `gorm:"column:status;size:50;not null" json:"status"`
|
Status enums.OrderStatus `gorm:"column:status;size:50;not null" json:"status"`
|
||||||
BasePrice float64 `gorm:"column:base_price;type:decimal(10,2);not null" json:"base_price"`
|
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"`
|
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"`
|
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"`
|
CreatedAt time.Time `gorm:"column:created_at;not null" json:"created_at"`
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at;not null" json:"updated_at"`
|
UpdatedAt time.Time `gorm:"column:updated_at;not null" json:"updated_at"`
|
||||||
Products []OrderProduct `gorm:"foreignKey:OrderID;references:OrderID" json:"products"`
|
Products []OrderProduct `gorm:"foreignKey:OrderID;references:OrderID" json:"products"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (CustomerOrder) TableName() string {
|
func (CustomerOrder) TableName() string {
|
||||||
|
|||||||
20
app/model/orderStatusHistory.go
Normal file
20
app/model/orderStatusHistory.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderStatusHistory struct {
|
||||||
|
Id uint `gorm:"column:id;primaryKey;autoIncrement"`
|
||||||
|
OrderId uint `gorm:"column:order_id;not null;index:idx_order_status_history_order"`
|
||||||
|
OldStatus *enums.OrderStatus `gorm:"column:old_status;type:varchar(50)"`
|
||||||
|
NewStatus enums.OrderStatus `gorm:"column:new_status;type:varchar(50);not null"`
|
||||||
|
CreatedAt time.Time `gorm:"column:created_at;not null;autoCreateTime"`
|
||||||
|
UserId uint `gorm:"column:user_id;index:idx_order_status_history_user;not null"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (OrderStatusHistory) TableName() string {
|
||||||
|
return "b2b_order_status_history"
|
||||||
|
}
|
||||||
@@ -5,17 +5,19 @@ import (
|
|||||||
|
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/db"
|
"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/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/filters"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UIOrdersRepo interface {
|
type UIOrdersRepo interface {
|
||||||
UserHasOrder(user_id uint, order_id uint) (bool, error)
|
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)
|
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, base_price float64, tax_incl float64, tax_excl float64) error
|
PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string, originalUserId uint, base_price float64, tax_incl float64, tax_excl float64) (*model.CustomerOrder, error)
|
||||||
ChangeOrderAddress(order_id uint, country_id uint, address_info string) 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{}
|
type OrdersRepo struct{}
|
||||||
@@ -37,6 +39,18 @@ func (repo *OrdersRepo) UserHasOrder(user_id uint, order_id uint) (bool, error)
|
|||||||
return amt >= 1, err
|
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) {
|
func (repo *OrdersRepo) Find(user_id uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.CustomerOrder], error) {
|
||||||
var list []model.CustomerOrder
|
var list []model.CustomerOrder
|
||||||
var total int64
|
var total int64
|
||||||
@@ -71,13 +85,13 @@ func (repo *OrdersRepo) Find(user_id uint, p find.Paging, filt *filters.FiltersL
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, country_id uint, address_info string, originalUserId uint, base_price float64, tax_incl float64, tax_excl float64) (*model.CustomerOrder, error) {
|
||||||
order := model.CustomerOrder{
|
order := model.CustomerOrder{
|
||||||
UserID: cart.UserID,
|
UserID: cart.UserID,
|
||||||
Name: name,
|
Name: name,
|
||||||
CountryID: country_id,
|
CountryID: country_id,
|
||||||
AddressString: address_info,
|
AddressString: address_info,
|
||||||
Status: constdata.NEW_ORDER_STATUS,
|
Status: enums.OrderStatusPending,
|
||||||
Products: make([]model.OrderProduct, 0, len(cart.Products)),
|
Products: make([]model.OrderProduct, 0, len(cart.Products)),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,14 +102,35 @@ func (repo *OrdersRepo) PlaceNewOrder(cart *model.CustomerCart, name string, cou
|
|||||||
Amount: product.Amount,
|
Amount: product.Amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
order.CreatedAt = time.Now()
|
order.CreatedAt = time.Now()
|
||||||
order.UpdatedAt = time.Now()
|
order.UpdatedAt = time.Now()
|
||||||
order.BasePrice = base_price
|
order.BasePrice = base_price
|
||||||
order.TaxIncl = tax_incl
|
order.TaxIncl = tax_incl
|
||||||
order.TaxExcl = tax_excl
|
order.TaxExcl = tax_excl
|
||||||
|
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 {
|
func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, address_info string) error {
|
||||||
@@ -110,13 +145,48 @@ func (repo *OrdersRepo) ChangeOrderAddress(order_id uint, country_id uint, addre
|
|||||||
Error
|
Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *OrdersRepo) ChangeOrderStatus(order_id uint, status string) error {
|
func (repo *OrdersRepo) ChangeOrderStatus(orderID uint, newStatus enums.OrderStatus, userId uint) error {
|
||||||
return db.DB.
|
tx := db.Get().Begin()
|
||||||
Table("b2b_customer_orders").
|
|
||||||
Where("order_id = ?", order_id).
|
var currentStatus enums.OrderStatus
|
||||||
Updates(map[string]interface{}{
|
err := tx.Table("b2b_customer_orders").
|
||||||
"status": status,
|
Select("status").
|
||||||
"updated_at": time.Now(),
|
Where("order_id = ?", orderID).
|
||||||
}).
|
Scan(¤tStatus).Error
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"git.ma-al.com/goc_daniel/b2b/app/templ/emails"
|
"git.ma-al.com/goc_daniel/b2b/app/templ/emails"
|
||||||
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/view"
|
"git.ma-al.com/goc_daniel/b2b/app/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,6 +45,11 @@ func getLangID(isoCode string) uint {
|
|||||||
// SendEmail sends an email to the specified recipient
|
// SendEmail sends an email to the specified recipient
|
||||||
func (s *EmailService) SendEmail(to, subject, body string) error {
|
func (s *EmailService) SendEmail(to, subject, body string) error {
|
||||||
if !s.config.Enabled {
|
if !s.config.Enabled {
|
||||||
|
logger.Debug("email service is disabled",
|
||||||
|
"service", "EmailService.SendEmail",
|
||||||
|
"to", to,
|
||||||
|
"subject", subject,
|
||||||
|
)
|
||||||
return fmt.Errorf("email service is disabled")
|
return fmt.Errorf("email service is disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +75,12 @@ func (s *EmailService) SendEmail(to, subject, body string) error {
|
|||||||
// Send email
|
// Send email
|
||||||
addr := fmt.Sprintf("%s:%d", s.config.SMTPHost, s.config.SMTPPort)
|
addr := fmt.Sprintf("%s:%d", s.config.SMTPHost, s.config.SMTPPort)
|
||||||
if err := smtp.SendMail(addr, auth, s.config.FromEmail, []string{to}, []byte(msg.String())); err != nil {
|
if err := smtp.SendMail(addr, auth, s.config.FromEmail, []string{to}, []byte(msg.String())); err != nil {
|
||||||
|
logger.Error("failed to send email",
|
||||||
|
"service", "EmailService.SendEmail",
|
||||||
|
"to", to,
|
||||||
|
"subject", subject,
|
||||||
|
"error", err.Error(),
|
||||||
|
)
|
||||||
return fmt.Errorf("failed to send email: %w", err)
|
return fmt.Errorf("failed to send email: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,9 +132,12 @@ func (s *EmailService) SendNewUserAdminNotification(userEmail, userName, baseURL
|
|||||||
// SendNewOrderPlacedNotification sends an email to admin when new order is placed
|
// SendNewOrderPlacedNotification sends an email to admin when new order is placed
|
||||||
func (s *EmailService) SendNewOrderPlacedNotification(userID uint) error {
|
func (s *EmailService) SendNewOrderPlacedNotification(userID uint) error {
|
||||||
if s.config.AdminEmail == "" {
|
if s.config.AdminEmail == "" {
|
||||||
return nil // No admin email configured
|
logger.Warn("no admin email setup in the config",
|
||||||
|
"service", "EmailService.SendNewOrderPlacedNotification",
|
||||||
|
"user_id", userID,
|
||||||
|
)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
subject := "New Order Created"
|
subject := "New Order Created"
|
||||||
body := s.newOrderPlacedTemplate(userID)
|
body := s.newOrderPlacedTemplate(userID)
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package orderService
|
|||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/actions/orderStatusActions"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms"
|
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||||
|
"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/cartsRepo"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/repos/ordersRepo"
|
"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/repos/productsRepo"
|
||||||
@@ -23,6 +25,7 @@ type OrderService struct {
|
|||||||
productsRepo productsRepo.UIProductsRepo
|
productsRepo productsRepo.UIProductsRepo
|
||||||
addressesService *addressesService.AddressesService
|
addressesService *addressesService.AddressesService
|
||||||
emailService *emailService.EmailService
|
emailService *emailService.EmailService
|
||||||
|
actionRegistry *orderStatusActions.ActionRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *OrderService {
|
func New() *OrderService {
|
||||||
@@ -32,9 +35,23 @@ func New() *OrderService {
|
|||||||
productsRepo: productsRepo.New(),
|
productsRepo: productsRepo.New(),
|
||||||
addressesService: addressesService.New(),
|
addressesService: addressesService.New(),
|
||||||
emailService: emailService.NewEmailService(),
|
emailService: emailService.NewEmailService(),
|
||||||
|
actionRegistry: &orderStatusActions.GlobalRegistry,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ValidStatuses = map[enums.OrderStatus]bool{
|
||||||
|
enums.OrderStatusPending: true,
|
||||||
|
enums.OrderStatusConfirmed: true,
|
||||||
|
enums.OrderStatusProcessing: true,
|
||||||
|
enums.OrderStatusShipped: true,
|
||||||
|
enums.OrderStatusOutForDelivery: true,
|
||||||
|
enums.OrderStatusDelivered: true,
|
||||||
|
enums.OrderStatusCancelled: true,
|
||||||
|
enums.OrderStatusReturned: true,
|
||||||
|
enums.OrderStatusRefunded: true,
|
||||||
|
enums.OrderStatusFailed: true,
|
||||||
|
}
|
||||||
|
|
||||||
func (s *OrderService) Find(user *model.Customer, p find.Paging, filt *filters.FiltersList) (*find.Found[model.CustomerOrder], error) {
|
func (s *OrderService) Find(user *model.Customer, p find.Paging, filt *filters.FiltersList) (*find.Found[model.CustomerOrder], error) {
|
||||||
if !user.HasPermission(perms.OrdersViewAll) {
|
if !user.HasPermission(perms.OrdersViewAll) {
|
||||||
// append filter to view only this user's orders
|
// append filter to view only this user's orders
|
||||||
@@ -63,7 +80,7 @@ func (s *OrderService) Find(user *model.Customer, p find.Paging, filt *filters.F
|
|||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, country_id uint, address_info string) error {
|
func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, country_id uint, address_info string, originalUserId uint) error {
|
||||||
_, err := s.addressesService.ValidateAddressJson(address_info, country_id)
|
_, err := s.addressesService.ValidateAddressJson(address_info, country_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -92,7 +109,7 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
|||||||
base_price, tax_incl, tax_excl, err := s.getOrderTotalPrice(user_id, cart_id, country_id)
|
base_price, tax_incl, tax_excl, err := s.getOrderTotalPrice(user_id, cart_id, country_id)
|
||||||
|
|
||||||
// all checks passed
|
// all checks passed
|
||||||
err = s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, base_price, tax_incl, tax_excl)
|
order, err := s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, originalUserId, base_price, tax_incl, tax_excl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -109,19 +126,8 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// send email to admin
|
return s.ChangeOrderStatus(user_id, order.OrderID, enums.OrderStatusPending)
|
||||||
go func(user_id uint) {
|
|
||||||
err := s.emailService.SendNewOrderPlacedNotification(user_id)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warn("failed to send new order notification",
|
|
||||||
"service", "orderService",
|
|
||||||
"user_id", user_id,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}(user_id)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OrderService) ChangeOrderAddress(user *model.Customer, order_id uint, country_id uint, address_info string) error {
|
func (s *OrderService) ChangeOrderAddress(user *model.Customer, order_id uint, country_id uint, address_info string) error {
|
||||||
@@ -144,20 +150,35 @@ func (s *OrderService) ChangeOrderAddress(user *model.Customer, order_id uint, c
|
|||||||
return s.ordersRepo.ChangeOrderAddress(order_id, country_id, address_info)
|
return s.ordersRepo.ChangeOrderAddress(order_id, country_id, address_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is obiously just an initial version of this function
|
func (s *OrderService) ChangeOrderStatus(userId, orderId uint, newStatus enums.OrderStatus) error {
|
||||||
func (s *OrderService) ChangeOrderStatus(user *model.Customer, order_id uint, status string) error {
|
order, err := s.ordersRepo.Get(orderId)
|
||||||
if !user.HasPermission(perms.OrdersModifyAll) {
|
if err != nil {
|
||||||
exists, err := s.ordersRepo.UserHasOrder(user.ID, order_id)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
if order == nil {
|
||||||
}
|
return responseErrors.ErrOrderNotFound
|
||||||
|
|
||||||
if !exists {
|
|
||||||
return responseErrors.ErrUserHasNoSuchOrder
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.ordersRepo.ChangeOrderStatus(order_id, status)
|
if !ValidStatuses[newStatus] {
|
||||||
|
return responseErrors.ErrInvalidStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.ordersRepo.ChangeOrderStatus(order.OrderID, newStatus, userId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
actionCtx := orderStatusActions.ActionContext{
|
||||||
|
Order: order,
|
||||||
|
UserId: &userId,
|
||||||
|
EmailService: s.emailService,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_ = s.actionRegistry.ExecuteForStatus(newStatus, actionCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OrderService) getOrderTotalPrice(user_id uint, cart_id uint, country_id uint) (float64, float64, float64, error) {
|
func (s *OrderService) getOrderTotalPrice(user_id uint, cart_id uint, country_id uint) (float64, float64, float64, error) {
|
||||||
|
|||||||
@@ -14,6 +14,14 @@ func GetLangID(c fiber.Ctx) (uint, bool) {
|
|||||||
return user_locale.OriginalUser.LangID, true
|
return user_locale.OriginalUser.LangID, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOriginalUserID(c fiber.Ctx) (uint, bool) {
|
||||||
|
user_locale, ok := c.Locals(constdata.USER_LOCALE).(*model.UserLocale)
|
||||||
|
if !ok || user_locale.OriginalUser == nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return user_locale.OriginalUser.ID, true
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserID(c fiber.Ctx) (uint, bool) {
|
func GetUserID(c fiber.Ctx) (uint, bool) {
|
||||||
user_locale, ok := c.Locals(constdata.USER_LOCALE).(*model.UserLocale)
|
user_locale, ok := c.Locals(constdata.USER_LOCALE).(*model.UserLocale)
|
||||||
if !ok || user_locale.User == nil {
|
if !ok || user_locale.User == nil {
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ var (
|
|||||||
// Typed errors for orders handler
|
// Typed errors for orders handler
|
||||||
ErrEmptyCart = errors.New("the cart is empty")
|
ErrEmptyCart = errors.New("the cart is empty")
|
||||||
ErrUserHasNoSuchOrder = errors.New("user does not have order with given id")
|
ErrUserHasNoSuchOrder = errors.New("user does not have order with given id")
|
||||||
|
ErrInvalidStatus = errors.New("invalid order status")
|
||||||
|
ErrOrderNotFound = errors.New("order not found")
|
||||||
|
|
||||||
// Typed errors for price reduction handler
|
// Typed errors for price reduction handler
|
||||||
ErrInvalidReductionType = errors.New("invalid reduction type: must be 'amount' or 'percentage'")
|
ErrInvalidReductionType = errors.New("invalid reduction type: must be 'amount' or 'percentage'")
|
||||||
@@ -314,7 +316,8 @@ func GetErrorStatus(err error) int {
|
|||||||
errors.Is(err, ErrMaxAmtOfAddressesReached),
|
errors.Is(err, ErrMaxAmtOfAddressesReached),
|
||||||
errors.Is(err, ErrUserHasNoSuchAddress),
|
errors.Is(err, ErrUserHasNoSuchAddress),
|
||||||
errors.Is(err, ErrInvalidCountryID),
|
errors.Is(err, ErrInvalidCountryID),
|
||||||
errors.Is(err, ErrInvalidAddressJSON):
|
errors.Is(err, ErrInvalidAddressJSON),
|
||||||
|
errors.Is(err, ErrInvalidStatus):
|
||||||
return fiber.StatusBadRequest
|
return fiber.StatusBadRequest
|
||||||
case errors.Is(err, ErrSpecificPriceNotFound):
|
case errors.Is(err, ErrSpecificPriceNotFound):
|
||||||
return fiber.StatusNotFound
|
return fiber.StatusNotFound
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
info:
|
info:
|
||||||
name: Delete Index - MeiliSearch
|
name: Delete Index - MeiliSearch
|
||||||
type: http
|
type: http
|
||||||
seq: 7
|
seq: 8
|
||||||
|
|
||||||
http:
|
http:
|
||||||
method: DELETE
|
method: DELETE
|
||||||
|
|||||||
19
bruno/api_v1/cart/Add new cart.yml
Normal file
19
bruno/api_v1/cart/Add new cart.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
info:
|
||||||
|
name: Add new cart
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: POST
|
||||||
|
url: "{{bas_url}}/restricted/carts/add-new-cart?name=TestCart"
|
||||||
|
params:
|
||||||
|
- name: name
|
||||||
|
value: TestCart
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
31
bruno/api_v1/cart/Add product to cart.yml
Normal file
31
bruno/api_v1/cart/Add product to cart.yml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
info:
|
||||||
|
name: Add product to cart
|
||||||
|
type: http
|
||||||
|
seq: 6
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: POST
|
||||||
|
url: "{{bas_url}}/restricted/carts/add-product-to-cart?cart_id=1&product_id=51&product_attribute_id=1115&amount=1&set_amount=true"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: product_id
|
||||||
|
value: "51"
|
||||||
|
type: query
|
||||||
|
- name: product_attribute_id
|
||||||
|
value: "1115"
|
||||||
|
type: query
|
||||||
|
- name: amount
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: set_amount
|
||||||
|
value: "true"
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
22
bruno/api_v1/cart/Change cart name.yml
Normal file
22
bruno/api_v1/cart/Change cart name.yml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
info:
|
||||||
|
name: Change cart name
|
||||||
|
type: http
|
||||||
|
seq: 3
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: PATCH
|
||||||
|
url: "{{bas_url}}/restricted/carts/change-cart-name?cart_id=1&new_name=UpdatedCart"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: new_name
|
||||||
|
value: UpdatedCart
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
19
bruno/api_v1/cart/Remove cart.yml
Normal file
19
bruno/api_v1/cart/Remove cart.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
info:
|
||||||
|
name: Remove cart
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: DELETE
|
||||||
|
url: "{{bas_url}}/restricted/carts/remove-cart?cart_id=1"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
25
bruno/api_v1/cart/Remove product from cart.yml
Normal file
25
bruno/api_v1/cart/Remove product from cart.yml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
info:
|
||||||
|
name: Remove product from cart
|
||||||
|
type: http
|
||||||
|
seq: 7
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: DELETE
|
||||||
|
url: "{{bas_url}}/restricted/carts/remove-product-from-cart?cart_id=1&product_id=51&product_attribute_id=1115"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: product_id
|
||||||
|
value: "51"
|
||||||
|
type: query
|
||||||
|
- name: product_attribute_id
|
||||||
|
value: "1115"
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
19
bruno/api_v1/cart/Retrieve cart.yml
Normal file
19
bruno/api_v1/cart/Retrieve cart.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
info:
|
||||||
|
name: Retrieve cart
|
||||||
|
type: http
|
||||||
|
seq: 5
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: GET
|
||||||
|
url: "{{bas_url}}/restricted/carts/retrieve-cart?cart_id=1"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
15
bruno/api_v1/cart/Retrieve carts info.yml
Normal file
15
bruno/api_v1/cart/Retrieve carts info.yml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
info:
|
||||||
|
name: Retrieve carts info
|
||||||
|
type: http
|
||||||
|
seq: 4
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: GET
|
||||||
|
url: "{{bas_url}}/restricted/carts/retrieve-carts-info"
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
7
bruno/api_v1/cart/folder.yml
Normal file
7
bruno/api_v1/cart/folder.yml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
info:
|
||||||
|
name: cart
|
||||||
|
type: folder
|
||||||
|
seq: 7
|
||||||
|
|
||||||
|
request:
|
||||||
|
auth: inherit
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
info:
|
info:
|
||||||
name: currency
|
name: currency
|
||||||
type: folder
|
type: folder
|
||||||
seq: 9
|
seq: 10
|
||||||
|
|
||||||
request:
|
request:
|
||||||
auth: inherit
|
auth: inherit
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
info:
|
info:
|
||||||
name: customer
|
name: customer
|
||||||
type: folder
|
type: folder
|
||||||
seq: 10
|
seq: 11
|
||||||
|
|
||||||
request:
|
request:
|
||||||
auth: inherit
|
auth: inherit
|
||||||
|
|||||||
33
bruno/api_v1/order/Change order address.yml
Normal file
33
bruno/api_v1/order/Change order address.yml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
info:
|
||||||
|
name: Change order address
|
||||||
|
type: http
|
||||||
|
seq: 3
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: POST
|
||||||
|
url: "{{bas_url}}/restricted/orders/change-order-address?order_id=1&country_id=1"
|
||||||
|
params:
|
||||||
|
- name: order_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: country_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
data: |-
|
||||||
|
{
|
||||||
|
"postal_code": "31-154",
|
||||||
|
"city": "Kraków",
|
||||||
|
"voivodeship": "śląskie",
|
||||||
|
"street": "Długa",
|
||||||
|
"building_no": "5",
|
||||||
|
"recipient": "Adam Adamowicz"
|
||||||
|
}
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
22
bruno/api_v1/order/Change order status.yml
Normal file
22
bruno/api_v1/order/Change order status.yml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
info:
|
||||||
|
name: Change order status
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: PATCH
|
||||||
|
url: "{{bas_url}}/restricted/orders/change-order-status?order_id=2&status=PENDING"
|
||||||
|
params:
|
||||||
|
- name: order_id
|
||||||
|
value: "2"
|
||||||
|
type: query
|
||||||
|
- name: status
|
||||||
|
value: PENDING
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
25
bruno/api_v1/order/List.yml
Normal file
25
bruno/api_v1/order/List.yml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
info:
|
||||||
|
name: List
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: GET
|
||||||
|
url: "{{bas_url}}/restricted/orders/list?p=1&elems=30&sort=order_id,desc"
|
||||||
|
params:
|
||||||
|
- name: p
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: elems
|
||||||
|
value: "30"
|
||||||
|
type: query
|
||||||
|
- name: sort
|
||||||
|
value: order_id,desc
|
||||||
|
type: query
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
37
bruno/api_v1/order/Place new order.yml
Normal file
37
bruno/api_v1/order/Place new order.yml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
info:
|
||||||
|
name: Place new order
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
|
||||||
|
http:
|
||||||
|
method: POST
|
||||||
|
url: "{{bas_url}}/restricted/orders/place-new-order?cart_id=1&name=Test+Order&country_id=1"
|
||||||
|
params:
|
||||||
|
- name: cart_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
- name: name
|
||||||
|
value: Test Order
|
||||||
|
type: query
|
||||||
|
- name: country_id
|
||||||
|
value: "1"
|
||||||
|
type: query
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
data: |-
|
||||||
|
{
|
||||||
|
"postal_code": "31-154",
|
||||||
|
"city": "Kraków",
|
||||||
|
"voivodeship": "małopolskie",
|
||||||
|
"street": "Długa",
|
||||||
|
"building_no": "5",
|
||||||
|
"apartment_no": "7",
|
||||||
|
"recipient": "Jan Kowalski"
|
||||||
|
}
|
||||||
|
auth: inherit
|
||||||
|
|
||||||
|
settings:
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
followRedirects: true
|
||||||
|
maxRedirects: 5
|
||||||
7
bruno/api_v1/order/folder.yml
Normal file
7
bruno/api_v1/order/folder.yml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
info:
|
||||||
|
name: order
|
||||||
|
type: folder
|
||||||
|
seq: 13
|
||||||
|
|
||||||
|
request:
|
||||||
|
auth: inherit
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
info:
|
info:
|
||||||
name: product
|
name: product
|
||||||
type: folder
|
type: folder
|
||||||
seq: 8
|
seq: 9
|
||||||
|
|
||||||
request:
|
request:
|
||||||
auth: inherit
|
auth: inherit
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
info:
|
info:
|
||||||
name: routes
|
name: routes
|
||||||
type: folder
|
type: folder
|
||||||
seq: 10
|
seq: 12
|
||||||
|
|
||||||
request:
|
request:
|
||||||
auth: inherit
|
auth: inherit
|
||||||
|
|||||||
@@ -462,6 +462,30 @@ END$$
|
|||||||
|
|
||||||
DELIMITER ;
|
DELIMITER ;
|
||||||
|
|
||||||
|
CREATE TABLE b2b_order_status_history (
|
||||||
|
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
order_id BIGINT UNSIGNED NOT NULL,
|
||||||
|
old_status VARCHAR(50) NULL,
|
||||||
|
new_status VARCHAR(50) NOT NULL,
|
||||||
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||||
|
user_id BIGINT UNSIGNED NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_order_status_history_order
|
||||||
|
ON b2b_order_status_history(order_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_order_status_history_user
|
||||||
|
ON b2b_order_status_history(user_id);
|
||||||
|
|
||||||
|
ALTER TABLE b2b_order_status_history
|
||||||
|
ADD CONSTRAINT fk_order
|
||||||
|
FOREIGN KEY (order_id) REFERENCES b2b_customer_orders(order_id);
|
||||||
|
|
||||||
|
ALTER TABLE b2b_order_status_history
|
||||||
|
ADD CONSTRAINT fk_user
|
||||||
|
FOREIGN KEY (user_id) REFERENCES b2b_customers(id);
|
||||||
|
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
|
|
||||||
DROP TABLE IF EXISTS b2b_addresses;
|
DROP TABLE IF EXISTS b2b_addresses;
|
||||||
|
|||||||
Reference in New Issue
Block a user