diff --git a/main b/main index 04774a8..ccf28fd 100755 Binary files a/main and b/main differ diff --git a/routers/router/api/v1/productpipeline/productpipeline.go b/routers/router/api/v1/productpipeline/productpipeline.go index 8a9f22d..d719d4d 100644 --- a/routers/router/api/v1/productpipeline/productpipeline.go +++ b/routers/router/api/v1/productpipeline/productpipeline.go @@ -10,7 +10,9 @@ import ( "jannex/admin-dashboard-backend/modules/logger" "jannex/admin-dashboard-backend/modules/notification" "jannex/admin-dashboard-backend/modules/structs" + "sort" "strconv" + "time" "git.ex.umbach.dev/Alex/roese-utils/rslogger" "git.ex.umbach.dev/Alex/roese-utils/rsutils" @@ -201,6 +203,47 @@ func fetchProducts(srv *sheets.Service) error { } } + // sort new products + + // compare function + comparePublishedAt := func(a, b structs.NewProduct) int { + if a.PublishedAt == "" && b.PublishedAt == "" { + return 0 + } + if a.PublishedAt == "" { + return 1 + } + if b.PublishedAt == "" { + return -1 + } + + dateFormat := "02.01.2006" + dateA, errA := time.Parse(dateFormat, a.PublishedAt) + dateB, errB := time.Parse(dateFormat, b.PublishedAt) + + if errA != nil || errB != nil { + return 0 // Handle parsing errors if necessary + } + + if dateA.After(dateB) { + return -1 + } + if dateA.Before(dateB) { + return 1 + } + return 0 + } + + // sort slice + sort.Slice(newProducts, func(i, j int) bool { + return comparePublishedAt(newProducts[i], newProducts[j]) < 0 + }) + + // limit to max 10 new products + if len(newProducts) > 10 { + newProducts = newProducts[:10] + } + cache.SetPipelineProductsCache(structs.PipelineProductsCache{ NewProducts: newProducts, InWorkProducts: inWorkProducts,