chore: apply new logger to order actions
This commit is contained in:
@@ -1,122 +1,116 @@
|
|||||||
package orderStatusActions
|
package orderStatusActions
|
||||||
|
|
||||||
import (
|
// func init() {
|
||||||
"log"
|
// 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.OrderStatusShipped, ActionChain{
|
||||||
GlobalRegistry.Register(enums.OrderStatusConfirmed, ActionChain{
|
// NotifyWarehouseShipped,
|
||||||
SendOrderConfirmationEmail,
|
// GenerateTrackingNumber,
|
||||||
NotifyInventorySystem,
|
// SendShippingNotificationEmail,
|
||||||
})
|
// })
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusProcessing, ActionChain{
|
// GlobalRegistry.Register(enums.OrderStatusDelivered, ActionChain{
|
||||||
NotifyWarehouse,
|
// SendDeliveryConfirmationEmail,
|
||||||
ReserveInventory,
|
// NotifyFulfillmentComplete,
|
||||||
})
|
// })
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusShipped, ActionChain{
|
// GlobalRegistry.Register(enums.OrderStatusCancelled, ActionChain{
|
||||||
NotifyWarehouseShipped,
|
// SendCancellationEmail,
|
||||||
GenerateTrackingNumber,
|
// ReleaseInventory,
|
||||||
SendShippingNotificationEmail,
|
// ProcessRefund,
|
||||||
})
|
// })
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusDelivered, ActionChain{
|
// GlobalRegistry.Register(enums.OrderStatusReturned, ActionChain{
|
||||||
SendDeliveryConfirmationEmail,
|
// SendReturnConfirmationEmail,
|
||||||
NotifyFulfillmentComplete,
|
// NotifyReturnsDepartment,
|
||||||
})
|
// })
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusCancelled, ActionChain{
|
// GlobalRegistry.Register(enums.OrderStatusRefunded, ActionChain{
|
||||||
SendCancellationEmail,
|
// NotifyRefundProcessed,
|
||||||
ReleaseInventory,
|
// })
|
||||||
ProcessRefund,
|
|
||||||
})
|
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusReturned, ActionChain{
|
// GlobalRegistry.Register(enums.OrderStatusPending, ActionChain{})
|
||||||
SendReturnConfirmationEmail,
|
// }
|
||||||
NotifyReturnsDepartment,
|
|
||||||
})
|
|
||||||
|
|
||||||
GlobalRegistry.Register(enums.OrderStatusRefunded, ActionChain{
|
// var SendOrderConfirmationEmail = WithID("send_order_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
NotifyRefundProcessed,
|
// 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 {
|
// var NotifyWarehouse = WithID("notify_warehouse", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Sending order confirmation email for order %d", actionCtx.OrderId)
|
// log.Printf("Notifying warehouse for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var NotifyInventorySystem = WithID("notify_inventory_system", func(actionCtx ActionContext) ActionResult {
|
// var ReserveInventory = WithID("reserve_inventory", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Notifying inventory system for order %d", actionCtx.OrderId)
|
// log.Printf("Reserving inventory for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var NotifyWarehouse = WithID("notify_warehouse", func(actionCtx ActionContext) ActionResult {
|
// var NotifyWarehouseShipped = WithID("notify_warehouse_shipped", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Notifying warehouse for order %d", actionCtx.OrderId)
|
// log.Printf("Notifying warehouse of shipment for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var ReserveInventory = WithID("reserve_inventory", func(actionCtx ActionContext) ActionResult {
|
// var GenerateTrackingNumber = WithID("generate_tracking_number", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Reserving inventory for order %d", actionCtx.OrderId)
|
// log.Printf("Generating tracking number for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var NotifyWarehouseShipped = WithID("notify_warehouse_shipped", func(actionCtx ActionContext) ActionResult {
|
// var SendShippingNotificationEmail = WithID("send_shipping_notification_email", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Notifying warehouse of shipment for order %d", actionCtx.OrderId)
|
// log.Printf("Sending shipping notification email for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var GenerateTrackingNumber = WithID("generate_tracking_number", func(actionCtx ActionContext) ActionResult {
|
// var SendDeliveryConfirmationEmail = WithID("send_delivery_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Generating tracking number for order %d", actionCtx.OrderId)
|
// log.Printf("Sending delivery confirmation email for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var SendShippingNotificationEmail = WithID("send_shipping_notification_email", func(actionCtx ActionContext) ActionResult {
|
// var NotifyFulfillmentComplete = WithID("notify_fulfillment_complete", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Sending shipping notification email for order %d", actionCtx.OrderId)
|
// log.Printf("Notifying fulfillment complete for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var SendDeliveryConfirmationEmail = WithID("send_delivery_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
// var SendCancellationEmail = WithID("send_cancellation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Sending delivery confirmation email for order %d", actionCtx.OrderId)
|
// log.Printf("Sending cancellation email for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var NotifyFulfillmentComplete = WithID("notify_fulfillment_complete", func(actionCtx ActionContext) ActionResult {
|
// var ReleaseInventory = WithID("release_inventory", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Notifying fulfillment complete for order %d", actionCtx.OrderId)
|
// log.Printf("Releasing inventory for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var SendCancellationEmail = WithID("send_cancellation_email", func(actionCtx ActionContext) ActionResult {
|
// var ProcessRefund = WithID("process_refund", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Sending cancellation email for order %d", actionCtx.OrderId)
|
// log.Printf("Processing refund for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var ReleaseInventory = WithID("release_inventory", func(actionCtx ActionContext) ActionResult {
|
// var SendReturnConfirmationEmail = WithID("send_return_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Releasing inventory for order %d", actionCtx.OrderId)
|
// log.Printf("Sending return confirmation email for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var ProcessRefund = WithID("process_refund", func(actionCtx ActionContext) ActionResult {
|
// var NotifyReturnsDepartment = WithID("notify_returns_department", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Processing refund for order %d", actionCtx.OrderId)
|
// log.Printf("Notifying returns department for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// return ActionResult{Err: nil}
|
||||||
})
|
// })
|
||||||
|
|
||||||
var SendReturnConfirmationEmail = WithID("send_return_confirmation_email", func(actionCtx ActionContext) ActionResult {
|
// var NotifyRefundProcessed = WithID("notify_refund_processed", func(actionCtx ActionContext) ActionResult {
|
||||||
log.Printf("Sending return confirmation email for order %d", actionCtx.OrderId)
|
// log.Printf("Notifying refund processed for order %d", actionCtx.OrderId)
|
||||||
return ActionResult{Err: nil}
|
// 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}
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package orderStatusActions
|
package orderStatusActions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
"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/model/enums"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/service/emailService"
|
"git.ma-al.com/goc_daniel/b2b/app/service/emailService"
|
||||||
|
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var GlobalRegistry = make(ActionRegistry)
|
var GlobalRegistry = make(ActionRegistry)
|
||||||
@@ -13,7 +12,6 @@ var GlobalRegistry = make(ActionRegistry)
|
|||||||
type ActionID string
|
type ActionID string
|
||||||
|
|
||||||
type ActionContext struct {
|
type ActionContext struct {
|
||||||
OrderId uint
|
|
||||||
Order *model.CustomerOrder
|
Order *model.CustomerOrder
|
||||||
UserId *uint
|
UserId *uint
|
||||||
EmailService *emailService.EmailService
|
EmailService *emailService.EmailService
|
||||||
@@ -37,7 +35,12 @@ func (c ActionChain) Execute(actionCtx ActionContext) []ActionResult {
|
|||||||
result := action.Execute(actionCtx)
|
result := action.Execute(actionCtx)
|
||||||
results = append(results, result)
|
results = append(results, result)
|
||||||
if result.Err != nil {
|
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
|
return results
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
fmt.Printf("userID: %v\n", userID)
|
|
||||||
subject := "New Order Created"
|
subject := "New Order Created"
|
||||||
body := s.newOrderPlacedTemplate(userID)
|
body := s.newOrderPlacedTemplate(userID)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user