diff --git a/app/module/products/service/products.service.go b/app/module/products/service/products.service.go index ba445db..701d944 100644 --- a/app/module/products/service/products.service.go +++ b/app/module/products/service/products.service.go @@ -30,13 +30,12 @@ import ( "github.com/rs/zerolog" ) - type productsService struct { - Repo repository.ProductsRepository - Log zerolog.Logger - Cfg *config.Config - MinioStorage *minioStorage.MinioStorage - UsersRepo usersRepository.UsersRepository + Repo repository.ProductsRepository + Log zerolog.Logger + Cfg *config.Config + MinioStorage *minioStorage.MinioStorage + UsersRepo usersRepository.UsersRepository ApprovalHistoriesService approvalHistoriesService.ApprovalHistoriesService } @@ -310,25 +309,63 @@ func (_i *productsService) Delete(id uint) (err error) { func (_i *productsService) Viewer(c *fiber.Ctx) (err error) { filename := c.Params("filename") - // Find product by filename (repository will search using LIKE pattern) + var objectName string + var found bool + + // First, try to find by ThumbnailPath (for main product images) result, err := _i.Repo.FindByThumbnailPath(filename) - if err != nil { + if err == nil && result != nil && result.ThumbnailPath != nil && *result.ThumbnailPath != "" { + // Check if the filename matches the thumbnail + if strings.Contains(*result.ThumbnailPath, filename) { + objectName = *result.ThumbnailPath + found = true + } + } + + // If not found in ThumbnailPath, search in Colors JSON field + if !found { + // Create a query request with large limit to search all products + queryReq := request.ProductsQueryRequest{ + Pagination: &paginator.Pagination{ + Page: 1, + Limit: 1000, // Large limit to search all products + }, + } + allProducts, _, err := _i.Repo.GetAll(queryReq) + if err == nil { + for _, product := range allProducts { + if product.Colors != nil && *product.Colors != "" { + var rawColors []struct { + Name string `json:"name"` + ImagePath *string `json:"image_path"` + } + + if err := json.Unmarshal([]byte(*product.Colors), &rawColors); err == nil { + for _, color := range rawColors { + if color.ImagePath != nil && strings.Contains(*color.ImagePath, filename) { + objectName = *color.ImagePath + found = true + break + } + } + } + if found { + break + } + } + } + } + } + + if !found || objectName == "" { return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ "error": true, "msg": "Product file not found", }) } - if result.ThumbnailPath == nil || *result.ThumbnailPath == "" { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ - "error": true, - "msg": "Product thumbnail path not found", - }) - } - ctx := context.Background() bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName - objectName := *result.ThumbnailPath _i.Log.Info().Str("timestamp", time.Now(). Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer"). @@ -370,8 +407,6 @@ func (_i *productsService) Viewer(c *fiber.Ctx) (err error) { return } - - func getFileExtension(filename string) string { // split file name parts := strings.Split(filename, ".") @@ -409,14 +444,14 @@ func (_i *productsService) Approve(id uint, authToken string) (product *response userID := user.ID statusApprove := 2 - err = _i.ApprovalHistoriesService.CreateHistory( - "products", - id, - &statusApprove, // ✅ pointer - "approve", - &userID, - nil, - ) + err = _i.ApprovalHistoriesService.CreateHistory( + "products", + id, + &statusApprove, // ✅ pointer + "approve", + &userID, + nil, + ) if err != nil { _i.Log.Error().Err(err).Msg("Failed to save approval history") } @@ -461,14 +496,14 @@ func (_i *productsService) Reject(id uint, authToken string, message *string) (p userID := user.ID statusReject := 3 - err = _i.ApprovalHistoriesService.CreateHistory( - "productss", - id, - &statusReject, // ✅ pointer - "reject", - &userID, - message, - ) + err = _i.ApprovalHistoriesService.CreateHistory( + "productss", + id, + &statusReject, // ✅ pointer + "reject", + &userID, + message, + ) if err != nil { _i.Log.Error().Err(err).Msg("Failed to save rejection history") } @@ -511,7 +546,7 @@ func (_i *productsService) Comment( err = _i.ApprovalHistoriesService.CreateHistory( "banners", id, - nil, // status_id NULL + nil, // status_id NULL "comment", &userID, message, diff --git a/go.mod b/go.mod index 6c79ec8..b1bb13a 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/google/uuid v1.6.0 github.com/minio/minio-go/v7 v7.0.68 github.com/pelletier/go-toml/v2 v2.1.1 github.com/rs/zerolog v1.31.0 @@ -34,7 +35,6 @@ require ( github.com/go-openapi/spec v0.20.15 // indirect github.com/go-openapi/swag v0.22.10 // indirect github.com/go-resty/resty/v2 v2.7.0 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v5 v5.4.3 // indirect