feat: order action per status change
This commit is contained in:
@@ -122,7 +122,7 @@ func (s *EmailService) SendNewOrderPlacedNotification(userID uint) error {
|
||||
if s.config.AdminEmail == "" {
|
||||
return nil // No admin email configured
|
||||
}
|
||||
|
||||
fmt.Printf("userID: %v\n", userID)
|
||||
subject := "New Order Created"
|
||||
body := s.newOrderPlacedTemplate(userID)
|
||||
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"fmt"
|
||||
"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/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/ordersRepo"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/addressesService"
|
||||
@@ -20,6 +22,7 @@ type OrderService struct {
|
||||
cartsRepo cartsRepo.UICartsRepo
|
||||
addressesService *addressesService.AddressesService
|
||||
emailService *emailService.EmailService
|
||||
actionRegistry *orderStatusActions.ActionRegistry
|
||||
}
|
||||
|
||||
func New() *OrderService {
|
||||
@@ -28,9 +31,23 @@ func New() *OrderService {
|
||||
cartsRepo: cartsRepo.New(),
|
||||
addressesService: addressesService.New(),
|
||||
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) {
|
||||
if !user.HasPermission(perms.OrdersViewAll) {
|
||||
// append filter to view only this user's orders
|
||||
@@ -56,7 +73,7 @@ func (s *OrderService) Find(user *model.Customer, p find.Paging, filt *filters.F
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -83,7 +100,7 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
||||
}
|
||||
|
||||
// all checks passed
|
||||
err = s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info)
|
||||
order, err := s.ordersRepo.PlaceNewOrder(cart, name, country_id, address_info, originalUserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,16 +113,8 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
||||
_ = err
|
||||
}
|
||||
|
||||
// send email to admin
|
||||
go func(user_id uint) {
|
||||
err := s.emailService.SendNewOrderPlacedNotification(user_id)
|
||||
if err != nil {
|
||||
// Log error but don't fail placing order
|
||||
_ = err
|
||||
}
|
||||
}(user_id)
|
||||
return s.ChangeOrderStatus(user_id, order.OrderID, enums.OrderStatusPending)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *OrderService) ChangeOrderAddress(user *model.Customer, order_id uint, country_id uint, address_info string) error {
|
||||
@@ -128,18 +137,33 @@ func (s *OrderService) ChangeOrderAddress(user *model.Customer, order_id uint, c
|
||||
return s.ordersRepo.ChangeOrderAddress(order_id, country_id, address_info)
|
||||
}
|
||||
|
||||
// This is obiously just an initial version of this function
|
||||
func (s *OrderService) ChangeOrderStatus(user *model.Customer, order_id uint, status string) error {
|
||||
if !user.HasPermission(perms.OrdersModifyAll) {
|
||||
exists, err := s.ordersRepo.UserHasOrder(user.ID, order_id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return responseErrors.ErrUserHasNoSuchOrder
|
||||
}
|
||||
func (s *OrderService) ChangeOrderStatus(userId, orderId uint, newStatus enums.OrderStatus) error {
|
||||
order, err := s.ordersRepo.Get(orderId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if order == nil {
|
||||
return responseErrors.ErrOrderNotFound
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user