chore: apply new logger to order actions
This commit is contained in:
@@ -1,122 +1,116 @@
|
||||
package orderStatusActions
|
||||
|
||||
import (
|
||||
"log"
|
||||
// func init() {
|
||||
// GlobalRegistry.Register(enums.OrderStatusConfirmed, ActionChain{
|
||||
// SendOrderConfirmationEmail,
|
||||
// NotifyInventorySystem,
|
||||
// })
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model/enums"
|
||||
)
|
||||
// GlobalRegistry.Register(enums.OrderStatusProcessing, ActionChain{
|
||||
// NotifyWarehouse,
|
||||
// ReserveInventory,
|
||||
// })
|
||||
|
||||
func init() {
|
||||
GlobalRegistry.Register(enums.OrderStatusConfirmed, ActionChain{
|
||||
SendOrderConfirmationEmail,
|
||||
NotifyInventorySystem,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusShipped, ActionChain{
|
||||
// NotifyWarehouseShipped,
|
||||
// GenerateTrackingNumber,
|
||||
// SendShippingNotificationEmail,
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusProcessing, ActionChain{
|
||||
NotifyWarehouse,
|
||||
ReserveInventory,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusDelivered, ActionChain{
|
||||
// SendDeliveryConfirmationEmail,
|
||||
// NotifyFulfillmentComplete,
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusShipped, ActionChain{
|
||||
NotifyWarehouseShipped,
|
||||
GenerateTrackingNumber,
|
||||
SendShippingNotificationEmail,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusCancelled, ActionChain{
|
||||
// SendCancellationEmail,
|
||||
// ReleaseInventory,
|
||||
// ProcessRefund,
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusDelivered, ActionChain{
|
||||
SendDeliveryConfirmationEmail,
|
||||
NotifyFulfillmentComplete,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusReturned, ActionChain{
|
||||
// SendReturnConfirmationEmail,
|
||||
// NotifyReturnsDepartment,
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusCancelled, ActionChain{
|
||||
SendCancellationEmail,
|
||||
ReleaseInventory,
|
||||
ProcessRefund,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusRefunded, ActionChain{
|
||||
// NotifyRefundProcessed,
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusReturned, ActionChain{
|
||||
SendReturnConfirmationEmail,
|
||||
NotifyReturnsDepartment,
|
||||
})
|
||||
// GlobalRegistry.Register(enums.OrderStatusPending, ActionChain{})
|
||||
// }
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusRefunded, ActionChain{
|
||||
NotifyRefundProcessed,
|
||||
})
|
||||
// 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}
|
||||
// })
|
||||
|
||||
GlobalRegistry.Register(enums.OrderStatusPending, ActionChain{})
|
||||
}
|
||||
// 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 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 NotifyWarehouse = WithID("notify_warehouse", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Notifying warehouse 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 ReserveInventory = WithID("reserve_inventory", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Reserving inventory 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 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 ReserveInventory = WithID("reserve_inventory", func(actionCtx ActionContext) ActionResult {
|
||||
log.Printf("Reserving inventory 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 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 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 GenerateTrackingNumber = WithID("generate_tracking_number", func(actionCtx ActionContext) ActionResult {
|
||||
log.Printf("Generating tracking number 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 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 NotifyFulfillmentComplete = WithID("notify_fulfillment_complete", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Notifying fulfillment complete 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 SendCancellationEmail = WithID("send_cancellation_email", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Sending cancellation 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 ReleaseInventory = WithID("release_inventory", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Releasing inventory 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 ProcessRefund = WithID("process_refund", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Processing refund 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 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 ProcessRefund = WithID("process_refund", func(actionCtx ActionContext) ActionResult {
|
||||
log.Printf("Processing refund 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 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}
|
||||
})
|
||||
// var NotifyRefundProcessed = WithID("notify_refund_processed", func(actionCtx ActionContext) ActionResult {
|
||||
// log.Printf("Notifying refund processed for order %d", actionCtx.OrderId)
|
||||
// return ActionResult{Err: nil}
|
||||
// })
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
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"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||
)
|
||||
|
||||
var GlobalRegistry = make(ActionRegistry)
|
||||
@@ -13,7 +12,6 @@ var GlobalRegistry = make(ActionRegistry)
|
||||
type ActionID string
|
||||
|
||||
type ActionContext struct {
|
||||
OrderId uint
|
||||
Order *model.CustomerOrder
|
||||
UserId *uint
|
||||
EmailService *emailService.EmailService
|
||||
@@ -37,7 +35,12 @@ func (c ActionChain) Execute(actionCtx ActionContext) []ActionResult {
|
||||
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)
|
||||
logger.Debug("action failed",
|
||||
"action_id", action.ID(),
|
||||
"order", actionCtx.Order,
|
||||
"user_id", actionCtx.UserId,
|
||||
"error", result.Err,
|
||||
)
|
||||
}
|
||||
}
|
||||
return results
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/templ/emails"
|
||||
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/logger"
|
||||
"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
|
||||
func (s *EmailService) SendEmail(to, subject, body string) error {
|
||||
if !s.config.Enabled {
|
||||
logger.Debug("email service is disabled",
|
||||
"service", "EmailService.SendEmail",
|
||||
"to", to,
|
||||
"subject", subject,
|
||||
)
|
||||
return fmt.Errorf("email service is disabled")
|
||||
}
|
||||
|
||||
@@ -69,6 +75,12 @@ func (s *EmailService) SendEmail(to, subject, body string) error {
|
||||
// Send email
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -120,9 +132,12 @@ func (s *EmailService) SendNewUserAdminNotification(userEmail, userName, baseURL
|
||||
// SendNewOrderPlacedNotification sends an email to admin when new order is placed
|
||||
func (s *EmailService) SendNewOrderPlacedNotification(userID uint) error {
|
||||
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
|
||||
}
|
||||
fmt.Printf("userID: %v\n", userID)
|
||||
subject := "New Order Created"
|
||||
body := s.newOrderPlacedTemplate(userID)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user