fix: edit table and migrations
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
subject := "New Order Created"
|
||||
body := s.newOrderPlacedTemplate(userID)
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
routesRepo "git.ma-al.com/goc_daniel/b2b/app/repos/routesRepo"
|
||||
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/utils/responseErrors"
|
||||
)
|
||||
|
||||
@@ -193,18 +194,52 @@ func (s *MenuService) GetBreadcrumb(root_category_id uint, start_category_id uin
|
||||
return breadcrumb, nil
|
||||
}
|
||||
|
||||
func (s *MenuService) GetTopMenu(languageId uint, roleId uint) ([]*model.B2BTopMenu, error) {
|
||||
func (s *MenuService) GetTopMenu(languageId uint, roleId uint) ([]*model.B2BMenu, error) {
|
||||
items, err := s.routesRepo.GetTopMenu(languageId, roleId)
|
||||
if err != nil {
|
||||
logger.Error("failed to get top menu",
|
||||
"handler", "ManuService.GetTopMenu",
|
||||
"language_id", languageId,
|
||||
"role_id", roleId,
|
||||
"error", err.Error(),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
menuMap := make(map[int]*model.B2BTopMenu, len(items))
|
||||
roots := make([]*model.B2BTopMenu, 0)
|
||||
menus := make([]model.B2BMenu, len(items))
|
||||
for i := range items {
|
||||
menus[i] = items[i].B2BMenu
|
||||
}
|
||||
|
||||
return buildMenu(menus), nil
|
||||
}
|
||||
|
||||
func (s *MenuService) GetCustomerManagementMenu(languageId uint) ([]*model.B2BMenu, error) {
|
||||
items, err := s.routesRepo.GetCustomerManagementMenu(languageId)
|
||||
if err != nil {
|
||||
logger.Error("failed to get customer management menu",
|
||||
"handler", "ManuService.GetCustomerManagementMenu",
|
||||
"language_id", languageId,
|
||||
"error", err.Error(),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
menus := make([]model.B2BMenu, len(items))
|
||||
for i := range items {
|
||||
menus[i] = items[i].B2BMenu
|
||||
}
|
||||
|
||||
return buildMenu(menus), nil
|
||||
}
|
||||
|
||||
func buildMenu(items []model.B2BMenu) []*model.B2BMenu {
|
||||
menuMap := make(map[int]*model.B2BMenu, len(items))
|
||||
roots := make([]*model.B2BMenu, 0)
|
||||
|
||||
for i := range items {
|
||||
menu := &items[i]
|
||||
menu.Children = make([]*model.B2BTopMenu, 0)
|
||||
menu.Children = make([]*model.B2BMenu, 0)
|
||||
menuMap[menu.MenuID] = menu
|
||||
}
|
||||
|
||||
@@ -226,7 +261,7 @@ func (s *MenuService) GetTopMenu(languageId uint, roleId uint) ([]*model.B2BTopM
|
||||
parent.Children = append(parent.Children, menu)
|
||||
}
|
||||
|
||||
return roots, nil
|
||||
return roots
|
||||
}
|
||||
|
||||
func (s *MenuService) appendAdditional(all_categories *[]model.ScannedCategory, id_lang uint, iso_code string) {
|
||||
|
||||
@@ -3,8 +3,10 @@ package orderService
|
||||
import (
|
||||
"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/repos/productsRepo"
|
||||
@@ -23,6 +25,7 @@ type OrderService struct {
|
||||
productsRepo productsRepo.UIProductsRepo
|
||||
addressesService *addressesService.AddressesService
|
||||
emailService *emailService.EmailService
|
||||
actionRegistry *orderStatusActions.ActionRegistry
|
||||
}
|
||||
|
||||
func New() *OrderService {
|
||||
@@ -32,9 +35,23 @@ func New() *OrderService {
|
||||
productsRepo: productsRepo.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
|
||||
@@ -63,7 +80,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
|
||||
@@ -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)
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
@@ -109,19 +126,8 @@ func (s *OrderService) PlaceNewOrder(user_id uint, cart_id uint, name string, co
|
||||
)
|
||||
}
|
||||
|
||||
// send email to admin
|
||||
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 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 {
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (s *OrderService) getOrderTotalPrice(user_id uint, cart_id uint, country_id uint) (float64, float64, float64, error) {
|
||||
|
||||
Reference in New Issue
Block a user