feat: create customer management menu

This commit is contained in:
2026-04-17 09:17:26 +02:00
parent 7bce04e05a
commit 612e97e76e
11 changed files with 190 additions and 9 deletions

View File

@@ -3,6 +3,8 @@ package restricted
import (
"strconv"
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware"
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms"
"git.ma-al.com/goc_daniel/b2b/app/service/menuService"
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
@@ -29,6 +31,7 @@ func MenuHandlerRoutes(r fiber.Router) fiber.Router {
r.Get("/get-category-tree", handler.GetCategoryTree)
r.Get("/get-breadcrumb", handler.GetBreadcrumb)
r.Get("/get-top-menu", handler.GetTopMenu)
r.Get("/get-customer-management-menu", middleware.Require(perms.UserReadAny), handler.GetCustomerManagementMenu)
return r
}
@@ -100,3 +103,18 @@ func (h *MenuHandler) GetTopMenu(c fiber.Ctx) error {
return c.JSON(response.Make(&menu, len(menu), i18n.T_(c, response.Message_OK)))
}
func (h *MenuHandler) GetCustomerManagementMenu(c fiber.Ctx) error {
langId, ok := localeExtractor.GetLangID(c)
if !ok {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
menu, err := h.menuService.GetCustomerManagementMenu(langId)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&menu, len(menu), i18n.T_(c, response.Message_OK)))
}

View File

@@ -2,7 +2,7 @@ package model
import "encoding/json"
type B2BTopMenu struct {
type B2BMenu struct {
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement" json:"menu_id"`
Label json.RawMessage `gorm:"column:label;type:longtext;not null;default:'{}'" json:"label"`
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id" json:"parent_id,omitempty"`
@@ -10,10 +10,22 @@ type B2BTopMenu struct {
Active int8 `gorm:"column:active;type:tinyint;not null;default:1" json:"active"`
Position int `gorm:"column:position;not null;default:1" json:"position"`
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT" json:"parent,omitempty"`
Children []*B2BTopMenu `gorm:"foreignKey:ParentID" json:"children,omitempty"`
Parent *B2BMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT" json:"parent,omitempty"`
Children []*B2BMenu `gorm:"foreignKey:ParentID" json:"children,omitempty"`
}
type B2BTopMenu struct {
B2BMenu
}
func (B2BTopMenu) TableName() string {
return "b2b_top_menu"
}
type B2BCustomerManagementMenu struct {
B2BMenu
}
func (B2BCustomerManagementMenu) TableName() string {
return "b2b_customer_management_menu"
}

View File

@@ -9,6 +9,7 @@ import (
type UIRoutesRepo interface {
GetRoutes(langId uint, roleId uint) ([]model.Route, error)
GetTopMenu(id uint, roleId uint) ([]model.B2BTopMenu, error)
GetCustomerManagementMenu(langId uint) ([]model.B2BCustomerManagementMenu, error)
}
type RoutesRepo struct{}
@@ -38,10 +39,23 @@ func (p *RoutesRepo) GetTopMenu(langId uint, roleId uint) ([]model.B2BTopMenu, e
Get().
Model(model.B2BTopMenu{}).
Joins("JOIN b2b_top_menu_roles tmr ON tmr.top_menu_id = b2b_top_menu.menu_id").
Where(model.B2BTopMenu{Active: 1}).
Where(model.B2BTopMenu{B2BMenu: model.B2BMenu{Active: 1}}).
Where("tmr.role_id = ?", roleId).
Order("b2b_top_menu.parent_id ASC, b2b_top_menu.position ASC").
Find(&menus).Error
return menus, err
}
func (p *RoutesRepo) GetCustomerManagementMenu(langId uint) ([]model.B2BCustomerManagementMenu, error) {
var menus []model.B2BCustomerManagementMenu
err := db.
Get().
Model(model.B2BCustomerManagementMenu{}).
Where(model.B2BCustomerManagementMenu{B2BMenu: model.B2BMenu{Active: 1}}).
Order("b2b_customer_management_menu.parent_id ASC, b2b_customer_management_menu.position ASC").
Find(&menus).Error
return menus, err
}

View File

@@ -193,18 +193,41 @@ 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 {
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 {
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 +249,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) {