fix products listing

This commit is contained in:
2026-03-27 23:17:21 +01:00
parent ec05101037
commit 9ec329b1d6
429 changed files with 9816 additions and 3774 deletions

View File

@@ -6,6 +6,7 @@ import (
"git.ma-al.com/goc_daniel/b2b/app/config"
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/service/authService"
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
"github.com/gofiber/fiber/v3"
)
@@ -60,8 +61,8 @@ func AuthMiddleware() fiber.Handler {
}
// Set user in context
c.Locals("user", user.ToSession())
c.Locals("userID", user.ID)
c.Locals(constdata.USER_LOCALES_NAME, user.ToSession())
c.Locals(constdata.USER_LOCALES_ID, user.ID)
return c.Next()
}

View File

@@ -60,8 +60,8 @@ func (h *ListProductsHandler) GetListing(c fiber.Ctx) error {
var columnMappingGetListing map[string]string = map[string]string{
"product_id": "ps.id_product",
"name": "pl.name",
"reference": "ps.reference",
"reference": "p.reference",
"category_name": "cl.name",
"id_category": "cp.id_category",
"category_id": "cp.id_category",
"quantity": "sa.quantity",
}

View File

@@ -1,9 +1,11 @@
package restricted
import (
"strconv"
"encoding/json"
"fmt"
"git.ma-al.com/goc_daniel/b2b/app/service/meiliService"
searchservice "git.ma-al.com/goc_daniel/b2b/app/service/searchService"
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
@@ -12,26 +14,23 @@ import (
)
type MeiliSearchHandler struct {
meiliService *meiliService.MeiliService
meiliService *meiliService.MeiliService
searchService *searchservice.SearchService
}
func NewMeiliSearchHandler() *MeiliSearchHandler {
meiliService := meiliService.New()
return &MeiliSearchHandler{
meiliService: meiliService,
meiliService: meiliService.New(),
searchService: searchservice.New(),
}
}
func MeiliSearchHandlerRoutes(r fiber.Router) fiber.Router {
handler := NewMeiliSearchHandler()
// for testing purposes only. Must be removed before proper release.
r.Get("/create-index", handler.CreateIndex)
r.Get("/test", handler.Test)
r.Get("/settings", handler.GetSettings)
// for all users
r.Get("/search", handler.Search)
r.Post("/search", handler.Search)
r.Post("/settings", handler.GetSettings)
return r
}
@@ -45,6 +44,7 @@ func (h *MeiliSearchHandler) CreateIndex(c fiber.Ctx) error {
err := h.meiliService.CreateIndex(id_lang)
if err != nil {
fmt.Printf("CreateIndex error: %v\n", err)
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
@@ -53,22 +53,6 @@ func (h *MeiliSearchHandler) CreateIndex(c fiber.Ctx) error {
return c.JSON(response.Make(&nothing, 0, i18n.T_(c, response.Message_OK)))
}
func (h *MeiliSearchHandler) Test(c fiber.Ctx) error {
id_lang, ok := c.Locals("langID").(uint)
if !ok {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
test, err := h.meiliService.Test(id_lang)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&test, 0, i18n.T_(c, response.Message_OK)))
}
func (h *MeiliSearchHandler) Search(c fiber.Ctx) error {
id_lang, ok := c.Locals("langID").(uint)
if !ok {
@@ -76,22 +60,31 @@ func (h *MeiliSearchHandler) Search(c fiber.Ctx) error {
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
query := c.Query("query")
index := meiliService.GetIndexName(id_lang)
id_category_attribute := c.Query("id_category", "0")
id_category, err := strconv.Atoi(id_category_attribute)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
var body map[string]interface{}
if err := json.Unmarshal(c.Body(), &body); err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
}
meili_response, err := h.meiliService.Search(id_lang, query, uint(id_category))
result, err := h.searchService.Search(index, c.Body(), id_lang)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&meili_response, 0, i18n.T_(c, response.Message_OK)))
if h.searchService.IsIndexNotFound(result.Body) {
if createErr := h.meiliService.CreateIndex(id_lang); createErr == nil {
result, err = h.searchService.Search(index, c.Body(), id_lang)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
}
}
return c.Status(result.StatusCode).Send(result.Body)
}
func (h *MeiliSearchHandler) GetSettings(c fiber.Ctx) error {
@@ -101,11 +94,13 @@ func (h *MeiliSearchHandler) GetSettings(c fiber.Ctx) error {
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
settings, err := h.meiliService.GetIndexSettings(id_lang)
index := meiliService.GetIndexName(id_lang)
result, err := h.searchService.GetIndexSettings(index)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&settings, 0, i18n.T_(c, response.Message_OK)))
return c.Status(result.StatusCode).Send(result.Body)
}

View File

@@ -16,9 +16,7 @@ import (
"git.ma-al.com/goc_daniel/b2b/app/delivery/web/api/restricted"
"git.ma-al.com/goc_daniel/b2b/app/delivery/web/general"
// "github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v3"
// "github.com/gofiber/fiber/v3/middleware/filesystem"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/gofiber/fiber/v3/middleware/recover"
)
@@ -110,7 +108,7 @@ func (s *Server) Setup() error {
restricted.MenuHandlerRoutes(menu)
// meili search (restricted)
meiliSearch := s.restricted.Group("/meili-search")
meiliSearch := s.restricted.Group("/search")
restricted.MeiliSearchHandlerRoutes(meiliSearch)
// carts (restricted)