feat: order action per status change

This commit is contained in:
2026-04-16 14:19:27 +02:00
parent 7bce04e05a
commit 16f92e53ff
33 changed files with 748 additions and 61 deletions

View File

@@ -0,0 +1,122 @@
package orderStatusActions
import (
"log"
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
)
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}
})

View 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,
})
}

View File

@@ -0,0 +1,85 @@
package orderStatusActions
import (
"log"
"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"
)
var GlobalRegistry = make(ActionRegistry)
type ActionID string
type ActionContext struct {
OrderId uint
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 {
log.Printf("action %s failed for order %d: %v", action.ID(), actionCtx.OrderId, 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}
}